Line 97: Line 97:
 
|description=8051 Timers
 
|description=8051 Timers
 
}}
 
}}
 
+
<\br>
 
<syntaxhighlight>
 
<syntaxhighlight>
#define databus P2   // LCD databus connected to PORT2
+
#include<reg51.h>void delay_t0(void);// function prototypevoid delay_t0(){   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(); } }
sbit rs= P0^0; // Register select pin connected to P0.0
+
sbit rw= P0^1; // Read Write pin connected to P0.1
+
sbit en= P0^2; // Enable pin connected to P0.2
+
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Let us look at three important functons  in ''' lcd_8_bit.c'''<br />
+
 
1. '''void LCD_CmdWrite( char cmd)'''<br />
+
 
Commands are required to sent to lcd in order to set it up or initialize.
+
 
The timing diagrams for command write are shown in the figure,
+
[[File:LCD CmdWrite.jpg|figure: command write]]<br />
+
*step1: Send the I/P command to LCD.
+
*step2: Select the Control Register by making RS low.
+
*step3: Select Write operation making RW low.
+
*step4: Send a High-to-Low pulse on Enable PIN with some delay_us.
+
<syntaxhighlight>
+
</syntaxhighlight>
+
 
Would like to have your feedback and suggestions here;
 
Would like to have your feedback and suggestions here;
 
{{DISQUS}}
 
{{DISQUS}}

Revision as of 15:29, 30 July 2014



In this tutorial we will see 8051 timers. We will use the 8051 timers to generate a precise delay of 1 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

Timer Operation

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.
The 8051 timer and counter is the same unit, but in this tutorial we will discuss only the timer unit to simplify the discussion.

fig 1:Basic Timer

Timer Tick

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

Table. 1: Timer Register
T0
TH0 TL0
D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0


TMOD Register

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.

Table 3. Timer Modes
M1 M0 Operation
0 0 13 bit Timer
0 1 16 bit Timer
1 0 8 bit Auto Reload
1 1 Split Mode
Table 2. TMOD Register
TMOD
D7 D6 D5 D4 D3 D2 D1 D0
Gate C/T M1 M0 Gate C/T M1 M0
Timer1 Timer 0
fig 2:Mode 0
fig 3:Mode 1
fig 4:Mode 2


TCON Register

Table 4. TCON Register
TCON
D7 D6 D5 D4 D3 D2 D1 D0
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer1 Timer 0 Timer Interrupts

Timer Example

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


PREVIOUS TUTORIAL
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 }} <\br>

#include<reg51.h>void delay_t0(void);// function prototypevoid delay_t0(){   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();	 } }



Would like to have your feedback and suggestions here;