> > If I'd want a 1 mSec delay and smclk/mclk = 8MHz
> > What should mS be??
> >
> > void delay_ms(int del) { //This routine is
> > approx 1mS
> > delay @ 8 MHz
> > unsigned int ctr;
> > while(del--)
> > for (ctr=0; ctr < mS; ++ctr)
> > ;
> > }
> Depends upon how your compiler codes the loop, for
a start, and
whether
> you want to take interrupts into account or not...
One assumes that
you
> can time the loop for a given value of mS using a
scope, or
> hand-calculate the value of mS given the instructions in the loop.
Yes, a good way to get the value is measuring it with a scope.
But if you change something, for example
- more or longer interrupts
- clock frequency
- different compiler (or new version with better optimization,
some compilers even optimize empty loops totally away)
you may have to re-adjust the loop.
So, sometimes it is better to use a timer (if you have one).
Assume Timer_A counting in "Continuous Mode" with 32768Hz:
void delay_ms(unsigned short ms)
{
unsigned short tar, tar0, clk;
clk = ms * 32; // 32 Timer_A clocks are ca. one ms; ms < 2048!
// or exactly: clk = (32768ul*ms) / 1000; // ms < 2000!
while((tar0=TAR) != TAR); // read TAR until stable
do
while((tar=TAR) != TAR); // read TAR until stable
while((unsigned short)(tar-tar0) < clk);
}
Another way is to let a compare register (TACCRx) set a flag when
time is over.
Wolfgang
Hi,
it depends on the compiler, compiler options, interrupts etc. and therefore you
should test it by measuring the time 10,000 delay_ms needs:
const unsigned int mS24;
unsigned long int i;
for (i=0;i<10000;i++)
{
delay(1);
}
You can estimate the correct value of mS with binary search or calculating, but
you have to take into account the time a function call needs (and temperature
and voltage dependence of the clock) if you need the best fitting value.
Regards
Rolf
Reply by Paul Curtis●May 14, 20032003-05-14
Martijn,
> If I'd want a 1 mSec delay and smclk/mclk =
8MHz
> What should mS be??
>
> void delay_ms(int del) { //This routine is
> approx 1mS
> delay @ 8 MHz
> unsigned int ctr;
> while(del--)
> for (ctr=0; ctr < mS; ++ctr)
> ;
> }
Depends upon how your compiler codes the loop, for a start, and whether
you want to take interrupts into account or not... One assumes that you
can time the loop for a given value of mS using a scope, or
hand-calculate the value of mS given the instructions in the loop.
-- Paul.
Reply by Martijn Broens●May 14, 20032003-05-14
Hi all,
If I'd want a 1 mSec delay and smclk/mclk = 8MHz
What should mS be??
void delay_ms(int del) { //This routine is approx 1mS
delay @ 8 MHz
unsigned int ctr;
while(del--)
for (ctr=0; ctr < mS; ++ctr)
;
}
thanks,
Martijn