The Timer2/4/6 modules are 8-bit timers that incorporate the following features:
- 8-bit Timer and Period registers (TMR2 and PR2)
- Readable and writable (both registers)
- Software programmable prescaler (1:1, 1:4, 1:16, and 1:64)
- Software programmable postscaler (1:1 to 1:16)
- Interrupt on TMR2 match with PR2
- Optional use as the shift clock for the MSSP module
Some PIC® MCU devices only have a Timer2. Timer4 and Timer6 are just duplicates of the Timer2 peripheral. By adding more of this type of timer, a device can control multiple PWM outputs from different clock bases.
Below is a simplified block diagram of Timer2.
Timer2 has many features built in but its main feature is the ability to compare its contents to a fixed value stored in a separate register (PR2). When the two values match, then different events can be triggered, such as toggling a PWM pin from a high to low state.
Timer2/4/6 Operation
Timer2 is driven by the internal instruction clock (FOSC/4). The Timer2 (TMR2) register increments on each clock edge. A prescaler on the clock input allows direct input (1:1), divide-by-4, divide-by-16 and on some devices divide-by-64 prescale options. These options are selected by the prescaler control bits: T2CKPS of the Timer2 Control (T2CON) register. The value of TMR2 is compared to that of the Period (PR2) register on each clock cycle. When the two values match, the comparator generates a match signal as the timer output for other peripherals to use as a time base. A Timer2 interrupt can also be triggered by the match. The match signal becomes the input to an optional postscaler and also resets the value of TMR2 to 00h on the next cycle.
The TMR2 and PR2 registers are both directly readable and writable. The TMR2 register is cleared on any device reset, whereas the PR2 register initializes to FFh. Both the prescaler and postscaler counters are cleared on the following events:
- a write to the TMR2 register
- a write to the T2CON register
- Power-on Reset (POR)
- Brown-out Reset (BOR)
- MCLR Reset
- Watchdog Timer (WDT) Reset
- Stack Overflow Reset
- Stack Underflow Reset
- RESET Instruction
Timer2 can be turned on and off via the TMR2ON bit of the T2CON register.
Timer2/4/6 Interrupts
Timer2 can generate an interrupt when the TMR2 and PR2 registers match. That match signal can also feed a postscaler to delay the number of matches required to initiate a Timer2 interrupt. The output of the postscaler sets the Timer2 Interrupt Flag bit (TMR2IF) of the Peripheral Interrupt (PIR1) register. The interrupt is enabled by setting the TMR2 Match Interrupt Enable bit (TMR2IE) of the Peripheral Interrupt Enable (PIE1) register.
TOUTPS3:TOUTPS0
0000 = 1:1
0001 = 1:2
0010 = 1:3
…
1111 = 1:16
The postscaler has a range of 1:1 through 1:16 and is selected by the Timer Output Postscaler Select bits (T2OUTPS) of the Timer2 Control register (T2CON).
Timer2/4/6 Output Options
The output of the TMR2 match signal is available directly to the CCP modules, where it is typically used as a time base for operations in PWM mode. Having multiple Timer2 style time bases (e.g., Timer4, Timer6) can offer multiple unique PWM signals from a single PIC MCU device.
The TMR2 match signal can also be directed to the Master Synchronous Serial Port (MSSP) to act as the shift clock source when the MSSP is operating in SPI mode.
Timer2/4/6 Sleep Mode
The Timer2 timers cannot be operated while the processor is in Sleep mode. The contents of the TMR2 and PR2 registers will remain unchanged while the processor is in Sleep mode.
Timer2/4/6 Example using MPLAB Code Configurator
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 is to select the peripherals we will use and set up the PWM. As seen below, the three resources required are the System, TMR2::Timer, and the CCP3:PWM modules with the MCC list of options.
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 picture below.
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.
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.
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.
Generate Code
When all the setup screens are complete, click the MCC Generate Code button 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.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();
The OSCILLATOR_Initialize function takes the Oscillator Settings selected and sets up the proper registers for the 4 MHz internal oscillator.
The PIN_MANAGER_Initialize function sets the registers for the I/O pins.
The TMR2_Initialize function sets the registers for the Timer2 settings selected including the prescaler and PR2 value.
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.
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 screen capture below. 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.