(36 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
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.
 
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.
 
<br><br>
 
<br><br>
 +
 +
=8051 Timer Module=
 +
8051 has two indepenndent timer which can be used as <b>timer</b>(to generate delays)/<b>Counters</b>(count external events).<br>
 +
'''Timer 1''' is also used for generating baud rate in serial communication, which we will discuss in the next tutorial<br>
 +
Below table provides the details of the 8051 Timers.
 +
{| class="table table-striped table-hover table-condensed table-bordered"
 +
|-class="info"
 +
!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
 +
|}<br>
  
 
=Timer Basics=
 
=Timer Basics=
Line 11: Line 26:
 
[[FILE:timer.gif]]<br>
 
[[FILE:timer.gif]]<br>
  
=8051 Timer Module=
+
The Timer 0 is 16 bit wide as shown. This can be accessed as 2 eight bit registers '''TL0 and TL1'''. Same applies to Timer 1. The 8051 timer and counter are the same units, but in this tutorial, we will discuss only the timer unit to simplify the discussion. Fig 1, shows the basic 8051 timer unit. The registers '''TCON''' and '''TMOD''' affect the timer operation. The clock frequency is divided by 12 and used by the timer unit. Thus if a '''11.0592MHz''' external crystal is used, the timer uses a frequency of 921KHz. Thus timer increments every '''(1/921Khz) = 1.085μ''' seconds.
8051 has two indepenndent timer which can be used as timer,Counters.<br>
+
* The '''C/Ṫ = 0'''  bit of TMOD register selects operation of Timer/counter unit as timer.
Below table provides the details of the 8051 Timers.
+
* The '''TR bit''' of TCON register is used to start the timer.
 +
[[File:Basic Timer.jpeg]]
 +
<br><br><br>
 +
 
 +
= Timer Registers =
 
{| class="table table-striped table-hover table-condensed table-bordered"
 
{| class="table table-striped table-hover table-condensed table-bordered"
 
|-class="info"
 
|-class="info"
!Timer || Size || Control Register || Count Register || Min Delay || Max Delay
+
|TMOD
 
|-
 
|-
|TIMER0|| 16-bit || TMOD,TCON|| TH0,TL0|| 0.2usec || 13.107ms
+
!colspan="4"|Timer1||colspan="4"|Timer 0  
|-   
+
|TIMER1|| 16-bit || TMOD,TCON|| TH1,TL1 || 0.2usec || 104.857ms
+
 
|-
 
|-
|TIMER2(8052 only)|| 16-bit || TMOD,TCON|| TH1,TL1 || 0.2usec || 104.857ms
+
|7||6||5||4||3||2||1||0
|}<br>
+
|-
 +
|Gate||C/T||M1||M0||Gate||C/T||M1||M0
 +
|}
 +
 
 +
*<b>Gate Control</b>
 +
0 = Timer enabled<br>
 +
1 = Timer enabled if INTx is high
 +
*<b>C/T:</b>Counter or Timer Selector
 +
0 = Internal count source (clock/12)<br>
 +
1 = External count source T0/T1(P3.4/P3.5) pin.
 +
*<b>M1-M0:</b>Mode Control
 +
00-Mode 0, 13 bit count mode<br>
 +
01-Mode 1, 16 bit count mode<br>
 +
10-Mode 2, Auto reload mode<br>
 +
11-Mode 3, Split Timer mode
 +
 
 +
{| class="table table-striped table-hover table-condensed table-bordered"
 +
|-class="info"
 +
|TCON  
 +
|-
 +
|7 || 6 ||  5 || 4 || 3 || 2 || 1 || 0  
 +
|-
 +
|TF1||TR1||TF0||TR0|| || || ||
 +
|}  
 +
 
 +
*<b>TRx:</b> Timer x run control
 +
0 = Timer not running<br>
 +
1 = Timer running
 +
*<b>TFx:</b> Timer x OverFlow flag
 +
0 = Timer has not overflowed/rolled over<br>
 +
1 = Timer has overflowed/rolled over
 +
 
 +
<br><br><br>
  
 
=Timer Calculation=
 
=Timer Calculation=
PIC Oscillator frequency is divided by 4 and then fed to the controller, Now this this freq can be further divided by presacalar to generate the range of delays.<br>
+
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. <br>
 
Time to increment the Timer count by one(timer tick) can be determined as below. <br>
tick = (Prescalar/(Fosc/4)<br>
+
tick = (1/(Fosc/12)<br>
tick = (Prescalar/(20Mhz/4))<br>
+
$$tick = 12/Fosc$$
$$tick = (Prescalar * 4)/Fosc$$
+
For  Fosc == 11.0592Mhz, the tick time will be<br>  
 +
tick = 12/11.0592M = 1.085069444us = 1.085us
  
 
Now the Timer value for the required delay can be calculated as below.<br>
 
Now the Timer value for the required delay can be calculated as below.<br>
Line 36: Line 86:
 
Count = (Delay/tick)<br>
 
Count = (Delay/tick)<br>
 
RegValue = TimerMax- Count <br>
 
RegValue = TimerMax- Count <br>
RegValue = TimerMax-(Delay/tick) = TimerMax - (Delay/((Prescalar *4)/Fosc))<br>
+
RegValue = TimerMax-(Delay/tick) = TimerMax - (Delay/1.085us)<br>
$$RegValue = TimerMax-((Delay * Fosc)/(Prescalar*4))$$
+
$$RegValue = TimerMax-((Delay/1.085) * 10^6)$$
 +
<br><br>
  
=Video Tutorial=
+
=Timer Mode1=
==Timer Basics==
+
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.  
{{#ev:youtube|5CtkgpWG0vQ|600}}
+
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.
==Generating 50ms Delay with Timer 0, Mode 1==
+
{{#ev:youtube|pAKm7TCp7es|600}}
+
{{Box|type=green|text=<br/>
+
In this tutorial we will see 8051 timers. We will use the 8051 timers to generate a precise delay of 50 milli sec.
+
=8051 timers/counters=
+
The 8051 has 2 timers/counters.  
+
*They can be used to generate precise timing, i.e., we can measure time between events. The unit is then called '''timer'''.
+
*It can also be used to count external events, known as '''counter'''.
+
*'''Timer 1''' is also used for generating baud rate in serial communication, which we will discuss in the next tutorial<br />
+
}}
+
  
==Timer Operation==
+
===Timer Calculation for 50ms delay===
{{Box|type=l_green_light|text=
+
Fosc = 11.0592Mhz<br>
The Timer 0 is a 16 bit registers as shown. This can be accessed as 2 eight bit registers '''TL0 and TL1'''. Same applies to Timer 1.<br />
+
Delay = 50ms<br>
The 8051 timer and counter is the same unit, but in this tutorial we will discuss only the timer unit to simplify the discussion.
+
$$RegValue = TimerMax-((Delay/1.085) * 10^6)$$
}}
+
RegValue = 65536 - (50ms/1.085)*10^6 = 65536 - 46082 = 19453 = 0x4BFD
  
[[File:Basic Timer.jpeg|thumbnail|670xpx|fig 1:Basic Timer]]
+
===Polling Method===
===Timer Tick===
+
<html><script src="https://gist.github.com/SaheblalBagwan/55c4749a9d55ab2f407ed71b0257e1f1.js"></script></html>
{{Box|type=l_green_light|text=
+
Fig 1, shows the basic 8051 timer unit. The registers '''TCON''' and '''TMOD''' affect the timer operation. The clock frequency is divided by 12 and used by the timer unit. Thus if a '''11.0592MHz''' external crystal is used, the timer uses a frequency of 921KHz. Thus timer increments every '''1.085μ''' seconds.
+
* The '''C/Ṫ = 0'''  bit of TMOD register selects operation of Timer/counter unit as timer.
+
* The '''TR bit''' of TCON register is used to start the timer.
+
}}
+
=== Timer Registers ===
+
====Timer Register T0/T1 ====
+
{| class="wikitable" style="text-align:center;background-color:#87A96B;margin: 1em auto 1em auto;"
+
|+ Table. 1: Timer Register
+
|-
+
!colspan = '16'|T0
+
|-
+
|colspan='8'|TH0||colspan='8'|TL0
+
|-
+
|D15||D14||D13||D12||D11||D10||D9||D8||D7||D6||D5||D4||D3||D2||D1||D0
+
|}
+
  
====TMOD Register ====
+
===Interrupt Method===
{{Box|type=l_green_light|text=
+
<html><script src="https://gist.github.com/SaheblalBagwan/699f9d126f239b1523c810f0f4e340e9.js"></script></html>
The '''TMOD''' Register specifies the operational mode of the two timers. The higher nibble is used for Timer 1 and Lower for the timer 0 as shown below.
+
 
}}
+
 
{| class="wikitable floatright" style="text-align:center;background-color:#87A96B;"
+
=Timer Mode2=
|+ Table 3. Timer Modes
+
The timer in Mode-2 can be used as an 8-bit timer to count from 00 to FFH . The timer value for the required delay needs to be loaded into Timer Count registers TH(which is copied to TL). After loading the values to the register,  the timers must be started.  
|-
+
Now the Timer starts incrementing TL and once it reaches the max value(0xff), it rolls back to zero setting the overflow flag and reloads the value from TH.
!M1||M0||Operation
+
[[File:Mode_2.jpeg]]
|-
+
 
|0||0||13 bit Timer
+
===Timer Calculation for 250µs delay===
|-
+
Fosc = 11.0592Mhz<br>
|0||1||16 bit Timer
+
Delay = 250µs<br>
|-
+
$$RegValue = TimerMax-((Delay/1.085) * 10^6)$$ <br>
|1||0||8 bit Auto Reload
+
RegValue = 256 - (250µs/1.085)*10^6 = 256 - 230 = 26 = 0x1A
|-
+
 
|1||1||Split Mode
+
<html><script src="https://gist.github.com/SaheblalBagwan/b8fa48ee29693f2a42a7338f0f103a80.js"></script></html>
|}
+
{| class="wikitable" style="text-align:center;background-color:#87A96B;"
+
|+ Table 2. TMOD Register
+
|-
+
!colspan = '8'|TMOD
+
|-
+
|D7||D6||D5||D4||D3||D2||D1||D0
+
|-
+
|Gate||C/T||M1||M0||Gate||C/T||M1||M0
+
|-
+
!colspan="4"|Timer1||colspan="4"|Timer 0
+
|}
+
  
====TCON Register====
 
{| class="wikitable" style="text-align:center;background-color:#87A96B;margin: 1em auto 1em auto;"
 
|+ Table 4. TCON Register
 
|-
 
!colspan = '8'|TCON
 
|-
 
|D7||D6||D5||D4||D3||D2||D1||D0
 
|-
 
|TF1||TR1||TF0||TR0||IE1||IT1||IE0||IT0
 
|-
 
!colspan="2"|Timer1||colspan="2"|Timer 0||colspan="4"|Interrupts
 
|}
 
  
[[File:Mode_0.jpeg|thumbnail|680xpx|fig 2:Mode 0]]
 
[[File:{{#setmainimage: Mode_1.jpeg}}|thumbnail|680xpx|fig 3:Mode 1]]
 
[[File:Mode_2.jpeg|thumbnail|680xpx|fig 4:Mode 2]]
 
  
<br/>
 
<br/>
 
<br/>
 
<br/>
 
=Timer Example=
 
{{Box|type=l_green_light|text= This example will demonstrate use of Timer 0 for generating delay. We will use Timer 0 in mode 1 to generate a delay of 50ms and will toggle pins of P3 every 50ms}}
 
<syntaxhighlight>
 
#include<reg51.h>
 
void delay_t0(void);// function prototype
 
  
void delay_t0()
+
[[File:00_Lpc1768_Timer.jpg]]
{
+
  TMOD = 0x01; //Timer zero mode 1 
+
  TH0 = 0X4B;
+
  TL0 = 0XFF;
+
  TR0 = 1; //turn ON Timer zero
+
  while(TF0 == 0);
+
  TF0 = 0; //clear the timer
+
  TR0 = 0;
+
}
+
void main()
+
{
+
  P3 = 0x00; //set port as output 
+
  while(1)
+
  {
+
  P3 = 0XFF;
+
  delay_t0();
+
  P3 = 0X00;
+
  delay_t0();
+
  }
+
}
+
</syntaxhighlight>
+
  
 +
=Downloads=
 +
Download the sample code and design files from [https://github.com/ExploreEmbedded/8051_DevelopmentBoard this link].
  
[[File:LCDDisplay.JPG|left|thumbnail|x80px|{{Box|type=l_blue_light|text=LCD 16 x 2 Interfacing with 8051}}|link=LCD 16 x 2 Interfacing with 8051|'''''PREVIOUS TUTORIAL''''']]
 
[[File:Serial_Communicaton_Poster.jpeg|right|thumbnail|x85px|link=8051_serial_communication|Serial Communication|'''''NEXT TUTORIAL''''']]
 
{{#seo:
 
|title=8051_Timers
 
|titlemode=append
 
|keywords=8051,AT89s51,at89c51,p89v51rd2,XploreLabz,Timer,Timers,8051 timers/counters,8051 timer operation,8051 Timer registers,TMOD Register,TCON register
 
|description=8051 Timers
 
}}
 
 
Would like to have your feedback and suggestions here;
 
Would like to have your feedback and suggestions here;
 
{{DISQUS}}
 
{{DISQUS}}

Latest revision as of 10:12, 2 September 2016


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.

8051 Timer Module

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

Timer Basics

As the name suggests these are used to measure the time or generate the accurate time delay. The microcontroller can also generate/measure the required time delays by running loops, but the timer/counter relieves the CPU from that redundant and repetitive task, allowing it to allocate maximum processing time for other tasks.

The timer is nothing but a simple binary counter that can be configured to count clock pulses(Internal/External). Once it reaches the Max value, it will roll back to zero setting up an OverFlow flag and generates the interrupt if enabled.

Timer.gif

The Timer 0 is 16 bit wide as shown. This can be accessed as 2 eight bit registers TL0 and TL1. Same applies to Timer 1. The 8051 timer and counter are the same units, but in this tutorial, we will discuss only the timer unit to simplify the discussion. Fig 1, shows the basic 8051 timer unit. The registers TCON and TMOD affect the timer operation. The clock frequency is divided by 12 and used by the timer unit. Thus if a 11.0592MHz external crystal is used, the timer uses a frequency of 921KHz. Thus timer increments every (1/921Khz) = 1.085μ seconds.

  • The C/Ṫ = 0 bit of TMOD register selects operation of Timer/counter unit as timer.
  • The TR bit of TCON register is used to start the timer.

Basic Timer.jpeg


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




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

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.

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

Polling Method

Interrupt Method


Timer Mode2

The timer in Mode-2 can be used as an 8-bit timer to count from 00 to FFH . The timer value for the required delay needs to be loaded into Timer Count registers TH(which is copied to TL). After loading the values to the register, the timers must be started. Now the Timer starts incrementing TL and once it reaches the max value(0xff), it rolls back to zero setting the overflow flag and reloads the value from TH. Mode 2.jpeg

Timer Calculation for 250µs delay

Fosc = 11.0592Mhz
Delay = 250µs
$$RegValue = TimerMax-((Delay/1.085) * 10^6)$$
RegValue = 256 - (250µs/1.085)*10^6 = 256 - 230 = 26 = 0x1A



00 Lpc1768 Timer.jpg

Downloads

Download the sample code and design files from this link.

Would like to have your feedback and suggestions here;