TAB4333
General Features
Timers are used as delay timing generation or used as counter to count events happening outside the PIC. PIC18F452 has four 16-bit timers: Timer 0, Timer 1, Timer 2, and Timer 3. All counters are count-up type. Timers 1-3 can be paired capture / compare / pulse-width modulation (CCP) circuitry for external time control and measurement. Timer 0 is not paired with CCP, so it is usually used for internal Looptime timing i.e. delay loop application. Timer 0 can be clocked by either the internal clock (Fosc/4) or by external clock input via pin RA4 (pin 4 of port A) The RA4 pin is also labeled T0CKI to show that it can be used as Timer 0 clock input.
TAB4333
Timer 0 Features
TMR0ON
Read TMROH
TAB4333
Timer 0 On/Off
To turn Timer 0 on, we could use: bsf T0CON,TMR0ON or bsf 0xFD5,7 [ref: header file]
Timer 0 8-bit/16-bit
Timer 0 can be used as an 8-bit or 16-bit timer. To set Timer 0 to 16-bit mode we could use: bcf T0CON, T08BIT
TAB4333
TAB4333
TAB4333
TAB4333
Xtal 20MHz
OSC1
5KHz
Note: For longer time C10 might be larger than 65536. In this case prescaler is needed.
TAB4333 11
Delay =
Or
if you want the value to put in TMRH & TMR0L to get a determinate DELAY :
TMR_value = 65536
DELAY * Fosc
4* Prescaler
TAB4333
12
HERE
DELAY AGAIN
TAB4333
13
Other requirement:
Fp prescaler ( N)
Fosc
synchronizer
TMR0H (8-bit)
TMR0L (8-bit)
20MHz
presettable & readable Timer/counters TMR0IF flag set on overflow What is timer value for 20ms rollover?
TAB4333
14
Counter Programming
As a timer the crystal of PIC is used as the source of the clock frequency to drive the timer. As counter clock pulses outside the PIC increments the TMR0H and TMR0L. Usage of T0CON, TMR0H & TMR0L registers are similar to timers. External clock input could be via T0CKI(RA4) for Timer0 or RC0/T1OSO/T1CKI(RC0) for Timer1. Related registers for these are T0CON or T1CON. Counter counts up upon arrival of each input clock pulse.
TAB4333
15
:
RB0 RA4
TAB4333
16