i have calculated earlier that it takes the 8051, 1/20th of a second to count from 0 to 46,079. However, the TFx flag is set when the timer overflows back to 0. Thus, if we want to use the TFx flag to indicate when 1/20th of a second has passed we must set the timer initially to 65536 less 46079, or 19,457.
If we set the timer to 19,457, 1/20th of a second later the timer will overflow. Thus we come up with the following code to execute a pause of 1/20th of a second:
TH0=0x076;High byte of 19,457 (76 * 256 = 19,456)
TL0=0x001;Low byte of 19,457 (19,456 + 1 = 19,457)
#include(stdio.h)
#include(at89c51xd2.h)
void delay(void);
void main()
{ int j=0,s=0,m=0,h=0;
SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */
TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */
TH1 = 0x076; /* TH1: reload value */
TL1 = 0x001;
TR1 = 1; /* TR1: timer 1 run */
TI = 1;
while(1)
{
if(TF1==1)
j++;
if(j==20)
{
s++;
if(s==60)
{
s=0;
m++;
if(m==60)
{
h++;
m=0;
}
}
printf("the time is %d:%d:%d\n",h,m,s);
j=0;
}
}
}
void delay(void)
{
long int i;
for(i=0;i<50000;i++)
{
;
}}
A Little about the blog.....
This blog contains all the interview questions that i face during my interviews with various MNC's. I am trying to post the solutions for almost all the questions and i have covered few important topics which would be frequent topics in interviews. I hope the blog would be useful not only for the job aspirants but also to the one who tries to upgrade his/her knowledge in C and Embedded concepts......
please enter your mail address in the above mail address box to get the immediate updates of the new interview questions.....
please enter your mail address in the above mail address box to get the immediate updates of the new interview questions.....
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment