Navigation

Tuesday 8 September 2020

Timer2 Interrupt with PIC16F8XX Microcontroller

In this tutorial, we would perform the task of con guring the Timer2 interrupt in PIC16F8xx microcontroller for a period of 4ms and a duty cycle of 2ms. Timer2 is an 8-bit timer that can be used as the PWM time base for the PWM mode of the CCP2 module.

The following steps should be taken when con guring the CCP module for PWM operation:

1. Set the PWM period by writing to the PR2 register.

2. Set the PWM duty cycle by writing to the CCPR2L register and CCP2CON<5:4> bits.

3. Make the CCP2 pin an output by clearing the TRISC<1> bit.

4. Set the TMR2 prescale value and enable Timer2 by writing to T2CON.

5. Con gure the CCP2 module for PWM operation.

There are eight registers that will be used - these are INTCON, PIR1, PIE1, PIE2, T2CON, PR2, CCPR2L and CCP2CON registers.

INTCON Register

The INTCON Register contains the GIE and PEIE bits which must be set for Timer0 interrupt operation. As shown in Table 1, bit 7 and bit 6 are set. Timer0 interrupt can be enabled/disabled by setting/clearing the TMR0IE enable bit.

GIE = 1 enables all unmasked interrupts

PEIE = 1 Enables all unmasked peripheral interrupts

PIR1 Register

The PIR1 Register contains the TMR2IF bit (PIR1<1>). The TMR2IF bit is the TMR2 to PR2 Match Interrupt Flag bit. A value of 1 indicates that a TMR2 to PR2 match occurred (which must be cleared in software after the interrupt occurs) . A value of 0 indicates that no TMR2 to PR2 match occurred.

PIE1 Register

The PIE1 Register contains the TMR2IE bit (PIE1<1>). The TMR2IE bit is the TMR2 to PR2 Match Interrupt Enable bit. A value of 1 enables the TMR2 to PR2 match interrupt while a value of 0 disables the TMR2 to PR2 match interrupt.

PIE2 Register

The PIE2 register contains CCP2IE bit (PIE1<0>) which is the CCP2 Interrupt Enable bit. A value of 1 enables the CCP2 interrupt while a value of 0 disables it.

T2CON Register

The T2CON register is written into to set the TMR2 prescale value and enable Timer2

As shown in Table 2, the following are set up: TOUTPS3 : TOUTPS0: Timer2 Output Postscale Select bits = 0000 = 1:1 postscale

TMR2ON: Timer2 On bit = 1. Therefore, Timer2 is on

T2CKPS1 : T2CKPS0: Timer2 Clock Prescale Select bits = 1x Therefore, Prescaler is 16

PR2 Register

The Timer2 module has an 8-bit period register called the PR2 register. Timer2 increments from 00h until it matches PR2 and then resets to 00h on the next increment cycle. The PR2 register is written into to set the PWM period. The formula for the PWM period is given as:

Therefore, the formula for the value that would be written into the PR2 register is:

We want a PWM period of 4ms

FOSC is the frequency of oscillation, which is given by the frequency of the crystal oscillator, we choose a value of 4MHz.

TMR2 prescale value was set to 16 in the T2CON<1:0> Register.

Therefore the calculation becomes:

A value of 249 (11111001) is written into the PR2 register.

CCPR2L & CCP2CON registers

The PWM duty cycle of the CCP2 pin (RC1 pin) is speci ed by writing to the CCPR2L register and to the CCP2CON<5:4> bits. Up to 10-bit resolution is available. The CCPR2L contains the eight MSBs and the CCP2CON<5:4> contains the two LSBs. This 10-bit value is represented by CCPR2L:CCP2CON<5:4>. The following equation is used to calculate the PWM duty cycle:

The value written into CCPR2L:CCP2CON<5:4> is therefore given by the equation:

We want a duty cycle of 2ms. So the calculation becomes:

500 in binary is 01111101002. The 8 MSBs are 01111101 which is written into the CCPR2L Register. The 2 LSBs are 002 which are written into CCP2CON<5:4>.

The CCP2CON Register also has other bits that needs to be configured for PWM operation which are CCP2CON<3:0> bits

CCP2M3:CCP2M0 bits are the CCP2 Mode Select bits. As shown in Table 3, setting them to 11xx con gures the CCP2 module for PWM mode

The following code sums up the con figuration of the Timer2 interrupt

Figure 1 shows a 4ms period with a 2ms duty cycle on the RC1 (CCP2) pin of PIC16F876A microcontroller after con guration for Timer2 interrupt.