Home - Search - Members
Full Version: A little bit of a problem with while loop..
Judgement
Jul 2 2008, 10:53 PM
How do I use the internal timer in the 8051 for my programs? I need to use it to send a delayed SMS.


edit: I've changed the thread title. Before, I was asking on how to use the internal timer.
sashijoseph
Jul 3 2008, 5:28 AM
You would need to first initialise certain registers.
The TMOD register has to be 1st loaded with a value indicating which(of the 4) modes the timer will use.
If 16 bit mode(mode 1) is used,the TH and TL registers are to be loaded with an initial value from which the up count starts.In 16 bit mode the timer can count from 0x00 to 0xffff.So say you load TH and TH with 0x2B and 0x2C respectively,then the timer will start counting from 0x2B2C to 0xFFFF.
Now each count represents some fixed amount of delay(depending on the xtal),say d.Thus the total delay would be the total no. of counts * d.In the above case ( FFFF-2B2C) * d.
d can be found by dividing the xtal frequency by 12 and dividing 1000000 by the derived value to arrive at 'd' in micro seconds.
For eg. with a 11059200Hz xtal.....
11059200/12= 921600.
1000000/921600 = 1.085 us

Thus after every 1.085us the timer will increment.
Plz read the datasheet for more info.
Judgement
Jul 3 2008, 5:49 AM
Thanks. Uh..about the datasheet..I don't see it.
sashijoseph
Jul 3 2008, 8:34 AM
Yes you're right... the Atmel datasheet doesn't have it.
Use this tut instead http://www.8052.com/tuttimer.phtml
Judgement
Jul 6 2008, 5:08 AM
Thanks. But now I have another problem. I can't end my while loop using return(1);
It always gives me an error. Is there any way I can solve this? Or is there any alternative I can take?

void read_sms(void)
{
int r,y;
r=0; y=0;

while(y!=1)
{
int r=0;
for(r=0;r<13;r++)
{
tx0(readsms[r]);
}
}
}


How can I stop the while loop? Thanks..
pdi33
Jul 6 2008, 5:25 AM
well, u have to set the integer y to 1 to come out of the loop. The condition would be that u do that within the read_sms() function as the variable is local and will not be modified but instead stored and regained if u try setting that variable in the interrupt routine. The best thing would be to define the variable outside the routine (global variable and i think ur problem would be solved.

Ps.
if ur tx0() function is returning a 1 then just modify ur instruction to:
y = tx0(readsms[r]);
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Rickey's World © 2003 - 2007