- This topic has 1 reply, 2 voices, and was last updated 8 years, 10 months ago by .
Viewing 2 posts - 1 through 2 (of 2 total)
Viewing 2 posts - 1 through 2 (of 2 total)
- You must be logged in to reply to this topic.
|
Microcontroller › 8051 › Accessing timers on a microcontroller 8051
Could someone please help me out with this example. I dont understand why the they did not use (25μs × 1000). Why did they break it down to (25μs × 250 × 40) and later load (25μs × 250 × 36) on the microcontroller instead. Thank you.
Example 14: Write an 8051 C program to toggle only pin P1.5 continuously every 250 ms.
Use Timer 0, mode 2 (8-bit auto-reload) to create the delay.
Solution:
Solution: Assume XTAL=11.0592MHz
For the delay of 250ms the count exceeds 256. hence, count for 25μs is calculated and
count is 23
Therefore for 250ms =>25μs × 250 × 40 = 250 ms
Program
#include <reg51.h>
void T0M2Delay(void);
sbit mybit=P1^5;
void main(void){
unsigned char x,y;
while (1) {
mybit=~mybit;
for (x=0;x<250;x++)
for (y=0;y<36;y++) //we put 36, not 40
T0M2Delay();
}
}
void T0M2Delay(void){
TMOD=0x02;
TH0=-23;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
it is very hard to get exact required delay in embedded C programming
u hv to do trial and error for getting closest value of required delay
in loop inside loop it waste more cycles so we can not directly put 250*1000 = 250 ms
also we can not put 250*25*40 = 250 ms. instead we have to put different count like 40,39,38,36 …. likewise and check when there is closest value of 250 ms