Newbie timing problem
PropNewbie
Posts: 10
Using the code below to get a simple 1khz toggle on a pin. I am getting values significantly different than expected. For a delay of 40000·I get 963.3 hz output. For the following delays I get
Delay····Expected (Hz)···· Actual (Hz)
40000······· 1000·············· 963.3
20000········2000··············1881.0
10000······· 4000············· 3551.0
The board is a Propeller demo board with the supplied 5Mhz chip.·Measurements taken·on a Fluke 8060A multimeter. I don't think it is the meter playing games - I tested it against the 1khz calibration output on my scope and it was spot on. (I trust the meter more than my super cheapo scope).
Can anybody shed any light on why the values should be off by so much?
Code below
###########
CON
· _CLKMODE = XTAL1 + PLL16X
· _XINFREQ = 5_000_000
·
PUB Main
· Toggle·
·· return
PUB Toggle | delayCnt
· dira[noparse][[/noparse]3]:=1
· delayCnt := 40000
· repeat
··· waitcnt(delayCnt+cnt)
··· !outa[noparse][[/noparse]3]
·
Delay····Expected (Hz)···· Actual (Hz)
40000······· 1000·············· 963.3
20000········2000··············1881.0
10000······· 4000············· 3551.0
The board is a Propeller demo board with the supplied 5Mhz chip.·Measurements taken·on a Fluke 8060A multimeter. I don't think it is the meter playing games - I tested it against the 1khz calibration output on my scope and it was spot on. (I trust the meter more than my super cheapo scope).
Can anybody shed any light on why the values should be off by so much?
Code below
###########
CON
· _CLKMODE = XTAL1 + PLL16X
· _XINFREQ = 5_000_000
·
PUB Main
· Toggle·
·· return
PUB Toggle | delayCnt
· dira[noparse][[/noparse]3]:=1
· delayCnt := 40000
· repeat
··· waitcnt(delayCnt+cnt)
··· !outa[noparse][[/noparse]3]
·
Comments
What you have is a fixed length pause in waitcnt() whereas what you want is something synchronised to a constant timing mark. What you're seeing is due to the effect of executing the internal code for 'repeat' and '!outa[noparse]/noparse'.
It allows the WAITCNT to wait for time points a specific number of clock ticks
apart regardless of the execution time of the intervening Spin statements.
I did read about that - it is in the WAITCNT section under "synchronized delay timing". It just did not seem to be the likely source. How many cycles do a single REPEAT and OUTA statement take? My calculations work out that to generate the observed error of 37 Hz for the 1kHz wave, the loop overhead and OUTA would have to take 1480 clock cycles. Is this reasonable?
I sure appreciate the advice and assistance
Sounds about right. The code is executing approximately 8 Spin bytecode instructions so that indicates around 185 cycles each, 46 PASM instructions per bytecode. That's in the right ballpark.