Academic Program

Modify main.c to Implement Application Algorithm

Double click on the main.c file to open it up in the Editor window.

The PWM Duty cycle is adjusted by a value of 0 to 1023 in the PWM control routine. This is a 10-bit resolution. This example will only use half of the range of 1 to 512 to change the brightness of the LED connected to the PWM output. The main loop of code adjusts the PWM value in increments of ten and uses a for-loop delay routine to allow the PWM value to be displayed on the D2 (RA4) LED. 

We need to add the following code to the main.c file:

while (1)
    {
        // Add your application code
        PWM4_Initialize();
        for (int duty = 1; duty < 512; duty = duty + 10)
        {
            PWM4_LoadDutyValue(duty);
            for (int delay = 1; delay < 500; delay++)
            {;}
        }
    }

 

The PWM4_LoadDutyValue(duty) function was generated by the MCC and its definition is located in the pwm4.h file.
The PWM4_Initialize() function is also generated to initialize the PWM peripheral. It's also located in the pwm4.h file.

This is a simple example of how the MCC generates a library of useful functions to make creating a PWM project code much easier and quicker.

Next, click on the Build Main Project icon Build Main Project icon to compile the code. You should see a BUILD SUCCESSFUL message in the output window.

Build project