Step 4: Add Application Code to the Project

Last modified by Microchip on 2026/06/26 07:37

Add Application Code to the Project

Once the project is generated, open the main.c file and add the following lines of code (inside while loop) after SYS_Initialize(NULL); inside the int main(void) function.

main.c file

The LED1_Toggle() function changes the state of LED1. If LED1 is ON, it turns OFF and vice versa. This is how the LED is made to blink.

Information

Note: You can also view the list of various functions of General-Purpose Input/Output (GPIO). Enter the name of the "GPIO" (here LED1) and “_”, then Ctrl + Space to view the list of functions.

GPIO

The value 1500000UL used in the for loop is selected to introduce a delay of approximately 500 ms when the microcontroller operates at a clock frequency of 48 MHz. The suffix UL denotes an Unsigned Long data type, which is capable of holding large integer values.

This value is chosen based on instruction execution analysis. The function LED1_Toggle() requires approximately six assembly instructions, while the loop for (int i = 0; i < 1500000UL; i++); requires approximately 11 to 12 assembly instructions. Together, this results in an estimated total of 18 assembly instructions per iteration. 

LED1_Toggle() function

The PIC32CM LS00 Curiosity Nano + Touch Evaluation Kit operates at a clock frequency of 48 MHz, meaning it executes 48 million instructions per second. For a 500 ms delay, approximately 24 million instructions are executed. Dividing this instruction count by the estimated number of assembly instructions per loop iteration (18) results in approximately 1,333,333 iterations. For simplicity and to ensure sufficient delay, this value is rounded up to 1,500,000.

Information

Note: To open the above disassembly window, go to Window > Debugging > Disassembly (this window will open if the project is in debug mode).

Window > Debugging > Disassembly

Back to Top