Academic Program

Generate Drivers and Modify main.c

Generate Drivers and Modify main.c

Step 9 - Click on the Generate code button to have MCC create the software libraries for this project.

generate.png

 

Step 10 -The project will now have both generated header and source files. It should also have a generated main.c file.

Note: MCC may offer to generate a main.c file. Click on Yes to allow it to complete this task.

proj5files.png

 

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

Step 11 - 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. To perform this routine, the following code needs to be entered in the main.c file under the while (1) area where the MCC generated template says // Add your application code.

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

 

The PWM6_LoadDutyValue(duty) function was generated by the MCC and its definition is located in the pwm6.h file.
The PWM6_Initialize() function is also generated to initialize the PWM peripheral. It's also located in the pwm6.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.

Step 12 - Click on the Build Project (hammer icon) to compile the code and you should see a "BUILD SUCCESSFUL" message in the Output window of MPLAB X.

Main_Build_Project.png

BUILD SUCCESSFUL