Module - 4 Notes
Module - 4 Notes
Timer programming:
The 8051 has two timers /counters, they can be used either as
Timers to generate a time delay or as
Event counters to count events happening outside the microcontroller
TMOD Register:
Both timers 0 and 1 use the same register, called TMOD (timer mode), to set
the various timer operation modes
TMOD is an 8-bit register
The lower 4 bits are for Timer 0, the upper 4 bits are for Timer 1
In each case, the lower 2 bits are used to set the timer mode, the upper 2 bits
to specify the operation.
Example:
Find the timer’s clock frequency and its period for various 8051-based system, with
the crystal frequency 11.0592 MHz and 12 MHz when C/T bit of TMOD is 0.
Solution:
crystal frequency=11.0592 MHz,
Timer Clock frequency = crystal frequency /12 = 921.6 KHz
Timer Clock Period = 1 / Timer Clock frequency = 1.085 us
This indicates that one increment in count will take 1.085 microsecond.
Similarly,
If crystal frequency =12MHz,
Timer Clock frequency = crystal frequency /12 = 1MHz
Timer Clock Period = 1 / Timer Clock frequency = 1us
This indicates that one increment in count will take 1 microsecond
Explain the steps to program timer 0 or timer 1 in mode 1:
Example Programs:
Assume that XTAL = 11.0592 MHz, write a program to generate a square wave of
2kHz frequency on pin P1.5. Use timer 0 in mode 1.
Solution:
(a) T = 1 / f = 1 / 2 kHz = 500 us the period of square wave.
(b) 1 / 2 of it for the high and low portion of the pulse is 250 us.
(c) 250 us / 1.085 us = 230 and 65536 – 230 = 65306 which in hex
is FF1AH.
(d) TL = 1A and TH = FF, all in hex. The program is as follow.
Assume XTAL = 11.0592 MHz, write a program to generate a square wave of 50 kHz
frequency on pin P2.3.
Solution:
Look at the following steps.
(a) T = 1 / 50 = 20 ms, the period of square wave.
(b) 1 / 2 of it for the high and low portion of the pulse is 10 ms.
(c) 10 ms / 1.085 us = 9216 and 65536 – 9216 = 56320 in decimal, and in hex it is
DC00H.
(d) TL = 00H and TH = DCH (hex).
Example Program:
Assume XTAL = 11.0592 MHz, find the frequency of the square wave generated on
pin P1.0 in the following program.
MOV TMOD, #20H ; Timer1/8-bit/auto reload
MOV TH1, #5 ; TH1 = 5
SETB TR1 ; start the timer 1
BACK: JNB TF1, BACK ; till timer rolls over i.e., TF1=1
CPL P1.0 ; P1.0 to high to low pulse.
CLR TF1 ; clear Timer 1 flag
SJMP BACK ; mode 2 is auto-reload
Solution:
First notice the target address of SJMP. In mode 2 we do not need to reload TH since
it is auto-reloading.
Now (256 - 05) × 1.085 us = 251 × 1.085 us = 272.33 us is the high portion of the
pulse. Since it is a 50% duty cycle square wave, the period T is twice that; as a result,
T = 2 × 272.33 us = 544.67 us and the frequency = 1.83597 kHz.
Counter Programming:
Timers can also be used as counters counting events happening outside the 8051.
when it is used as a counter, it is a pulse outside of the 8051 that increments the TH,
TL registers.
TMOD and TH, TL registers are the same as for the timer discussed previously
Programming the timer in the last section also applies to programming it as a counter
except the source of the frequency.
The C/T bit in the TMOD registers decides the source of the clock for the timer.
When C/T = 1, the timer is used as a counter and gets its pulses from outside the 8051.
The counter counts up as pulses are fed from pins 14 and 15 of microcontroller, these
pins are called T0 (timer 0 input) and T1 (timer 1 input).
Example Program.
Assuming that clock pulses are fed into pin T1, write a program for counter 1 in mode
2 to count the pulses and display the state of the TL1 count on P2, which is connected
to 8 LEDs.
Solution: MOV TM0D, #01100000B ; counter 1 mode 2, C/T=1external pulses
MOV TH1, #0 ; clear TH1
SETB P3.5 ; make T1 input
AGAIN: SETB TR1 ; start the counter 1
BACK: MOV A, TL1 ; get copy of TL
MOV P2, A ; display it on port 2
JNB TF1, Back ; keep doing, if TF = 0
CLR TR1 ; stop the counter 1
CLR TF1 ; make TF=0
SJMP AGAIN ; keep doing it
Notice in the above program the role of the instruction SETB P3.5. we make
P3.5 an input port by making it high. In other words, we must configure (set high) the
T1 pin (pin P3.5) to allow pulses to be fed into it.
Programming for Serial Data
Transmission
SBUF Register
SCON Register
#include<reg51.h>
void main(void)
{
TMOD=0X20; // Configure timer 1 in mode 2
TH1=0XFD; //9600bpS
SCON=0X50; // serial mode 1
TR1=1; // start the timer 1
while(1) // repeat continuously
{
SBUF='Y'; //load ASCII value of ‘Y’ in SBUF
while(TI==0); // loop as long as TI=0
TI=0; // Clear TI
}
}
Write a 8051 C program to transfer letter ‘YES’ serially at 4800 baud rate continuosly.
#include<reg51.h>
void main(void)
{
TMOD=0X20; // TIMER 1 IN MODE 2
TH1=0XFD; //9600 BAUD RATE
SCON=0X50;
TR1=1;
while(1)
{
SBUF='Y';
while(TI==0);
TI=0;
SBUF='E';
while(TI==0);
TI=0;
SBUF='S';
while(TI==0);
TI=0;
}
}