Timing Question
ajward
Posts: 1,130
I started going back through the PE Kit book tweaking and twiddling with the code to see what happens and looking at the waveforms. The code for LedOnOffP4 made a decent square wave.
I ran this sample to approximate a 1 kHz signal....
When I looked at the waveform on the scope, the signal was about 1.22 mS. Ran the code on 4 propellers and the results were very similar... 1.21 to 1.23 mS.
I'm thinking the extra time comes from the overhead of the square wave being generated in software. Am I close or way off base?
Anyhow... thanks for any advice!!
Amanda
Edit: In my early morning, caffeine not yet kicked in yet haze, I confused kHz with mS. Problem's the same... just different! ;-)
I ran this sample to approximate a 1 kHz signal....
PUB One_kHz_Output dira[4] := 1 repeat outa[4] := 1 waitcnt(clkfreq/2000 + cnt) outa[4] := 0 waitcnt(clkfreq/2000 + cnt)
When I looked at the waveform on the scope, the signal was about 1.22 mS. Ran the code on 4 propellers and the results were very similar... 1.21 to 1.23 mS.
I'm thinking the extra time comes from the overhead of the square wave being generated in software. Am I close or way off base?
Anyhow... thanks for any advice!!
Amanda
Edit: In my early morning, caffeine not yet kicked in yet haze, I confused kHz with mS. Problem's the same... just different! ;-)
Comments
You have to account for the overhead in spin and have something that is not skewed by the time it takes the interpreter to execute the spin instructions and translate them into machine code. With this you have a single starting point and you're always adding clk/2000 to it every time, so it's not being skewed by the additional ~.22kHz to execute the code and then move to the next wait command, it instead just builds off of the initial creating a perfect wait of clkfreq/2000 + the last which is based on the initial, every single time.
Say you have to wait 1000 cnt to wait one second.
Loop control: 20 cnt.
set up the wait: 20 cnt.
wait the 1000 cnt.
You have now paused for 1040 cnt.
So if you instead set up a wait so it always look the next 'even' 1000 cnt
What you did between these waits does not matter, as long they NEVER take more than a 1000 cnt to do.
a=cnt+1000
loop:
wait for cnt=a
do my thing
a=a+1000
jmp loop
If you are running through the PEK again, go to page 64 in the Ver 1.2 PDF.
It is discussed there.
Jim