Discussion in "8051 Discussion Forum" started by    Harrison    Feb 18, 2009.
Wed Feb 18 2009, 10:36 pm
#1
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
Thu Feb 19 2009, 02:15 am
#2
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.
Thu Feb 19 2009, 02:18 pm
#3
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.
Sun Feb 22 2009, 01:23 am
#4
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
Sun Feb 22 2009, 02:39 am
#5


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
#6
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
}
}


[ Edited Sun Feb 22 2009, 10:20 am ]
Mon Feb 23 2009, 06:15 pm
#7
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
#8


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
#9
@ 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
Fri Dec 16 2011, 03:26 am
#10
check videos in tutorial section on how to use keil... that might help you better

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

ArnoldDiant
Fri Apr 26 2024, 03:53 am
RodneyKnorb
Thu Apr 25 2024, 07:08 pm
Williamjef
Thu Apr 25 2024, 02:08 pm
SamuelSmise
Thu Apr 25 2024, 09:56 am
DustinErele
Thu Apr 25 2024, 08:44 am
ztaletpzca
Wed Apr 24 2024, 11:19 pm
IrardlPex
Wed Apr 24 2024, 08:42 pm
Charlestehed
Wed Apr 24 2024, 05:20 pm