Microprocessor Ch9
Microprocessor Ch9
9 Timer Programming
OUTLINE
• Counter programming
• Timer programming in C
TIMERS
Timers
- 8051 has 2 built-in timers: timer 0 and timer 1
- They can be used to generate a specific time delay
(more accurate and easier than loops)
- Or, can be used as counters to count events happening outside the uC.
- Both timers are 16 bits wide.
• Timer registers
- The contents of each timer is stored in two 8-bit registers
- Timer 0: TH0 (higher byte), TL0 (lower byte)
- Timer 1: TH1 (higher byte), TL1 (lower byte)
Ex.
MOV TL0, #4FH
MOV R5, TH1
TIMERS: MODE
– M1, M0: the combination of the two bits specify the operation modes of the timer
• (M1,M0)= 00: mode 0; 01: mode 1; 10: mode 2; 11: mode 3.
– C/T (counter/timer)
• Whether the timer will be used as a delay generator (timer) or event counter
• C/T = 0: used as timer for time delay generation
• C/T = 1: event counter
– Gate
• Gate = 0: the timer is started or stopped by software
• Gate = 1: the timer is started or stopped by external hardware
TIMERS: CLOCK SOURCE
CLR P2.3
MOV TMOD, #01 ; 1. timer 0, mode 1 (16-bit)
HERE: MOV TL0, #3EH ; 2. load init value
MOV TH0, #0B8H
SETB P2.3 ; set P2.3 high
SETB TR0 ; 3. start timer
AGAIN: JNB TF0, AGAIN ; 4. monitor TF0 until timer overflows
CLR TR0 ; 5. stop timer
CLR TF0 ; 6. clear flag
CLR P2.3
• Counter programming
• Timer programming in C
COUNTER
Counter (C/T=1)
– Counting the events happening outside of 8051.
– Counter v.s. timer
• If C/T=0: timer mode, The clock source is XTAL/12
The value of TH0, TL0 increases by 1 for each clock
• If C/T = 1: counter mode. The clock source is an outside pulse to be counted
The value of TH0, TL0 increases by 1 for each outside pulse
We can use the counter to count the # of pulses
The remaining operations are the same for counter and timer (ex. mode 0, 1, 2)
– Connection
• External pulses are connected through P3.4 (T0) or P3.5 (T1)
COUNTER
• TCON register
– 8-bit register
– TR0, TF0, TR1, TF1
• SETB TR0 is the same as SETB TCON.4
– IE1, IT1, IE0, IT0 are used by interrupt (will be discussed in Ch. 11)
• Gate bit of TMOD
– Gate = 0
• Start timer: SETB TR0; stop timer: CLR TR0
– Gate = 1
• The start and stop of timer is done externally through P3.2 for timer 0 or
P3.3 for timer 1 via a simple switch (e.g. stop watch)
COUNTER:
• Ex: Design a simple stop watch that can count from 0 second to 59 seconds. Use a switch to control
the start and stop of the watch. When the value of the stop watch arrives at 60 sec, reset it to 0.
SW BIT P3.3
SETB SW ; input
MOV A, #0
MOV TMOD, #00001001 ; Gate = 1, C/T = 0, mode = 1 (16-bit counter)
START: ACALL DELAY1SEC
INC A
CJNE A, #60, DISPLAY
MOV A, #0 ; if A = 60, reset the display of the stop watch
DISPLAY: ACALL LCD
SJMP START
;--------- subroutine ----------------------------
DELAY1SEC: MOV R2, #200 ; 5 ms x 200 loops
AGAIN: MOV TH0, #0EEH
MOV TL0, #00H ; init = EE00, 5ms delay
HERE: JNB TF0, HERE
CLR TF0
DJNZ R2, AGAIN
RET
OUTLINE
. Counter programming
. Timer programming in C
PROGRAMMING IN C
• Ex: Write a program to toggle P1.5 every 250 ms. Use timer 0, mode 2 (8-bit auto reload)
#include <reg51.h>
void T0M2delay25us(void);
sbit mybit = P1^5;
void main(void) {
unsigned int x;
while(1){
mybit = ~mybit;
for (x = 0; x < 10000; x++)
T0M2delay25us();
}
}
void T0M2delay25us(void) {
TMOD = 0x02; // timer 0, mode 2
TH0 = -23; // count 23 times, then overflow. 23*1.085 = 25 us
TR0 = 1;
while (TF0 = = 0);
TR0 = 0;
TF0 =0;
}
PROGRAMMING IN C
• Ex: Assume a 1 Hz external clock is being fed into pin T0 (P3.4). Write a C program for
counter T0 in mode 1 (16-bit) to display TH0 and TL0 on P2 and P1, respectively.
#include <reg51.h>
void main( ) {
T0 = 1; //(make T0 an input)
TMOD = 0x05; // 0000 0101 (C/T = 1, mode 1)
TL0 = 0;
TH0 = 0; //clear counters
while(1) {
do {
TR0=1; //start timer
P1 = TL0;
P2 = TH0;
}while(TF0 = = 0);
TR0 = 0; //stop timer
TF0 = 0; //clear TF
}
}