void delay_us(unsigned short time_us)
{
unsigned short delay_loops;
register unsigned short i;
delay_loops = (time_us+3)/5*CYCLES_PER_US; // +3 for rounding up (dirty)
// one loop takes 5 cpu cycles
for (i=0; i < delay_loops; i++) {};
}
The delay_loops calculation appears to be incorrect. If you pass in 1us, you get a delay_loops of 0 (1+3=4, 4/5=0, 0*16=0). For the Axon you really want something close to 16 cycles, or 3 loops (provided the 5 cycle per loop comment holds true).void delay_us(unsigned short time_us)
{
unsigned short delay_loops;
register unsigned short i;
delay_loops = ((time_us * CYCLES_PER_US)+3) / 5; // +3 for rounding up (dirty)
// one loop takes 5 cpu cycles
for (i=0; i < delay_loops; i++) {};
}
With this version, if you pass in 1us, you get a delay_loops of 3 (1*16=16, 16+3=19, 19/5=3), which equates to 15 cycles (closest possible to the 16 cycles per us on the Axon).In timer640.c:
Current version:Code: [Select]void delay_us(unsigned short time_us)
The delay_loops calculation appears to be incorrect. If you pass in 1us, you get a delay_loops of 0 (1+3=4, 4/5=0, 0*16=0). For the Axon you really want something close to 16 cycles, or 3 loops (provided the 5 cycle per loop comment holds true).
{
unsigned short delay_loops;
register unsigned short i;
delay_loops = (time_us+3)/5*CYCLES_PER_US; // +3 for rounding up (dirty)
// one loop takes 5 cpu cycles
for (i=0; i < delay_loops; i++) {};
}
Proposed new version:Code: [Select]void delay_us(unsigned short time_us)
With this version, if you pass in 1us, you get a delay_loops of 3 (1*16=16, 16+3=19, 19/5=3), which equates to 15 cycles (closest possible to the 16 cycles per us on the Axon).
{
unsigned short delay_loops;
register unsigned short i;
delay_loops = ((time_us * CYCLES_PER_US)+3) / 5; // +3 for rounding up (dirty)
// one loop takes 5 cpu cycles
for (i=0; i < delay_loops; i++) {};
}
Is this correct, or am I missing something?
delay_loops = (time_us+3)/5*CYCLES_PER_US;
Plus the overhead of it being in a subroutine ie pushing/popping various registers at the start/end of the code
input delay_loops Cycles (5*delay_loops) Theoretical us (16Mhz = cycles/16)
us old / new old / new old / new
1 = 0 / 3 0 / 15 0 / 0.9375
2 = 16 / 7 80 / 35 5 / 2.1875
3 = 16 / 10 80 / 50 5 / 3.125
4 = 16 / 13 80 / 65 5 / 4.0625
5 = 16 / 16 80 / 80 5 / 5
6 = 16 / 19 80 / 95 5 / 5.9375
7 = 32 / 23 160 / 115 10 / 7.1875