8051 based LED blinking code using timer delay
In my earlier program I have blink a LED using delay and in that program the delay was generating through for Loop. Now I am posting a new program in which I have again blink a LED using delay but this time the delay is generated trough timers.
In 8051 there are two timers one is Timer0 and the other one is timer1. In this program I have generated 50ms sec delay through Timer0 and 50 ms delay using Timer1. I have used both the timers in this program just because you can understand how to use both the timers.
You can see the program in below image:-
The procedure of LED blinking is similar to the previous program but the changes is only of two functions which we called in this. One is Timer0_Delay() for generating 50 msec delay using timer0 and the other one is Timer1_Delay() for generating 50 msec delay using timer1.Now in above program LED will toggle after 100 msec. Both functions and macros you can see in below images.
In above images you can see the configuration of both the Timers. Timer setting can be done through TMOD register. Details ofTMOD register is given below.
TMOD Register
Bit 7
|
Bit 6
|
Bit 5
|
Bit 4
|
Bit 3
|
Bit 2
|
Bit 1
|
Bit 0
|
GATE
|
C/T
|
M1
|
M0
|
GATE
|
C/T
|
M1
|
M0
|
In this register first two bit is of timer mode selection of Timer 0 and bit 4 and 5 is of timer mode selection of Timer 1. In this program we have selected 16-bit timer mode for both the timers.
Mode selection:-
M1
|
M0
|
Mode
|
0
|
0
|
13-bit timer
|
0
|
1
|
16-bit timer
|
1
|
0
|
8-bit with auto reload
|
1
|
1
|
split timer mode
|
Bit 2 is Counter/timer selection bit for timer 0.we have selected timer mode for timer0 and same is for timer1 (Bit 6).
TMOD register is for configure the timer. Delay calculation can be done through THx and TLx Registers. Timer delay calculations is explained below :-
Timer Frequency = crystal Freq /12 = 11.0592 Mhz/12 = 921.6 kHz
Time = 1/f = 1/921.6 khz = 1.085 usec
Timer is of 16 bit so max count is = 2^16 =65536
Values loaded to timer0/1 = THx*256+TLx =75*256+253 =19453
Delay time = 65536-19453 = 46083 *1.085 us = 50 msec.
So you can generate different time delay by loading different values to THx and TLx registers.
Macros and function declaration is define in “timer.h” file shown in below image :-
For downloading complete program you can click here.Also follow my blog for further 8051 and other controller based programs.
No comments:
Post a Comment