0% found this document useful (0 votes)
3 views

MODULE 4_timers and counters_Lecture notes

This document provides an overview of timers and counters in the 8051 microcontroller, detailing their functions, modes of operation, and programming techniques. It explains the different timer modes (0-3), their configurations, and how to measure pulse duration and count pulses. Additionally, it covers serial communication fundamentals, including types of communication links and modes of serial data transmission supported by the 8051.

Uploaded by

Prajwal praju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

MODULE 4_timers and counters_Lecture notes

This document provides an overview of timers and counters in the 8051 microcontroller, detailing their functions, modes of operation, and programming techniques. It explains the different timer modes (0-3), their configurations, and how to measure pulse duration and count pulses. Additionally, it covers serial communication fundamentals, including types of communication links and modes of serial data transmission supported by the 8051.

Uploaded by

Prajwal praju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

MODULE 4

TIMERS AND COUNTERS

Timers/Counters are used generally for

 Time reference
 Creating delay
 Wave form properties measurement
 Periodic interrupt generation
 Waveform generation

8051 has two timers, Timer 0 and Timer 1.

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.

If Master CLK=12 MHz,

Timer Clock frequency = Master CLK/12 = 1 MHz

Timer Clock Period = 1micro second

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).

The following are timer related SFRs in 8051.

Acharya Institute Of Technology Page 1


SFR
SFR Name Description Address

TH0 Timer 0 High Byte 8Ch

TL0 Timer 0 Low Byte 8Ah

TH1 Timer 1 High Byte 8Dh

TL1 Timer 1 Low Byte 8Bh

TCON Timer Control 88h

TMOD Timer Mode 89h

TMOD Register

TCON Register

Acharya Institute Of Technology Page 2


Timer/ Counter Control Logic

TIMER MODES

Timers can operate in four different modes. They are as follows

Timer Mode-0: In this mode, the timer is used as a 13-bit UP counter as follows.

Fig. Operation of Timer on Mode-0

Acharya Institute Of Technology Page 3


The lower 5 bits of TLX and 8 bits of THX are used for the 13 bit count.Upper 3 bits of TLX are
ignored. When the counter rolls over from all 0's to all 1's, TFX flag is set and an interrupt is
generated. The input pulse is obtained from the previous stage. If TR1/0 bit is 1 and Gate bit is 0,
the counter continues counting up. If TR1/0 bit is 1 and Gate bit is 1, then the operation of the
counter is controlled by input. This mode is useful to measure the width of a given pulse fed to
input.

Timer Mode-1: This mode is similar to mode-0 except for the fact that the Timer operates in 16-bit
mode.

Fig: Operation of Timer in Mode 1

Acharya Institute Of Technology Page 4


Timer Mode-2: (Auto-Reload Mode): This is a 8 bit counter/timer operation. Counting is
performed in TLX while THX stores a constant value. In this mode when the timer overflows i.e. TLX
becomes FFH, it is fed with the value stored in THX. For example if we load THX with 50H then th e
timer in mode 2 will count from 50H to FFH. After that 50H is again reloaded. This mode is useful in
applications like fixed time sampling.

Fig: Operation of Timer in Mode 2

Acharya Institute Of Technology Page 5


Timer Mode-3: Timer 1 in mode-3 simply holds its count. The effect is same as setting TR1=0.
Timer0 in mode-3 establishes TL0 and TH0 as two separate counters.

Fig: Operation of Timer in Mode 3

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).

Acharya Institute Of Technology Page 6


Timer 0 Overflow Detection

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:

First a number to be written to the timer registers should be calculated:

Acharya Institute Of Technology Page 7


Then it should be written to the timer registers TH0 and TL0:

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.

How to measure pulse duration?

Acharya Institute Of Technology Page 8


Suppose it is necessary to measure the duration of an operation, for example how long a
device has been turned on? Look again at the figure illustrating the timer and pay attention
to the function of the GATE0 bit of the TMOD register. If it is cleared then the state of the
P3.2 pin doesn't affect timer operation. If GATE0 = 1 the timer will operate until the pin
P3.2 is cleared. Accordingly, if this pin is supplied with 5V through some external switch at
the moment the device is being turned on, the timer will measure duration of its operation,
which actually was the objective.

How to count up pulses?

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.

Acharya Institute Of Technology Page 9


PROGRAMMING 8051 TIMERS IN ASSEMBLY

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.

In any mode, Timer Clock period = 1/Timer Clock Frequency.

= 1/(Master Clock Frequency/12)

a. Mode 1 (16 bit timer/counter)

Value to be loaded in decimal = 65536 – (Delay Required/Timer clock period)


Convert the answer into hexadecimal and load onto THx and TLx register.

(65536D = FFFFH+1)

b. Mode 0 (13 bit timer/counter)

Value to be loaded in decimal = 8192 – (Delay Required/Timer clock period)

Convert the answer into hexadecimal and load onto THx and TLx register.

Acharya Institute Of Technology Page 10


(8192D = 1FFFH+1)

c. Mode 2 (8 bit auto reload)

Value to be loaded in decimal = 256 – (Delay Required/Timer clock period)


Convert the answer into hexadecimal and load onto THx register. Upon starting the

timer this value from THx will be reloaded to TLx register.

(256D = FFH+1)

Steps for programming timers in 8051

Mode 1:

 Load the TMOD value register indicating which timer (0 or 1) is to be used and
which timer mode is selected.

 Load registers TL and TH with initial count values.

 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.

 Go back to step 2 to load TH and TL again.

Mode 0:

The programming techniques mentioned here are also applicable to counter/timer


mode 0. The only difference is in the number of bits of the initialization value.

Mode 2:

 Load the TMOD value register indicating which timer (0 or 1) is to be used; select
timer mode 2.

Acharya Institute Of Technology Page 11


 Load TH register with the initial count value. As it is an 8-bit timer, the valid range
is from 00 to FFH.

 Start the timer.

 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.

 Clear the TFx flag.

 Go back to step 4, since mode 2 is auto-reload.

1. Write a program to continuously generate a square wave of 2 kHz frequency on pin


P1.5 using timer 1. Assume the crystal oscillator frequency to be 12 MHz.

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

65536 - 250 = FF06H. TL


= 06H and TH = 0FFH.

MOV TMOD,#10 ;Timer 1, mode 1

AGAIN: MOV TL1,#06H ;TL0 = 06H

MOV TH1,#0FFH ;TH0 = FFH

SETB TR1 ;Start timer 1

BACK: JNB TF1,BACK ;Stay until timer rolls over

CLR TR1 ;Stop timer 1

CPL P1.5 ;Complement P1.5 to get Hi, Lo

CLR TF1 ;Clear timer flag 1

SJMP AGAIN ;Reload timer

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

source P3.5 (T1).

Acharya Institute Of Technology Page 12


The TMOD value is 60H

The initialization value to be loaded into TH1 is


256 - 100 = 156 = 9CH

MOV TMOD,#60h ;Counter1, mode 2, C/T’= 1

MOV TH1,#9Ch ;Counting 100 pulses

SETB P3.5 ;Make T1 input

SETB TR1 ;Start timer 1

BACK: JNB TF1,BACK ;Keep doing it if TF = 0

CPL P1.0 ;Toggle port bit

CLR TF1 ;Clear timer overflow flag

SJMP BACK ;Keep doing it

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.

. BASICS OF SERIAL DATA COMMUNICATION,

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

Acharya Institute Of Technology Page 13


2. Half duplex communication link: In half duplex, the communication link can be used for
either transmission or

reception. Data is transmitted in only one direction at a time.

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

Types of Serial communication:

Serial data communication uses two types of communication.

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.

Transmitter Sync Receiver

data

Clock

Acharya Institute Of Technology Page 14


2. Asynchronous Serial data transmission: In this, different clock sources are used for
transmitter and receiver. In this mode, data is transmitted with start and stop bits. A transmission
begins with start bit, followed by data and then stop bit. For error checking purpose parity bit is
included just prior to stop bit. In Asynchronous serial data communication a single byte is
transmitted at a time.

Transmitter Start D0 D1 D2 D3 D4 D5 D6 D7 D8 Stop Receiver

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.

8051 SERIAL COMMUNICATION

The 8051 supports a full duplex serial port.

Three special function registers support serial communication.

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).

Acharya Institute Of Technology Page 15


3. PCON register: The SMOD bit (bit 7) of PCON register controls the baud rate in
asynchronous mode transmission.

Acharya Institute Of Technology Page 16


.

SERIAL COMMUNICATION MODES

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

= [2smod/32] x [Oscillator Clock Frequency] / [12 x [256 – [TH1]]]

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.

Acharya Institute Of Technology Page 17


smod
Baud rate = [2 /64] x Oscillator Clock Frequency

4. Mode 3

This is similar to mode 2 except baud rate is calculated as in mode 1

CONNECTIONS TO RS-232

RS-232 standards:

To allow compatibility among data communication equipment made by various


manufactures, an interfacing standard called RS232 was set by the Electronics Industries
Association (EIA) in 1960. Since the standard was set long before the advent of logic family, its
input and output voltage levels are not TTL compatible.

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.

DB9 Male Connector DB25 Male Connector

The pin description of DB9 and DB25 Connectors are as follows

Acharya Institute Of Technology Page 18


The 8051 connection to MAX232 is as follows.

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.

. SERIAL COMMUNICATION PROGRAMMING IN ASSEMBLY AND C.

Steps to programming the 8051 to transfer data serially

Acharya Institute Of Technology Page 19


1. The TMOD register is loaded with the value 20H, indicating the use of the Timer 1 in
mode 2 (8-bit auto reload) to set the baud rate.

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.

4. TR1 is set to 1 start timer 1.

5. TI is cleared by the “CLR TI” instruction.

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.

8. To transfer the next character, go to step 5.

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

START: MOV TMOD, #20H ; select timer 1 mode 2

MOV TH1, #0FAH ; load count to get baud rate of 4800

MOV SCON, #50H ; initialize UART in mode 2

; 8 bit data and 1 stop bit

SETB TR1 ; start timer

AGAIN: MOV SBUF, #'A' ; load char ‘A’ in SBUF

BACK: JNB TI, BACK ; Check for transmit interrupt flag

CLR TI ; Clear transmit interrupt flag

SJMP AGAIN

END

Acharya Institute Of Technology Page 20


Example 2. Write a program for the 8051 to transfer the message ‘EARTH’ serially at 9600 baud, 8
bit data, 1 stop bit continuously.

ORG 0000H

LJMP START

ORG 0030H

START: MOV TMOD, #20H ; select timer 1 mode 2

MOV TH1, #0FDH ; load count to get reqd. baud rate of 9600

MOV SCON, #50H ; initialise uart in mode 2

; 8 bit data and 1 stop bit

SETB TR1 ; start timer

LOOP: MOV A, #'E' ; load 1st letter ‘E’ in a

ACALL LOAD ; call load subroutine

MOV A, #'A' ; load 2nd letter ‘A’ in a

ACALL LOAD ; call load subroutine

MOV A, #'R' ; load 3rd letter ‘R’ in a

ACALL LOAD ; call load subroutine

MOV A, #'T' ; load 4th letter ‘T’ in a

ACALL LOAD ; call load subroutine

MOV A, #'H' ; load 4th letter ‘H’ in a

ACALL LOAD ; call load subroutine

SJMP LOOP ; repeat steps

Acharya Institute Of Technology Page 21


LOAD: MOV SBUF, A

HERE: JNB TI, HERE ; Check for transmit interrupt flag

CLR TI ; Clear transmit interrupt flag

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.

Steps taken by processor while processing an interrupt:

1. It completes the execution of the current instruction.

2. PSW is pushed to stack.

3. PC content is pushed to stack.

4. Interrupt flag is reset.

5. PC is loaded with ISR address.

ISR will always ends with RETI instruction. The execution of RETI instruction results in the
following.

1. POP the current stack top to the PC.

2. POP the current stack top to PSW.


Classification of interrupts.

1. External and internal interrupts.

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.)

2. Maskable and non-maskable interrupts.

The category of interrupts which can be disabled by the processor using program is called
maskable interrupts.

Acharya Institute Of Technology Page 22


Non-maskable interrupts are those category by which the programmer cannot disable it
using program.

3. Vectored and non-vectored interrupt.

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.

 Microcontroller receives an interrupt request from external device.

 Controller sends an acknowledgement (INTA) after completing the execution of


current instruction.

 The peripheral device sends the interrupt vector to the microcontroller.

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.

Interrupt source Type Vector address Priority

External interrupt 0 External 0003 Highest

Timer 0 interrupt Internal 000B

External interrupt 1 External 0013

Timer 1 interrupt Internal 001B

Serial interrupt Internal 0023 Lowest

8051 makes use of two registers to deal with 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.

Acharya Institute Of Technology Page 23


2. IP Register.

This is an 8 bit register used for setting the priority of the interrupts.

Acharya Institute Of Technology Page 24

You might also like