4 Timer 8051 02 02 2023
4 Timer 8051 02 02 2023
4 Timer 8051 02 02 2023
Slot: L15-16
_________________________________________
Group Members
Vansh Kaushik – 19BCT0115
Hritam Dutta – 19BCT0121
Anay Rajguru – 19BCT0137
Aditya Pradhan – 19BCT0155
8051 Microcontroller Timer 0 Mode 0
Introduction
8051 microcontrollers have two timers/counters which work on the clock
frequency. Timer/counter can be used for time delay generation, counting external
events, etc.
8051 has two timers Timer0 (T0) and Timer1 (T1), both are 16-bit wide. Since 8051 has 8-bit
architecture, each of these is accessed by two separate 8-bit registers as shown in the figure
below. These registers are used to load timer count.
TMOD register - TMOD is an 8-bit register used to set timer mode of timer0 and
timer1.
TCON Register - TCON is an 8-bit control register and contains a timer and interrupt flags.
Input -
As the Xtal oscillator frequency is 11.0592 MHz we have a machine cycle
of 1.085uSec. Hence, the required count to generate a delay of 1mSec. is,
Count =(1×10^-3)/(1.085×10^-6) ≈ 921
The maximum count of Mode0 is 2^13 (0 - 8191) and the Timer0 count will
increment from 0 – 8191. So we need to load value which is 921 less from
its maximum count i.e. 8191. Also, here in the below program, we need an
additional 13 MC (machine cycles) from call to return of delay function.
Hence value needed to be loaded is,
Value=(8191-Count)+Function_MCycles+1 =7284=
0x1C74
So we need to load 0x1C74 value in Timer0.
1C74 = 0001 1100 0111 0100 b, now load lower 5-bit in TL0 and next 8-bit
in TH0
so here we get,
TL0 = 0001 0100 = 0x14 and TH0 = 1110 0011 = 0xE3
Flowchart -
Algorithm -
1. Load Tmod register value i.e. TMOD = 0x00 for Timer0/1 mode0
(13-bit timer mode).
2. Load calculated THx value i.e. here TH0 = 0xE3.
3. Load calculated TLx value i.e. here TL0 = 0x14.
4. Start the timer by setting a TRx bit. i.e. here TR0 = 1.
5. Poll TFx flag till it does not get set.
6. Stop the timer by clearing TRx bit. i.e. here TR0 = 0.
7. Clear timer flag TFx bit i.e. here TF0 = 0.
8. Repeat from step 1 to 7 for the delay again.
Code -
#include <reg51.h>
sbit test = P1^0;
void timer_delay()
{
TH0 = 0xE3;
TL0 = 0x14;
TR0 = 1;
while(TF0 == 0);
TR0 = 0;
TF0 = 0;
}
void main()
{
TMOD = 0x00;
while(1)
{
test = ~test;
timer_delay();
}
}
Output:
https://youtu.be/JxVZ5nNa-5M
__________________________________________________
__________
Input -
As Xtal is 11.0592 MHz we have a machine cycle of 1.085uSec.
Hence, the required count to generate a delay of 1mSec. is,
Count =(1×10^(-3)) / (1.085×10^(-6) ) ≈ 921
And mode1 has a max count is 2^16 (0 - 65535) and it increments from 0
to 65535 so we need to load value which is 921 less from its max. count
i.e. 65535. Also, here in the below program, we need an additional 13 MC
(machine cycles) from call to return of delay function. Hence value
needed to be loaded is,
Value=(65535-Count)+Function_MCycles+1 =64615= (FC74)Hex
So we need to load FC74 Hex value higher byte in TH0 and lower byte in
TL0 as,
TH0 = 0xFC & TL0 = 0x74
Algorithm –
1. Load Tmod register value i.e. TMOD = 0x01 for Timer0 mode1
(16-bit timer mode).
2. Load calculated THx value i.e. here TH0 = 0xFC.
3. Load calculated TLx value i.e. here TL0 = 0x74.
4. Start the timer by setting a TRx bit. i.e. here TR0 = 1.
5. Poll TFx flag till it does not get set.
6. Stop the timer by clearing TRx bit. i.e. here TR0 = 0.
7. Clear timer flag TFx bit i.e. here TF0 = 0.
8. Repeat from step 1 to 7 for the delay again.
Code -
#include <reg51.h>
sbit test = P1^0;
void timer_delay()
{
TH0 = 0xFC;
TL0 = 0x74;
TR0 = 1;
while(TF0 == 0);
TR0 = 0;
TF0 = 0;
}
void main()
{
TMOD = 0x01;
while(1)
{
test = ~test;
timer_delay();
}
}
Flowchart -
Output –
_____________________________________________________
___________