delay calculations

Status
Not open for further replies.

AtomSoft

Well-Known Member
I made this code and wanted to know if the math is correct. It seems ok to me tho but would like another opinion.

Code:
#include <delays.h>
#define d_mhz 8000000

void delay_s(char sec);
void delay_ms(char msec);
void delay_us(char usec);

char d_tmp;              //delay calculation temp
char lt,lt2;             //loop temp

void delay_s(char sec)
{
     d_tmp = (d_mhz / 4) / 10000;

     for(lt=0;lt<sec;lt++)
          Delay10KTCYx(d_tmp);
}
void delay_ms(char msec)
{
     d_tmp = (d_mhz / 4) / 1000000;

     for(lt=0;lt<msec;lt++)
          Delay1KTCYx(d_tmp);
}
void delay_us(char usec)
{
     d_tmp = (d_mhz / 4) / 1000000;

     for(lt=0;lt<usec;lt++) {
          for(lt2=0;lt2<d_tmp;lt2++)
              Delay1TCY();
     }
}
 
Last edited:
I found this thread when doing a search of the forum, but thought I'd add my 2 cents.....

Your math seems fine, but you don't take into account the overhead for the looping and function calls. Hard to say by how much that will throw it off, as it's much harder to count instruction cycles in a high level language vs ASM.

But the functions are useful! It depends on your application.

Brian
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…