Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski 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
Joined
Jun 8, 2005
Messages
30
Location
CA
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
 
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:
 
Not to mention that what code the compiler creates will depend upon what optimizations you have enabled, as well as the compiler brand (to a lesser extent, hopefully).


Dan
Owner
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top