×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

writing code for delay rountine using C

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

RE: writing code for delay rountine using C

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!

RE: writing code for delay rountine using C

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: www.essexsteam.co.uk

RE: writing code for delay rountine using C

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
http://www.Hi-TecDesigns.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources