PDA

View Full Version : Frequency Errors



John Abshier
08-08-2010, 05:24 PM
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:

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

Beau Schwabe
08-08-2010, 05:37 PM
John,

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)

John Abshier
08-08-2010, 06:00 PM
The first code should account for delays due to Spin code execution. This is discussed in the PE Kit Labs.

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
08-08-2010, 09:18 PM
I did some more playing and the key is the horizontal time scale. For a 100 Hz signal, selecting 10ms for horizontal time gives a 2% error. Selecting 2ms for horizontal time gives 0 error.

John Abshier

Hanno
08-09-2010, 12:54 AM
The propscope measures frequencies using a state machine that looks for rising/falling flanks in the pc application. For best accuracy, you should have ~2 periods on screen.
Hanno