Anda di halaman 1dari 16

Embedded Systems TAB4333

Chapter 8 Timer and Counter

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

Timer 0 Clock Select


To use the RA4 pin as a clock input to Timer 0: bsf T0CON, T0CS To use the internal clock we could write: bcf T0CON, T0CS

Timer 0 Prescaler Use/Bypass


To get a resolution of one clock cycle, bypass the prescaler. To get a longer timer duration (at less resolution) use the prescaler. The PSA bit = 0 means to use prescaler ; and = 1 is to bypass prescaler. bsf T0CON, PSA ;bypass prescaler

TAB4333

Timer 0 Full Setup


To enable Timer 0 in 16-bit mode using the internal clock and bypass the prescaler we could write: movlw B10001000 movwf T0CON ;init timer0
TMR0ON = 1, T08BIT = 0, T0CS = 0, PSA = 1

TAB4333

INTCON and TMR0IF


Timers are all count-up. By default 8-bit: 00 to FF ; 16-bit: 0000 to FFFF. A value can be loaded into the timer to initiate the count not starting from 00 or 0000. [what is the impact?] TMR0H must be loaded before the TMR0L. Reset (overflow) when FF or FFFF is reached and goes into 00 or 0000 again. With overflow the TMR0IF (Timer0 Interrupt Flag) in INTCON register will be set indicating the count or timing has completed 1 cycle. [ref: INTCON register] TMR0H and TMR0L need to be reloaded after TMR0IF is set to repeat the loop.
TAB4333 7

Steps to program 16-bit Timer0


1) 2) 3) 4) 5) 6) 7) Initialize through T0CON register. Load initial count values into: TMR0H then TMR0L Start the timer [how?] Check TMR0IF flag: if not set then keep checking the flag else [how?] Stop the timer Clear TMR0IF flag Go back to step 2.

TAB4333

Example Squarewave generator


RB2

Xtal 20MHz

OSC1

5KHz

-16-bit timer -Internal clock -Increment on L-H trigger


Determine: TMR0H & TMR0L value Prescaler needed? Init word
TAB4333 9

Example Squarewave generator


FSQW = 5KHz ; CSQW = 1/FSQW = 0.2ms THSQW = TLSQW = 0.1ms FT0 = Fosc/4 = 5MHz ; CT0 = 1/FT0 = 0.2us So # of count for THSQW = THSQW/CT0 = 500 = 0x1F4 Thus starting value of the timer = FFFF 01F4 + 1 = FE0C [since 1F4 < FFFF so no need prescaler] Hence TMR0H = 0xFE and TMR0L = 0x0C Init word = 0001000 = 0x08

Pop Quiz Submit a program fragment to generate this squarewave.


TAB4333 10

Finding values for TMR0H and TMR0L


Assume: Crystal frequency = Q Hz No prescaler used Desired time delay = D sec 1. 2. 3. 4. 5. Timer freq, FT = Q/4 Hz Timer clock, CT = 1/FT sec. Number of counts in decimal, C10 = D/CT Timer value in decimal, TV = 65536 C10 + 1 Convert TV into hex value in the form yyxx. Load TMR0H with yy and TMR0L with xx

Note: For longer time C10 might be larger than 65536. In this case prescaler is needed.
TAB4333 11

Delay Time & Timer Values


if you want the delay in seconds: 65536 TMR_value * prescaler (Fosc / 4)

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

What is the time delay generated if the crystal used is 10 MHz?


MOVLW MOVWF MOVLW MOVWF MOVLW MOVWF BCF CALL BRA BSF BTFSS BRA BCF RETURN 0X08 ;16-bit, int clock, no prescaler T0CON 0X76 TMR0H 0X34 TMR0L INTC0N,TMR0IF DELAY HERE T0CON,TMR0ON INTCON,TMR0IF AGAIN T0CON,TMR0ON

HERE

DELAY AGAIN

TAB4333

13

Other requirement:
Fp prescaler ( N)

Fosc

synchronizer

TMR0H (8-bit)

TMR0L (8-bit)

20MHz

N? internal clock ( F Hz) Freq here, Fp < internal clock freq, F

presettable & readable Timer/counters TMR0IF flag set on overflow What is timer value for 20ms rollover?

Note: Max Fosc is 40MHz for 18F452

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

Counter Programming example


Assuming a clock pulse is fed into T0CKI to activate an 8-bit counter at Counter0(TIMER0). Display the TMR0L contents on Port B. BSF CLRF MOVLW MOVWF HERE MOVLW MOVWF BCF BSF AGAIN MOVFF BTFSS BRA BCF GOTO TRISA,RA4 TRISB 0x68 T0CON 0x00 TMR0L INTCON,TMR0IF T0CON,TMR0ON TMR0L,PORTB INTCON,TMR0IF AGAIN T0CON,TMR0ON HERE
RB7

:
RB0 RA4

TAB4333

16

Anda mungkin juga menyukai