Timer2 Example using MPLAB® Code Configurator

Last modified by Microchip on 2023/11/09 09:03

MPLAB® Code Configurator (MCC) makes setting up a 10-bit PWM peripheral easy. The steps include setting up the I/O, Timer2, and PWM module to make it run. MCC will automatically generate the code to load the proper registers and initialize the proper values to produce the desired PWM signal.

The best way to show how this is done is through a simple example. We will configure a PIC16F1825 Capture/Compare/PWM peripheral to create a PWM signal at 500 Hz, 50% duty cycle using a 4 MHz system clock and 1:16 prescaler.

MCC CCP:PWM Setup

The first step after launching MCC within MPLAB X IDE is to select the peripherals we will use and set up the PWM. As seen in the Project Resources figure, the three resources required are the System, TMR2::Timer, and the CCP3:PWM modules with the MCC list of options.

Project Resources

Project Resources

Back to top

System Setup

The system is where the oscillator speed is selected and any changes to the configuration settings you may need. Select the 4 MHz internal oscillator as shown in the Clock Setup figure.

Clock setup

Clock setup

Back to top

Timer2 Setup

Timer2 uses the oscillator selected in the System section to adjust the Timer2 period. Enter the time of "2.0 ms" for the period to yield a 500 Hz frequency. Select the prescaler as 1:16 from the drop-down menu. Check the Start Timer After Initialization box. This will start the timer running and also the PWM signal after the PIC16F1825 finishes initializing all the peripherals.

Timer 2 Setup

Timer 2 Setup

Back to top

CCP3:PWM Setup

By selecting the CCP3:PWM, MCC automatically selects the I/O pin RA2 in the I/O selection window. The RA2 pin actually shows up with the label CCP3 in green to show that the CCP3 peripheral now controls the I/O pin.

CCP I/O Setup

CCP I/O Setup

The CCP3:PWM setup screen is where the Duty Cycle is selected. Enter "50" for 50%.

PWM period and frequency are displayed in this window as well based on the Timer2 selection window.

CCP Timer Setup

CCP Timer Setup

Back to top

Generate Code

When all the setup screens are complete, click the MCC Generate Code button

 generate.png

and MCC produces the software files for the project.

The MCC will produce a main.c file that contains a System_Initialize function as its only component.

System Initialize Code

System Initialize Code

The System_Initialize function is placed in a file named mcc.c.
System_Initialize calls four functions:

OSCILLATOR_Initialize();
PIN_MANAGER_Initialize();
TMR2_Initialize();
PWM3_Initialize();

CCP Register Setup

CCP Register Setup

The OSCILLATOR_Initialize function takes the Oscillator Settings selected and sets up the proper registers for the 4 MHz internal oscillator.

Oscillator Register Setup

Oscillator Register Setup

The PIN_MANAGER_Initialize function sets the registers for the I/O pins.

Pin Register Setup

Pin Register Setup

The TMR2_Initialize function sets the registers for the Timer2 settings selected including the prescaler and PR2 value.

Timer 2 Register Setup

Timer 2 Register Setup

The PWM3_Initialize function selects the settings for the 50% duty cycle value. Notice the CCP3RL register is loaded with the proper value to create the proper high time of the 50% duty cycle.

50% Duty Cycle

50% Duty Cycle 

The code is then compiled within MPLABX IDE environment and programmed into the PIC16F1825. The device will start operating as soon as it's powered up. Timer2 will start running immediately after the initialization phase of the code. The results are shown on the Oscilloscope Output screen capture. The screen capture shows, in the measurement section, a period of 2 milliseconds and frequency of 500 Hz as we expected. Each pulse is an equal 1 milliseconds off the center of the signal for a perfect 50% duty cycle.

Oscilloscope Output

Oscilloscope Output

Back to top