PIC Timers
PIC Timers
Timers
• Timer0
• Timer1
• Timer2
• Timer3
Timer 0
• Timer0 can work as Timer/Counter in both 8-bit and 16-bit modes
• Dedicated 8-bit, software programmable prescaler
• Selectable clock source (internal or external)
• Interrupt on overflow
Timer1
• Timer1 can work as 16-bit timer or counter
• Readable and writable 8-bit registers (TMR1H and TMR1L)
• Selectable clock source (internal or external)
• Alternate clock source can be provided at Timer1 oscillator pins
(T1OSO & T1OSI)
• Interrupt on overflow
• Timer1 can be multiplexed with other peripherals like ADC etc. and
generates special event triggering for CCP (Capture, Compare and
PWM) events.
Timer2
• 8-bit Timer and Period registers (TMR2 and PR2, respectively)
• Software programmable prescaler (1:1, 1:4 and 1:16)
• Software programmable postscaler (1:1 – 1:16)
• Interrupt on TMR2 to PR2 match
• Optional use as the shift clock for the MSSP (Master Synchronous
Serial Port) module
Timer3
• Timer3 can work as 16-bit timer or counter
• Readable and writable 8-bit registers (TMR3H and TMR3L)
• Selectable clock source (internal or external)
• Alternate clock source can be provided at Timer1 oscillator pins
(T1OSO & T1OSI)
• Interrupt on overflow
• Timer3 can be multiplexed with other peripherals like ADC etc. and
generates special event triggering for CCP (Capture, Compare and
PWM) events.
Timer0 Block Diagram
PIC18F4550 Timer0 Registers
• T0CON (Timer0 Control Register)
• INTCON (Interrupt Control Register)
• TMR0 (Timer0 Register)
T0CON (Timer0 Control Register)
• PSA: This bit is set to high if there is no need to assign a prescaler value.
• 1 = Timer0 prescaler is not assigned. Timer0 clock input bypasses prescaler.
• 0 = Timer0 prescaler is assigned. Timer0 clock input comes from prescaler
output.
•
• T0SE: This bit is used when external source is selected for the Timer. This
bit is used to select external clock edge to increment the Timer.
• 1 = Increment on high-to-low transition on T0CKI pin (Pin6)
• 0 = Increment on low-to-high transition on T0CKI pin (Pin6)
T0CON (Timer0 Control Register)
• T0CS: This bit is used to select the proper clock source for Timer0.
• 1 = Transition on T0CKI pin (Pin6)
• 0 = Internal instruction cycle clock (CLKO)
• TMR0ON: This bit is set to high (1) to enable the Timer0 and set to low (0) to stop it.
INTCON (Interrupt Control Register)
• TMR0IF: This is Timer0 overflow flag bit. This bit is set when TMR0
register overflows. This bit cleared by the software.
TMR0 (Timer0 Register)
1. Select the Prescaler, Clock option, Mode of Timer0 with the T0CON register.
2. Fill the higher byte of Timer value in TMR0H and then fill lower byte value
in TMR0L register.
3. Set the TMR0ON bit to start the timer.
4. Wait until the TMR0IF flag gets high.
5. As TMR0IF gets high, set it to zero and stop the timer by clearing the
TMR0ON bit.
6. To start the Timer0 again repeat the process from step2.
Program
#include<p18f4550.h>
void T0_init(void);
void main()
{
TRISB=0; // COnfigure PortB as output Port.
LATB=0x01;
T0CON=0x07; // Prescaler= 1:256, 16-bit mode, Internal Clock
while(1)
{
T0_init(); // Initialize Timer0
LATB=(LATB<<1)|(LATB>>7); // Circular right shift at PortB
}
}
Program…
void T0_init()
{
TMR0H=0xD2; // Values calculated for 1 second delay with
12MHz crystal
TMR0L=0x39;
T0CONbits.TMR0ON=1; // Timer0 On
while(INTCONbits.TMR0IF==0); // Wait until TMR0IF gets flagged
T0CONbits.TMR0ON=0; // Timer0 Off
INTCONbits.TMR0IF=0; // Clear Timer0 interrupt flag
}