Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing code for delay rountine using C

Status
Not open for further replies.

shamone

Electrical
Jun 8, 2005
30
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
 
Replies continue below

Recommended for you

You failed to take the FOR loop instructions into considerations (compare and branch instructions). Look at the actually assembled code and do your calculations that way.

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!
 
Melone is correct - check the assembled code and the clock cycles for each instruction.

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:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor