AJM
07-08-2009, 08:50 PM
I am working on my first project and the first goal I have is to be able to accurately measure a sensors frequency. Before I hook my prop to any sensor, I want to make sure the code actually works correctly.
In the attached code I am using a counter to act as the sensor, outputting a frequency on pin 14. The signal then goes through a buffer circuit. Counter B is set up as a negative edge detector on the output of the buffer and inputs to pin 10. The program then displays the frequency on the serial terminal.
I do not have a scope but my multimeter does have a frequency setting. When connected to the output of the buffer (in this case) it reads 99.98 Hz. However, the output on the serial terminal can read anywhere from 100 to 104 Hz.
Because the output frequency is high (over 100 Hz), I think the extra cycles are being counted while the spin commands are being ran. Is this correct or am I way off in the programming/technique I am using here?
Any advise from the people here is always appreciated.
CON
_clkmode = xtal1 + pll16x ' Set up clkfreq = 80 MHz.
_xinfreq = 5_000_000
HOME = 1, CR = 13, CLS = 16, CRSRX = 14, CLREOL = 11
OBJ
Debug : "FullDuplexSerialPlus"
PUB Init
ctra[30..26] := %00100 ''
ctra[5..0] := 14
frqa := 5_369 '' 100 Hz ( frqa = freq*2^32 / clkfreq )
dira[14] := 0 '' Pin = Input
ctrb[30..26] := %01110 '' Negative Edge Detector
ctrb[8..0] := 10
frqb~
phsb~
Debug.start(31, 30, 0, 57600)
waitcnt(clkfreq*5 +cnt)
Debug.tx(CLS)
frqb := 1
Main
PUB Main | frequency
repeat
dira[14] := 1 '' Pin = Output
phsb~
waitcnt(clkfreq + cnt)
!dira[14]
frequency := phsb
Debug.str(String(HOME, "Frequency = "))
Debug.Dec(frequency)
Debug.tx(CR)
In the attached code I am using a counter to act as the sensor, outputting a frequency on pin 14. The signal then goes through a buffer circuit. Counter B is set up as a negative edge detector on the output of the buffer and inputs to pin 10. The program then displays the frequency on the serial terminal.
I do not have a scope but my multimeter does have a frequency setting. When connected to the output of the buffer (in this case) it reads 99.98 Hz. However, the output on the serial terminal can read anywhere from 100 to 104 Hz.
Because the output frequency is high (over 100 Hz), I think the extra cycles are being counted while the spin commands are being ran. Is this correct or am I way off in the programming/technique I am using here?
Any advise from the people here is always appreciated.
CON
_clkmode = xtal1 + pll16x ' Set up clkfreq = 80 MHz.
_xinfreq = 5_000_000
HOME = 1, CR = 13, CLS = 16, CRSRX = 14, CLREOL = 11
OBJ
Debug : "FullDuplexSerialPlus"
PUB Init
ctra[30..26] := %00100 ''
ctra[5..0] := 14
frqa := 5_369 '' 100 Hz ( frqa = freq*2^32 / clkfreq )
dira[14] := 0 '' Pin = Input
ctrb[30..26] := %01110 '' Negative Edge Detector
ctrb[8..0] := 10
frqb~
phsb~
Debug.start(31, 30, 0, 57600)
waitcnt(clkfreq*5 +cnt)
Debug.tx(CLS)
frqb := 1
Main
PUB Main | frequency
repeat
dira[14] := 1 '' Pin = Output
phsb~
waitcnt(clkfreq + cnt)
!dira[14]
frequency := phsb
Debug.str(String(HOME, "Frequency = "))
Debug.Dec(frequency)
Debug.tx(CR)