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

Lecture 12 8051 Timer Programing v2

This document discusses timers and counters in microcontrollers, specifically the 8051 microcontroller. It provides information on: - The 8051 has two 16-bit timers, Timer 0 and Timer 1, that can be used as timers to generate delays or as counters. - The timers have registers to store their values (TL0, TH0, TL1, TH1) and modes are set using the TMOD register. - As timers, they can be configured to count external events or generate time delays by overflowing their registers at max value. Prescalars allow dividing the clock to extend the timer range.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Lecture 12 8051 Timer Programing v2

This document discusses timers and counters in microcontrollers, specifically the 8051 microcontroller. It provides information on: - The 8051 has two 16-bit timers, Timer 0 and Timer 1, that can be used as timers to generate delays or as counters. - The timers have registers to store their values (TL0, TH0, TL1, TH1) and modes are set using the TMOD register. - As timers, they can be configured to count external events or generate time delays by overflowing their registers at max value. Prescalars allow dividing the clock to extend the timer range.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Microprocessor Interfacing

and Embedded System

Course Number: CSE331/EEE332/EEE453 Section 8 & 9


Prerequisites: CSE 332, Computer Organization & Architecture
 
Dr. Dihan Md. Nurudddin Hasan (dmh)

Room Number: SAC 1046B

Office Number: TBA, Ext: TBA

Email: [email protected]

Office Hours: Please click the appointment calendar (strictly by online


appointment)
https://www.tutorialspoint.com/embedded_sy
stems/es_io_programming.htm

8051 IO description
In 8051, I/O operations are done using four
ports and 40 pins. The following pin diagram
shows the details of the 40 pins. I/O operation−
port reserves 32 pins where each port has 8
pins. The other 8 pins are designated as Vcc,
GND, XTAL1, XTAL2, RST, EA (bar), ALE/PROG
(bar), and PSEN (bar).

The four ports P0, P1, P2, and P3, each


use 8 pins, making them 8-bit ports. Upon
RESET, all the ports are configured as
inputs, ready to be used as input ports.
When the first 0 is written to a port, it
becomes an output. To reconfigure it as
an input, a 1 must be sent to a port.
https://openlabpro.com/guide/timer-and-
counter-with-pic18f4550/

Functional diagram of a timer module

Basically, a timer is a clock that controls an event sequence at a fixed


amount of time. Timers are used for the precise delay generation and also
used to trigger an activity before and after a predetermined time and to
measure the time elapsed between two successive events.
https://openlabpro.com/guide/timer-and-
counter-with-pic18f4550/

Functional diagram of a timer module

The timer inside a microcontroller is a free running binary counter. The


counter increments for each pulse applied to it. The counter counts
continuously from 0 to (2^n)-1 where n is the number of bits. The timer
takes the internal clock as a reference clock, while the counter counts
external clocks or pulses applied through port pins. So basically timer is a
counter with an internal clock.
https://openlabpro.com/guide/timer-and-
counter-with-pic18f4550/

Functional diagram of a timer module

The main advantage of timers and counters is that it works independent of


microcontroller CPU and the timer values can be read whenever needs. Basically, a
standard microcontroller consists of 1 or more hardware timer modules of different bit
lengths.

•8-bit timers- capable of counting 0-255


•16-bit timer – capable of counting 0-65535

The initial values of the timer register can be set by the user and can be used to generate
required counts.
https://openlabpro.com/guide/timer-and-
counter-with-pic18f4550/

Functional diagram of a timer module

Counters
Timers are also called counters because they are used to count external
events. Timers are mainly used for counting or measuring external events.
For example in the case of a visitor counter, the sensor placed for detecting
the presence of a person goes high when someone crosses the door. The
output of the sensor is connected to the Timer Clock Input Pin of the
microcontroller. The timer register inside the microcontroller increments
each time when a person crosses the door. The value can be later read by
the CPU.
https://openlabpro.com/guide/timer-and-
counter-with-pic18f4550/

Functional diagram of a timer module

Prescalar

Prescalar is a configurable clock-divider circuit. It can be used to divide the clock


frequency input to the timer module.
For example, if the instruction clock is 5MHz and we use a prescaler of 2 to
divide it which effectively make the clock 2.5MHz. So each counting time will
increase from 0.2 µs to 0.4 µs.

timer module provides 256, 128, 64, 32, 16, 8, 4, 2 prescalar


https://openlabpro.com/guide/timer-and-
counter-with-pic18f4550/

Functional diagram of a timer module

Overflow

Overflow means the counter reached its maximum output and roll over to zero.
The microcontroller has an overflow flag to indicate the overflow of the counter and
generates overflow interrupts.

This gives an additional option to count the number of times the counter overflowed and
it extends the range of the counter. For example, if we need to count up to 512, we can
use the 8-bit timer and overflow flag. After two overflows, the count must be 512
(256+256).
https://openlabpro.com/guide/timer-and-
counter-with-pic18f4550/

Functional diagram of a timer module

Overflow

Overflow means the counter reached its maximum output and roll over to zero.
The microcontroller has an overflow flag to indicate the overflow of the counter and
generates overflow interrupts.

This gives an additional option to count the number of times the counter overflowed and
it extends the range of the counter. For example, if we need to count up to 512, we can
use the 8-bit timer and overflow flag. After two overflows, the count must be 512
(256+256).
https://microcontrollerslab.com/8051-timer-
generate-delay/

• 8051 microcontroller has two 16-bits built in timers.

• Each one of them can be individually programmed

• These timers can either be configured as timers to generate a time delay or as


counters to count events occurring outside the microcontroller.

• The 8051 timers have three general functions:

• Calculating the amounts of time between events.


• Counting events.
• Generating baud rate for serial port.
https://microcontrollerslab.com/8051-timer-
generate-delay/

how to generate delay using 8051 timers

8051 microcontroller has two


timers: Timer0 and Timer1. Both
of them can be used as timers or
counters and both are 16-bits
wide. But the architecture of
8051 is 8-bit, so each 16-bit is
accessed as two separate
registers of low byte and high
byte.
https://microcontrollerslab.com/8051-timer-
generate-delay/

how to generate delay using 8051 timers

8051 Timer0 Registers:


It is a 16-bit register and accessed as
low byte and high byte. The low byte is
referred as TL0 and the high byte is
referred as TH0. These registers can be
accessed like any other register.

8051 Timer1 Registers:


Timer1 is also a 16-bits register, which
is split into two bytes, referred to as TL1
and TH1. Timer0 and timer1 share two
common SFRs (special function
registers): TMOD and TCON.
https://microcontrollerslab.com/8051-timer-
generate-delay/

8051 timer TMOD (timer mode) REGISTER


GATE:           This bit is used for choice of
This timer is used to set various timer internal or external control. −
modes in timer0 and timer1. In this 8-bit GATE=1, we can start and stop the timer
TMOD register, 4 lower bits are set aside from the external source
GATE=0, we don’t need external
for timer0 and the 4 upper bits are used for
hardware to start and stop the timers
timer1.
For each timer, the lower 2 bits are used to C/T:                This bit decides whether the
set the timer mode while the remaining 2 timer is used as time delay generator or
upper bits are used to specify the operation. as an event counter.
C/T = 0, used as timer
C/T = 1,used as counter

MODE Bits M1, M0:


The last two bits for each timer i.e. M1
and M0 are used to select the timer
mode. 8051 has 4 timer modes which are
given below.
https://microcontrollerslab.com/8051-timer-
generate-delay/

8051 timer TMOD (timer mode) REGISTER


https://microcontrollerslab.com/8051-timer-
generate-delay/

8051 timer TMOD (timer mode) REGISTER


Mode 1:

•It is a 16-bit timer.
•Values from 0000 to FFFFH can be loaded into the
timer’s registers TL and TH.
•Timer must be started after loading TH and TL
with16-bit initial value.
•We can give initial values by using “SETB TR0” for
timer0 and “SETB TR1” for timer1.
•When timer is started, it starts counting until it
reaches its limit of FFFFH.
•After this limit of 0000H, it sets a TF (timer flag)
bit, TF0 for timer0, TF1 and for timer1.
•Now stop the timer with the instructions “CLR
TR0” for timer0 or “CLR TR1” for timer1.
•To repeat the process, the original values must be
reloaded in TH and TL registers and timer flag must
be reset to 0.
https://microcontrollerslab.com/8051-timer-
generate-delay/

8051 timer TCON REGISTER

The second special function register



is Timer control register. It is an 8 bit
register and each bit has a special
function. Bits, symbols and functions
of every bits of TCON register are as
follows:
https://microcontrollerslab.com/8051-timer-
generate-delay/

DELAY CALCULATION of 8051 timer 0 , mode 1

Clock source:
Timer needs a clock source. If C/T = 0, the crystal frequency attached to the 8051 is the source
of the clock for the timer. The value of crystal frequency attached to the microcontroller
decides the speed at which the timer ticks. Now suppose that crystal frequency is 11.059MHz.
Timer’s clock frequency:
The frequency for the timer is always 1/12th of the frequency of the crystal attached to the
8051.
TF = 1/12  x 11.059MHz = 921583 Hz

Timer’s clock period:


The time delay of one machine cycle is given below. We use this to generate the delay.
TP = 1/ 921583= 1.085 µ sec
https://microcontrollerslab.com/8051-timer-
generate-delay/

DELAY CALCULATION of 8051 timer 0, mode 1


For delay of 10ms:
1. Firstly divide the desired time delay value (10 ms) by the timer clock period. −

N=  1 / 1.085us (from previous slide)


N= 10ms / 1.0859usec
N=9216

2. Subtract the value of N from the maximum number of counts possible for 16 bit timer i.e.
2^16 = 65536.
M=65536-N
M=65536-9216
M= 56320
3. Convert this value to hexadecimal and write in TH and TL registers
MH=DC00H
TH=DCH
TL=00H
https://microcontrollerslab.com/8051-timer-
generate-delay/

Circuit 8051 timer 0, mode 1

Take an example of creating a delay of 10ms using timers of 8051


microcontroller. Crystal used is of frequency 11.059MHz.
https://microcontrollerslab.com/8051-timer-generate-delay/

#include<reg51.h>
sbit led=P2^0; // led at PORT 2 pin 0
void Delay(void); // Delay function declaration
DELAY code 8051 timer 0, mode 1 void main () // main function
{
led=0; //output PORT
while(1) // infinite loop −

{
led = 1; // LED ON
Delay();
led = 0; // LED OFF
Delay();
}
}
void Delay()
{
TMOD = 0x01; // Timer0 mode1
TH0=0xDC; //initial value for 10ms
TL0=0x00;
TR0 = 1; // timer0 start
while (TF0 == 0); // check overflow condition
TR0 = 0; // Stop Timer
TF0 = 0; // Clear flag
}
https://microcontrollerslab.com/8051-timer-generate-delay/

Output of code 8051 timer 0, mode 1 from previous slide


Additional Examples
http://what-when-how.com/8051-microcontroller/programming-
timers-0-and-1-in-8051-c/ −

You might also like