writing code for delay rountine using C
writing code for delay rountine using C
(OP)
Hi guys, got a brain teaser for you. I am trying to write code for a delay routine. Could someone tell me for a PIC16f84 @ 4MHz how long the lenght it takes to execute one line of code. I just used a "for" loop for the dealy and 1us for each instruction and put in the appropriate value but it is when longer than calculated. THis is what i have done:
-@ 4MHz (time for one instruction= 1/(4MHz/4)= 1us
-for one second delay--> 1sec/1us= 1000000 cycles
-1000000/2= 500000-->7A120hex
void deley(void)
{
int i;
for(i=0;, i<7A120;)
{
i++;
}
return;
}
but this ends up being like 10+ seconds. COould someone please point out the error?
Thanks
-@ 4MHz (time for one instruction= 1/(4MHz/4)= 1us
-for one second delay--> 1sec/1us= 1000000 cycles
-1000000/2= 500000-->7A120hex
void deley(void)
{
int i;
for(i=0;, i<7A120;)
{
i++;
}
return;
}
but this ends up being like 10+ seconds. COould someone please point out the error?
Thanks





RE: writing code for delay rountine using C
Also, every instruction does not execute in one clock cycle. If you look through the documention for your device, it should provide some information regarding the assembly instructions supported. Along with that information, will be the number of clock cycles it takes to execute each instruction.
Hope this helps!
RE: writing code for delay rountine using C
However a pragmatic solution is to divide &H7A120 by 10+ (try maybe &HC350) and use that
Good Luck
johnwm
________________________________________________________
To get the best from these forums read FAQ731-376 before posting
Steam Engine enthusiasts: www.essexsteam.co.uk
RE: writing code for delay rountine using C
Dan
Owner
http://www.Hi-TecDesigns.com