Generate Code and Program Device

Click Generate in the upper left sub-window in MPLAB to generate code. The generated code is the final code since this is a software-free solution.

As already described, since both the timer and CLCs can run in sleep mode, one can set the device in sleep mode in order to minimize the power consumption. This is done by inserting the following line of code after the system initialization in the main() function of the main.c file. However, this is optional.

SLEEP();

Build the project with short-cut key F11 and make sure no error occurs. Then click the Make and Program Device Main Project icon on the top toolbar in MPLAB to program the device. Now it is ready to detect the onboard LEDs D2 and D3 toggling when the onboard S2 button is pressed/released. The onboard LEDs D4 and D5 should toggle when the external rotary encoder connected to the input pin RB2 gets rotated. It is also recommended to use an oscilloscope to monitor how the debouncing is performed when the noise pulses are filtered out.

The user should now be able to reproduce the solution by following the above configurations.