Frequency Errors
John Abshier
Posts: 1,116
I am getting frequencies that I don't understand. At low frequencies the error is 5 percent. At higher frequencies the error is 1.5 percent. Here is the code:
John Abshier
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
#1, HOME, #8, BKSP, TAB, LF, CLREOL, CLRLB, CR, #16, CLS ' PST formmatting control
VAR
Pub Main | i
dira[0..1]~~ ' Make pins 0 and 1 outputs
outa[0]~~ ' High
outa[1]~ ' Low
i := cnt
repeat
i += clkfreq / 10
!outa[0..1] ' Toggle
waitcnt(i)
' clkfreq / 2 -> period 1.05 clkfreq / 10-> period 0.21 both 5 percent from expected.
' Above values tested on PDB and Demo Board. Lower values only tested on Demo Board
' clkfreq / 100 -> period 19.7 or 20.3 1.5 % clkfreq / 1000 -> period 1.97 or 2.03 1.5 %
' clkfreq / 10000 -> period 197 or 203 uS 1.5% error
John Abshier

Comments
Are you taking into account the time required for each spin instruction?
There is some simplification that you can do, but you will still have a small amount of delay based on the instructions that are called.
...and are you sure it's not the other way around? ... it seems that at lower frequencies the error should be less pronounced than at higher frequencies. I don't have anything to test right in front of me at the moment to confirm that however.
This
repeat i += clkfreq / 10 !outa[0..1] ' Toggle waitcnt(i)Could be simplified to something like this...
repeat !outa[0..1] ' Toggle waitcnt(cnt + clkfreq / 10)The error being bigger at lower frequencies is counter intutive. If someone gets different results, I will rerun and post PropScope images. I guess that I could have a bad PropScope.
John Abshier
John Abshier
Hanno