9 - Timer Programming
9 - Timer Programming
9 - Timer Programming
PROGRAMMING
It can be used for the generation of a Time Delay and for that
The 16-bit register of Timer0 is accessed as low byte and high byte.
The low-byte register is called TMR0L (Timer0 low byte) and the high-byte
byte of Timer0.
These registers can also be read like any other register.
"MOVFF TMR0L, PORTB copies TMR0L (low byte of Timer0) to PORTB.
Each timer has a control register, called TnCON (n=0,1,2), to set the corresponding
timers operation modes. T0CON is an 8-bit register used for control of Timer0.
case, the timers are often used for time delay generation.
If T0CS = 1, the clock source is external and comes from the
16-bit
It
flag,
in
order to repeat the process, the registers TMR0H
and TMR0L must be reloaded with the original value,
and the TMR0IF flag must be reset to 0 for the next
round.
Load the value into the T0CON register indicating which mode
(8-bit or 16- bit) is to be used and the selected pre-scaler option.
BCF
TRISB,5
MOVLW 0x08
MOVWF T0CON
MOVWF TMR0H
MOVLW 0xF2
MOVWF TMR0L
BCF
INTCON,
BTG
PORTB, 5
BSF
T0CON,
INTCON,
TMR0IF
AGAIN BTFSS
BRA
AGAIN
BCF
T0CON,
BRA
HERE
TMR0IF
TOCON is loaded.
FFF2H is loaded into TMR0H- TMRO0L.
The Timer0 interrupt flag is cleared by the "BCF INTCON,
TMR0IF" instruction.
PORTB.5 is toggled for the high and low portions of the pulse.
Timer0 is started by the "BSF T0CON, TMR0ON" instruction.
Timer0 counts up with the passing of each clock, which is
provided by the crystal oscillator. As the timer counts up, it
goes through the states of FFF3, FFF4, FFF5, FFF6, FFF7,
FFF8, FFF9, FFFA, FFFB, and so on until it reaches FFFFH.
One more clock rolls it to 0000, raising the Timer0 flag
(TMR0IF = 1).
At that point, the "BTFSS INTCON, TMR0IF" instruction
bypasses the "BRA AGAIN" instruction.
Timer0 is stopped by the instruction "BCF T0CON, TMR0ON",
and the process is repeated.
BCF
TRISB,5
MOVLW 0x08
MOVWF T0CON
MOVWF TMR0H
MOVLW 0x34
MOVWF TMR0L
BCF
INTCON,
TMR0IF
CALL
DELAY
BTG
PORTB, 5
;toggle PB5
BRA
HERE
DELAY BSF
T0CON
AGAIN BTFSS
INTCON
TMR0IF
BRA
AGAIN
BCF
T0CON,
RETURN
(a) the crystal frequency, and (b) the timer's (16-bit ) register
size.
Both of these factors are beyond the control of the PIC18
programmer.
The largest time delay is achieved by making both TMR0H and
mode with a prescaler of 64 and use internal clock (Fosc/4) for the
clock source, positive-edge.
Find the timer's clock frequency and its period for various PICl8based systems, with the following crystal frequencies. Assume that a
prescaler of 1 :64 is used.
( a) 1 0 MH z
1/4 x 10 MHz = 2.5 MHz and 1/64 x 2.5 MHz = 39062.5 Hz due to
1 :64 prescaler and T = 1/39062.5Hz = 25.6 us
(b) 1 6 MH z
1/4 x 16 MHz = 4 MHz and 1/64 x 4 MHz = 62500 Hz due to
prescaler and T = 1/62500 Hz = 16 us
BCF
TRISA,2
MOVLW 0x48
MOVWF T0CON
MOVWF TMR0L
BCF
INTCON,
TMR0IF
CALL
DELAY
BTG
PORTA, 2
;toggle PA2
BRA
HERE
DELAY BSF
T0CON
AGAIN BTFSS
INTCON
TMR0IF
BRA
AGAIN
BCF
T0CON,
RETURN
be set.
factors
T1CON
COUNTER PROGRAMMING
We were using the timers of the PIC18 to generate time delays.
These timers can also be used as counters to count events
happening outside the PICI8.
When the timer is used as a timer, the PICI8's crystal is used as
the source of the frequency.
When it is used as a counter, however, it is a pulse outside the
PIC 18 that increments the TH, TL registers.
These clock pulses could represent the number of people
passing through an entrance, or the number of wheel rotations,
or any other event that can be converted to pulses.
In counter mode, the registers such as T0CON, TMR0H, and
TMR0L are the same as for the timer .
The T0CS bit (Timer0 clock source) in the T0CON register decides the source of
PICI8.
When T0CS = 1, the counter counts up as pulses are fed from pin RA4 (PORTA4).
The pin is called T0CKI (Timer0 clock input
In the case of Timer0, when T0CS = 1, pin RA4 (PORTA4) provides the clock
pulse and the counter counts up for each clock pulse coming from that pin.
Similarly, for Timerl , when TMR1CS = 1, each clock pulse coming in from pin
Assuming that clock pulses are fed into pin T0CKI, write a program for counter 0 in
8- bit mode to count the pulses and display the state of the TMROL count on PORTB.
BSF
TRISA,4
CLRF
TRISB
MOVLW 0x68
MOVWF T0CON
;TMR0L = 0H,
MOVWF TMR0L
;Clear Timer0L .
BCF
INTCON,
TMR0IF
BSF
T0CON
INTCON
BRA
AGAIN
BCF
T0CON,
GOTO
HERE
PORTB
TMR0IF
Timer 3.
T3CON (Timer 3 Control Register can be used to