Delay loop in C
Discussion in "8051 Discussion Forum" started by Harrison Feb 18, 2009.
Wed Feb 18 2009, 10:36 pm
Hi all,
I am using Keil C and 8051 (12MHz Xtal). I am wondering how do I calucate the delay time. For example,
void delay (int ms)
{
char k;
while (ms--)
{
for (k=0, k < 120; k++);
}
}
thanks,
Harrison
I am using Keil C and 8051 (12MHz Xtal). I am wondering how do I calucate the delay time. For example,
void delay (int ms)
{
char k;
while (ms--)
{
for (k=0, k < 120; k++);
}
}
thanks,
Harrison
Thu Feb 19 2009, 02:15 am
I doubt you can calculate the delay fully, as you do not know exactly how
the compiler will code it.
The practical method is to time a known number of loops, then adjust
until it is correct.
the compiler will code it.
The practical method is to time a known number of loops, then adjust
until it is correct.
Thu Feb 19 2009, 02:18 pm
In C programs you cannot be sure of delay, coz it depends on compiler how it optimize the loops as soon as you make changes in the options the delay changes.
to test delay of the above loop try putting breakpoint before and after calling the function delay, and see how much time it consumed.
..
delay();
..
simulate on keil simulator with xtal 12Mhz. and see the results.
to test delay of the above loop try putting breakpoint before and after calling the function delay, and see how much time it consumed.
delay();
simulate on keil simulator with xtal 12Mhz. and see the results.
Sun Feb 22 2009, 01:23 am
Hi,
Thank you for your reply. But how do you measure the time. Don't tell me to use a watch timer. We are talking about ms or us.
Harrison
Thank you for your reply. But how do you measure the time. Don't tell me to use a watch timer. We are talking about ms or us.
Harrison
Sun Feb 22 2009, 02:39 am
Thank you for your reply. But how do you measure the time. Don't tell me to use a watch timer. We are talking about ms or us.Harrison
Use a stopwatch
but do a test that executes thousands of loops and divide the result.
For critical timing you may need to use a hardware timer.
Using a simulator you can count the number of instruction cycles used in a single loop.
Sun Feb 22 2009, 10:17 am
u'll have to use Timers for making exact delays..... it duznt matters whether u need delays of ms , us or even seconds...
below is a function of 1 second using timers.....
// the idea is to make a 50ms delay and run it 20 times as 20x50ms= 1000ms= 1sec
delay_1s() // timer of 1 sec
{
int d;
for(d=0;d<=20;d++)
{
TMOD=0x01;
TL0=0xFD;
TH0=0x04B;
TR0=1; // start timer.
while(TF0==0); // run until TF turns to 1
TR0=0; // stop timer
TF0=0; // reset the flag
}
}
below is a function of 1 second using timers.....
// the idea is to make a 50ms delay and run it 20 times as 20x50ms= 1000ms= 1sec
delay_1s() // timer of 1 sec
{
int d;
for(d=0;d<=20;d++)
{
TMOD=0x01;
TL0=0xFD;
TH0=0x04B;
TR0=1; // start timer.
while(TF0==0); // run until TF turns to 1
TR0=0; // stop timer
TF0=0; // reset the flag
}
}
[ Edited Sun Feb 22 2009, 10:20 am ]
Mon Feb 23 2009, 06:15 pm
well i told you to run under simulator and look at timer. like in keil as shown in fig below.
Fri Dec 09 2011, 03:02 am
well i told you to run under simulator and look at timer. like in keil as shown in fig below.Ajay Bhargav
how can i use the simulator i didn't catch it
please can you explain it in more details
thank you
Sun Dec 11 2011, 01:35 am
@ fares
use debug mode of keil
in debug mode automatically this window comes
by using F10 u can move next function and by using F11 u can move line by line
F10= step over
F11= step in
use debug mode of keil
in debug mode automatically this window comes
by using F10 u can move next function and by using F11 u can move line by line
F10= step over
F11= step in
Fri Dec 16 2011, 03:26 am
check videos in tutorial section on how to use keil... that might help you better
Powered by e107 Forum System