Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. 8-bit Microcontrollers Timer Programming
  2. 2. Timer Control Register
  3. 3. Timer Mode Register
  4. 4. Timer Modes M0 M1 Mode 0 0 Mode 0 (13 bit timer mode)‏ 0 1 Mode 1(16 Bit timer mode)‏ 1 0 Mode 2(8 bit auto reload)‏ 1 1 Mode 3(Split Timer mode)‏
  5. 5. Mode 1 Programming It is a 16 bit Timer. After TH and TL are loaded with 16 bit initial value, the timer must be started. SETB TR0 for timer 0. After timer is started, it starts to count u. it counts up until it reachesits limit of FFFFh. When it rolls over from FFFFh to 0000h. It sets a flag bit called TF. This flag can be monitored. After timer reaches its limit and rolls over, in order to repeat the process the registers TH and TL must be reloaded with the orignal value and TF must be reset to 0.
  6. 6. Clock source for timer The frequency of the crystalattached to 8051 will decides the speed at which the 8051 timer ticks. The frequecncy for the timer is always 1/12 th the frequency of the crystal attached to the 8051. Xtal Osc. ÷12
  7. 7. Find the timers clock frequency and its period for various 8051 based systems. 12 Mhz 16Mhz 11.0592Mhz 1/12 * 12Mhz = 1Mhz and T = 1/1Mhz = 1 µ s 1/12 * 16Mhz = 1.333 Mhz and T=1/1.333Mhz=0.75 µ s 1/12*11.0592Mhz= 921.6 Khz T= 1/921.6 Khz = 1.085 µ s
  8. 8. Square wave of 50% duty Cycles(equal portions high and low) on P1.5 bit. MOV TMOD,#01h here MOV TL0,#0F2h MOV TH0,#0FFh CPL P1.5 ACALL Delay SJUMP here delay using timer 0 Delay SETB TR0 Again JNB TF0, Again CLR TR0 CLR TF0 RET
  9. 9. Finding values to be loaded into the Timer Suppose we know the time delay. XTAL = 11.0592Mhz Divide the desired time delay by 1.085 µ s. Perform 65536-n where n is the decimal value we got. Convert this value into hex and load it to timers.
  10. 10. XTAL= 11.0592Mhz Need time delay of 5 ms. Now divide this time with the timer pulse time. 5ms/1.085 µ s = 4608 Clock pulses. 65536-4608 = 60928 = EE00h
  11. 11. #include<reg51.h> Void T0delay(void)‏ Void main { While (1) repeat forever { P1=0x55; T0delay(); P1=0xAA; T0delay(); } } Void T0delay()‏ { TMOD=0x01; TL0=0x00; TH0=0x35; TR0=1; while(TF0==0)‏ TR0=0; TF0=0; } In addition in built ROM, RAM, IO devices, Timers Requires less H/W, reduces PCB size & increases reliability
  12. 12. Delay calculation FFFFh-3500h = CAFFh = 51967 +1 =51968 51968*1.085 µ s= 56.384ms is the approximate delay.
  13. 13. Assignments Write a C program to toggle all bits of P2 countinuosly every 500ms. Use timer 1 mode 1 to create delay. Write a C program to toggle only pin P1.5 countinuesly every 250ms.use timer 0, mode 2 to create the delay. A switch is connected to pin P1.2. Write an C program to moniter Switch and create the following frequencies on P1.7. Switch = 0 500 hz Switch = 1 750 hz Use timer 0 mode 1 for both of them.