Aim:
In this tutorial, we are going to discuss the Timer module of 8051.
First, we will see what are timers, their working and later we will
configure the 8051 timers to generate the delay of 100ms and 500ms
respectively. At the end, we will see how to use the ExploreEmdedded
Timer library.
In this tutorial, we are going to discuss the Timer module of 8051.
First, we will see what are timers, their working and later we will
configure the 8051 timers to generate the delay of 100ms and 500ms
respectively. At the end, we will see how to use the ExploreEmdedded
Timer library.
Description:
8051 has two indepenndent timer which can be used as timer(to generate delays)/Counters(count external events).
Timer 1 is also used for generating baud rate in serial communication, which we will discuss in the next tutorial
Below table provides the details of the 8051 Timers.
TIMER SIZE CONTROL REGISTER COUNT REGISTER MIN DELAY MAX DELAY
TIMER0 16-bit TMOD,TCON TH0,TL0 1.085µs 71.107ms
TIMER1 16-bit TMOD,TCON TH1,TL1 1.085µs 71.107ms
TIMER2(8052 only) 16-bit T2CON RCAP2H,RCAP2L 1.085µs 71.107ms
8051 has two indepenndent timer which can be used as timer(to generate delays)/Counters(count external events).
Timer 1 is also used for generating baud rate in serial communication, which we will discuss in the next tutorial
Below table provides the details of the 8051 Timers.
Timer 1 is also used for generating baud rate in serial communication, which we will discuss in the next tutorial
Below table provides the details of the 8051 Timers.
TIMER | SIZE | CONTROL REGISTER | COUNT REGISTER | MIN DELAY | MAX DELAY |
---|---|---|---|---|---|
TIMER0 | 16-bit | TMOD,TCON | TH0,TL0 | 1.085µs | 71.107ms |
TIMER1 | 16-bit | TMOD,TCON | TH1,TL1 | 1.085µs | 71.107ms |
TIMER2(8052 only) | 16-bit | T2CON | RCAP2H,RCAP2L | 1.085µs | 71.107ms |
Timer Registers
TMOD
TIMER1 TIMER 0
7 6 5 4 3 2 1 0
Gate C/T M1 M0 Gate C/T M1 M0
- Gate Control
0 = Timer enabled
1 = Timer enabled if INTx is high
- C/T:Counter or Timer Selector
0 = Internal count source (clock/12)
1 = External count source T0/T1(P3.4/P3.5) pin.
- M1-M0:Mode Control
00-Mode 0, 13 bit count mode
01-Mode 1, 16 bit count mode
10-Mode 2, Auto reload mode
11-Mode 3, Split Timer mode
TCON
7 6 5 4 3 2 1 0
TF1 TR1 TF0 TR0
- TRx: Timer x run control
0 = Timer not running
1 = Timer running
- TFx: Timer x OverFlow flag
0 = Timer has not overflowed/rolled over
1 = Timer has overflowed/rolled over
TMOD | |||||||
TIMER1 | TIMER 0 | ||||||
---|---|---|---|---|---|---|---|
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
Gate | C/T | M1 | M0 | Gate | C/T | M1 | M0 |
- Gate Control
0 = Timer enabled
1 = Timer enabled if INTx is high
1 = Timer enabled if INTx is high
- C/T:Counter or Timer Selector
0 = Internal count source (clock/12)
1 = External count source T0/T1(P3.4/P3.5) pin.
1 = External count source T0/T1(P3.4/P3.5) pin.
- M1-M0:Mode Control
00-Mode 0, 13 bit count mode
01-Mode 1, 16 bit count mode
10-Mode 2, Auto reload mode
11-Mode 3, Split Timer mode
01-Mode 1, 16 bit count mode
10-Mode 2, Auto reload mode
11-Mode 3, Split Timer mode
TCON | |||||||
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
TF1 | TR1 | TF0 | TR0 |
- TRx: Timer x run control
0 = Timer not running
1 = Timer running
1 = Timer running
- TFx: Timer x OverFlow flag
0 = Timer has not overflowed/rolled over
1 = Timer has overflowed/rolled over
1 = Timer has overflowed/rolled over
Timer Calculation
8051 Oscillator frequency is divided by 12 and then fed to the
controller, Time to increment the Timer count by one(timer tick) can be
determined as below.
tick = (1/(Fosc/12)
$$tick = 12/Fosc$$ For Fosc == 11.0592Mhz, the tick time will be
tick = 12/11.0592M = 1.085069444us = 1.085us
Now the Timer value for the required delay can be calculated as below.
Delay = TimerCount * tick
Count = (Delay/tick)
RegValue = TimerMax- Count
RegValue = TimerMax-(Delay/tick) = TimerMax – (Delay/1.085us)
$$RegValue = TimerMax-((Delay/1.085) * 10^6)$$
8051 Oscillator frequency is divided by 12 and then fed to the
controller, Time to increment the Timer count by one(timer tick) can be
determined as below.
tick = (1/(Fosc/12)
$$tick = 12/Fosc$$ For Fosc == 11.0592Mhz, the tick time will be
tick = 12/11.0592M = 1.085069444us = 1.085us
tick = (1/(Fosc/12)
$$tick = 12/Fosc$$ For Fosc == 11.0592Mhz, the tick time will be
tick = 12/11.0592M = 1.085069444us = 1.085us
Now the Timer value for the required delay can be calculated as below.
Delay = TimerCount * tick
Count = (Delay/tick)
RegValue = TimerMax- Count
RegValue = TimerMax-(Delay/tick) = TimerMax – (Delay/1.085us)
$$RegValue = TimerMax-((Delay/1.085) * 10^6)$$
Delay = TimerCount * tick
Count = (Delay/tick)
RegValue = TimerMax- Count
RegValue = TimerMax-(Delay/tick) = TimerMax – (Delay/1.085us)
$$RegValue = TimerMax-((Delay/1.085) * 10^6)$$
Timer Mode1
The timer in Mode-1 can be used as a 16-bit timer to count from 0000 to
FFFFH thus allowing to generate a wide range of delay. The timer value
for the required delay needs to be loaded into Timer Count registers TH
& TL. After loading the values to the register, the timers must be
started. Now the Timer starts counting up and once it reaches the max
value(0xffff), it rolls back to zero setting the overflow flag. At this
point, the timer values must be reloaded and the overflow flag should
also be cleared.
The timer in Mode-1 can be used as a 16-bit timer to count from 0000 to
FFFFH thus allowing to generate a wide range of delay. The timer value
for the required delay needs to be loaded into Timer Count registers TH
& TL. After loading the values to the register, the timers must be
started. Now the Timer starts counting up and once it reaches the max
value(0xffff), it rolls back to zero setting the overflow flag. At this
point, the timer values must be reloaded and the overflow flag should
also be cleared.
Timer Calculation for 50ms delay
Fosc = 11.0592Mhz
Delay = 50ms
$$RegValue = TimerMax-((Delay/1.085) * 10^6)$$ RegValue = 65536 – (50ms/1.085)*10^6 = 65536 – 46082 = 19453 = 0x4BFD
Fosc = 11.0592Mhz
Delay = 50ms
$$RegValue = TimerMax-((Delay/1.085) * 10^6)$$ RegValue = 65536 – (50ms/1.085)*10^6 = 65536 – 46082 = 19453 = 0x4BFD
Delay = 50ms
$$RegValue = TimerMax-((Delay/1.085) * 10^6)$$ RegValue = 65536 – (50ms/1.085)*10^6 = 65536 – 46082 = 19453 = 0x4BFD
Block Diagram
Schematic
Code
// **********************************************
// Project: Interfacing 8-bit timer0 to 8051
// Author: Code Bloges
// Module description: Operate array of LED's
// **********************************************
#include<reg51.h>
sbit LED = P2^0;
void timerDelay()
{
TH0 = 0X4B; //Load the timer value
TL0 = 0XFD;
TR0 = 1; //turn ON Timer zero
while(TF0 == 0); // Wait for Timer Overflow
TF0 = 0; //clear the timer Over flow flag
TR0 = 0;
}
void main()
{
TMOD = 0x01; //Timer0 mode 1
while(1)
{
LED = 1;
timerDelay();
LED = 0;
timerDelay();
}
}
// **********************************************
// Project: Interfacing 8-bit timer0 to 8051
// Author: Code Bloges
// Module description: Operate array of LED's
// **********************************************
#include<reg51.h>
sbit LED = P2^0;
void timerDelay()
{
TH0 = 0X4B; //Load the timer value
TL0 = 0XFD;
TR0 = 1; //turn ON Timer zero
while(TF0 == 0); // Wait for Timer Overflow
TF0 = 0; //clear the timer Over flow flag
TR0 = 0;
}
void main()
{
TMOD = 0x01; //Timer0 mode 1
while(1)
{
LED = 1;
timerDelay();
LED = 0;
timerDelay();
}
}
Downloads:
The code was compiled in Keil uvision4 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.
The code was compiled in Keil uvision4 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.
To download code and proteus simulation click here.
Further Reading suggestions:
You may also like,
- Interfacing keypad with 8051
- Interfacing with UART of 8051 controller
- Interfacing SPI communication with 8051
- 8051 Displaying Custom Characters on LCD
- 8051 Graphical LCD
- RTC interfacing using I2C in 8051
- Interfacing ultrasonic sensor with 8051
- Interfacing GPS Modu with 8051
- Interfacing GSM Module with 8051
- ADC Module interfacing with 8051
- Interfacing PWM in 8051
- Interfacing keypad with 8051
- Interfacing with UART of 8051 controller
- Interfacing SPI communication with 8051
- 8051 Displaying Custom Characters on LCD
- 8051 Graphical LCD
- RTC interfacing using I2C in 8051
- Interfacing ultrasonic sensor with 8051
- Interfacing GPS Modu with 8051
- Interfacing GSM Module with 8051
- ADC Module interfacing with 8051
- Interfacing PWM in 8051
No comments:
Post a Comment