View previous topic :: View next topic |
Author |
Message |
shiva Guest
|
bug in delay_us() |
Posted: Sat Mar 12, 2005 4:57 pm |
|
|
Hi,
I have a bug in delay_us(x).
In below sample program when the delay_us(x) was used (where x is variable<255 and initalized at 12) the obtained delay value is 287us in stopwatch which is wrong. whereas when the delay_us(12) was used the obtained delay value is 12us on stopwatch which works corrrectly. Can anyone please explain me why is it happening in delay_us() function.Is there any bug in delay_us() function.Whereas delay_ms() function works correctly in both the cases.
please reply asap.
bye.
#include <16F876.h>
#device 16F876 ICD=TRUE
#use delay(clock=4000000)
#fuses XT,NOWDT, NOPROTECT, NOPUT, NOBROWNOUT
#define pin0 56
main()
{
int x;
x=12;
while(1)
{
output_high(pin0);
delay_us(x);
output_low(pin0);
output_high(pin0);
delay_us(12);
output_low(pin0);
}
} |
|
|
Charlie U
Joined: 09 Sep 2003 Posts: 183 Location: Somewhere under water in the Great Lakes
|
|
Posted: Sat Mar 12, 2005 5:33 pm |
|
|
Which version of the compiler are you using? There have been a few problems with the delays in past versions.
Also, are you certain that your oscillator/crystal is running at 4MHz? |
|
|
bluetooth
Joined: 08 Jan 2005 Posts: 74
|
|
Posted: Sat Mar 12, 2005 5:33 pm |
|
|
I built this with 3.219 (you didn't, and should have, stated your version).
Try looking at the list file.... the compiler handles variables and constants *way* differently... you're only executing 1 instruction per microsecond, and the differences in the way the delay is done probably account for the extra 31 uSecs....
Try using 250 for x and the fixed value - I'd guess the delta will still be about 30 uSecs.
Solutions: use the variable approach for both and tune for your needs or crank up your processor speed to make the error smaller.
Finally - be careful with consecutive output_high/output_low instructions, especially if you speed up the processor. The reason for this caution can be found on this forum....
Good luck! |
|
|
shiva
Joined: 12 Mar 2005 Posts: 2
|
bug in delay_us |
Posted: Sun Mar 13, 2005 2:02 am |
|
|
Sorry i forgot about the version i used.Its v3.0.0.13 |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sun Mar 13, 2005 8:16 am |
|
|
That's not a valid version number. Look at the top of the LST file. |
|
|
|