MODULE 4_timers and counters_Lecture notes
MODULE 4_timers and counters_Lecture notes
Time reference
Creating delay
Wave form properties measurement
Periodic interrupt generation
Waveform generation
Timer in 8051 is used as timer, counter and baud rate generator. Timer always counts up
irrespective of whether it is used as timer, counter, or baud rate generator: Timer is always
incremented by the microcontroller. The time taken to count one digit up is based on master clock
frequency.
This indicates that one increment in count will take 1 micro second.
The two timers in 8051 share two SFRs (TMOD and TCON) which control the timers, and each timer
also has two SFRs dedicated solely to itself (TH0/TL0 and TH1/TL1).
TMOD Register
TCON Register
TIMER MODES
Timer Mode-0: In this mode, the timer is used as a 13-bit UP counter as follows.
Timer Mode-1: This mode is similar to mode-0 except for the fact that the Timer operates in 16-bit
mode.
Control bits TR1 and TF1 are used by Timer-0 (higher 8 bits) (TH0) in Mode-3 while TR0 and TF0
are available to Timer-0 lower 8 bits(TL0).
Usually, there is no need to constantly read timer registers. It is sufficient to register the
moment they are cleared, i.e. when counting starts from 0. This condition is called an
overflow. When it occurrs, the TF0 bit of the TCON register will be automatically set. The
state of this bit can be constantly checked from within the program or by enabling an
interrupt which will stop the main program execution when this bit is set. Suppose it is
necessary to provide a program delay of 0.05 seconds (50 000 machine cycles), i.e. time
when the program seems to be stopped:
When enabled, the timer will resume counting from this number. The state of the TF0 bit,
i.e. whether it is set, is checked from within the program. It happens at the moment of
overflow, i.e. after exactly 50.000 machine cycles or 0.05 seconds.
Similarly to the previous example, the answer to this question again lies in the TCON
register. This time it's about the C/T0 bit. If the bit is cleared the timer counts pulses
generated by the internal oscillator, i.e. measures the time passed. If the bit is set, the timer
input is provided with pulses from the P3.4 pin (T0). Since these pulses are not always of
the same width, the timer cannot be used for time measurement and is turned into a
counter, therefore. The highest frequency that could be measured by such a counter is 1/24
frequency of used quartz-crystal.
Timer 1
Timer 1 is identical to timer 0, except for mode 3 which is a hold-count mode. It means that
they have the same function, their operation is controlled by the same registers TMOD and
TCON and both of them can operate in one out of 4 different modes.
In order to program 8051 timers, it is important to know the calculation of initial count value to
be stored in the timer register. The calculations are as follows.
(65536D = FFFFH+1)
Convert the answer into hexadecimal and load onto THx and TLx register.
(256D = FFH+1)
Mode 1:
Load the TMOD value register indicating which timer (0 or 1) is to be used and
which timer mode is selected.
Start the timer by the instruction “SETB TR0” for timer 0 and “SETB TR1” for timer 1.
Keep monitoring the timer flag (TF) with the “JNB TFx,target” instruction to see if it
is raised. Get out of the loop when TF becomes high.
Stop the timer with the instructions “CLR TR0” or “CLR TR1”, for timer 0 and timer
1, respectively.
Clear the TF flag for the next round with the instruction “CLR TF0” or “CLR TF1”,
for timer 0 and timer 1, respectively.
Mode 0:
Mode 2:
Load the TMOD value register indicating which timer (0 or 1) is to be used; select
timer mode 2.
keep monitoring the timer flag (TFx) with the “JNB TFx,target” instruction to see if it
is raised. Get out of the loop when TFx goes high.
The period of the square wave is T = 1/(2 kHz) = 500 s. Each half pulse = 250 s.
The value n for 250 s is: 250 s /1 s = 250
2. Write a program segment that uses timer 1 in mode 2 to toggle P1.0 once whenever the
counter reaches a count of 100. Assume the timer clock is taken from external
SERIAL COMMUNICATION.
DATA COMMUNICATION
The 8051 microcontroller is parallel device that transfers eight bits of data simultaneously
over eight data lines to parallel I/O devices. Parallel data transfer over a long is very expensive.
Hence, a serial communication is widely used in long distance communication. In serial data
communication, 8-bit data is converted to serial bits using a parallel in serial out shift register and
then it is transmitted over a single data line. The data byte is always transmitted with least
significant bit first.
Communication Links
1. Simplex communication link: In simplex transmission, the line is dedicated for transmission.
The transmitter sends and the receiver receives the data.
Transmitter Receiver
Transmitter Receiver
Receiver Transmitter
3. Full duplex communication link: If the data is transmitted in both ways at the same time, it
is a full duplex i.e. transmission and reception can proceed simultaneously. This communication
link requires two wires for data, one for transmission and one for reception.
Transmitter Receiver
Receiver Transmitter
1. Synchronous serial data communication: In this transmitter and receiver are synchronized. It
uses a common clock to synchronize the receiver and the transmitter. First the synch character is
sent and then the data is transmitted. This format is generally used for high speed transmission. In
Synchronous serial data communication a block of data is transmitted at a time.
data
Clock
Data
Clock 1 Clock2
Baud rate:
The rate at which the data is transmitted is called baud or transfer rate. The baud rate is the
reciprocal of the time to send one bit. In asynchronous transmission, baud rate is not equal to
number of bits per second. This is because; each byte is preceded by a start bit and followed by
parity and stop bit. For example, in synchronous transmission, if data is transmitted with 9600
baud, it means that 9600 bits are transmitted in one second. For bit transmission time = 1
second/ 9600 = 0.104 ms.
1. SBUF Register: Serial Buffer (SBUF) register is an 8-bit register. It has separate SBUF
registers for data transmission and for data reception. For a byte of data to be transferred
via the TXD line, it must be placed in SBUF register. Similarly, SBUF holds the 8-bit data
received by the RXD pin and read to accept the received data.
2. SCON register: The contents of the Serial Control (SCON) register are shown below. This
register contains mode selection bits, serial port interrupt bit (TI and RI) and also the
ninth data bit for transmission and reception (TB8 and RB8).
1.Mode 0
In this mode serial port runs in synchronous mode. The data is transmitted and received
through RXD pin and TXD is used for clock output. In this mode the baud rate is 1/12 of
clock frequency.
2.Mode 1
In this mode SBUF becomes a 10 bit full duplex transceiver. The ten bits are 1 start bit, 8
data bit and 1 stop bit. The interrupt flag TI/RI will be set once transmission or reception is
over. In this mode the baud rate is variable and is determined by the timer 1 overflow rate.
Baud rate = [2smod/32] x Timer 1 overflow Rate
3. Mode 2
This is similar to mode 1 except 11 bits are transmitted or received. The 11 bits are, 1 start
bit, 8 data bit, a programmable 9th data bit, 1 stop bit.
4. Mode 3
CONNECTIONS TO RS-232
RS-232 standards:
In RS232, a logic one (1) is represented by -3 to -25V and referred as MARK while logic zero
(0) is represented by +3 to +25V and referred as SPACE. For this reason to connect any RS232 to a
microcontroller system we must use voltage converters such as MAX232 to convert the TTL logic
level to RS232 voltage levels and vice-versa. MAX232 IC chips are commonly referred as line
drivers.
In RS232 standard we use two types of connectors. DB9 connector or DB25 connector.
The 8051 has two pins that are used specifically for transferring and receiving data serially. These
two pins are called TXD, RXD. Pin 11 of the 8051 (P3.1) assigned to TXD and pin 10 (P3.0) is
designated as RXD. These pins TTL compatible; therefore they require line driver (MAX 232) to
make them RS232 compatible. MAX 232 converts RS232 voltage levels to TTL voltage levels and
vice versa. One advantage of the MAX232 is that it uses a +5V power source which is the same as
the source voltage for the 8051. The typical connection diagram between MAX 232 and 8051 is
shown below.
2. The TH1 is loaded with one of the values in table 5.1 to set the baud rate for serial
data transfer.
3. The SCON register is loaded with the value 50H, indicating serial mode 1, where an
8-bit data is framed with start and stop bits.
6. The character byte to be transferred serially is written into the SBUF register.
7. The TI flag bit is monitored with the use of the instruction JNB TI, target to see if the
character has been transferred completely.
9.
Example 1. Write a program for the 8051 to transfer letter ‘A’ serially at 4800- baud rate, 8 bit data,
1 stop bit continuously.
ORG 0000H
LJMP START
ORG 0030H
SJMP AGAIN
END
ORG 0000H
LJMP START
ORG 0030H
MOV TH1, #0FDH ; load count to get reqd. baud rate of 9600
RET
END
BASICS OF INTERRUPTS.
During program execution if peripheral devices needs service from microcontroller, device will
generate interrupt and gets the service from microcontroller. When peripheral device activate the
interrupt signal, the processor branches to a program called interrupt service routine. After
executing the interrupt service routine the processor returns to the main program.
ISR will always ends with RETI instruction. The execution of RETI instruction results in the
following.
External interrupts are those initiated by peripheral devices through the external pins of
the microcontroller.
Internal interrupts are those activated by the internal peripherals of the microcontroller
like timers, serial controller etc.)
The category of interrupts which can be disabled by the processor using program is called
maskable interrupts.
Starting address of the ISR is called interrupt vector. In vectored interrupts the starting
address is predefined. In non-vectored interrputs, the starting address is provided by the
peripheral as follows.
INTERRUPT STRUCTURE.
8051 has five interrupts. They are maskable and vectored interrupts. Out of these five, two are
external interrupt and three are internal interrupts.
1. IE Register
This is an 8 bit register used for enabling or disabling the interrupts. The structure of IE
register is shown below.
This is an 8 bit register used for setting the priority of the interrupts.