Handy dandy event catcher pw measurer
RichardF
Posts: 168
In my continual playing with the fascinating ctrx registers, I offfer the following little gem:
'MeasPulseIn.spin
{wait for positive pulse into pin, then measure pulse width in microseconds}
CON
_clkmode = xtal1 + pll16x················
_xinfreq = 5_000_000
LCD_PIN·· = 27········································ ' for Parallax 4x20 serial LCD on A0
LCD_BAUD· = 19_200
LCD_LINES = 4
pin = 14··················································'input pin
OBJ
lcd : "debug_lcd"
VAR
long stack1[noparse][[/noparse]40], pw
PUB start
if lcd.start(LCD_PIN, LCD_BAUD, LCD_LINES)············· ' start lcd
··· lcd.cursor(0)·················································· ·' cursor off
··· lcd.backLight(false)······································ ··· ' backlight on (if available)
··· lcd.cls···························································· ' clear the lcd
··· lcd.str(string(" ", 6))
cognew(getPulse, @stack1)···
repeat
· updateLcd(pw,0,0)
PUB getPulse
ctra[noparse][[/noparse]30..26] := %01000··················· 'set ctra to add frqa to phsa while pin is positive
ctra[noparse][[/noparse]5..0] := pin····························· 'input pin
frqa := clkfreq/1_000_000·················'add microseconds to phsa
repeat
· phsa := 0
· waitpeq(|< pin, |< pin, 0)·············· 'wait for pos pulse in
· waitpne(|< pin, |< pin, 0)·············· 'wait for pos pulse to clear
· pw := phsa··································'phsa holds pulse width in microseconds
PUB updateLcd(value,x,y)
lcd.gotoxy(x,y)
lcd.decf(value,12)
Would appreciate·it if someone with an accurate square wave generator would check this out as to how short a pulse width can be measured. More ctrx fun in the works·
Richard···
Post Edited (RichardF) : 6/29/2007 1:19:04 AM GMT
'MeasPulseIn.spin
{wait for positive pulse into pin, then measure pulse width in microseconds}
CON
_clkmode = xtal1 + pll16x················
_xinfreq = 5_000_000
LCD_PIN·· = 27········································ ' for Parallax 4x20 serial LCD on A0
LCD_BAUD· = 19_200
LCD_LINES = 4
pin = 14··················································'input pin
OBJ
lcd : "debug_lcd"
VAR
long stack1[noparse][[/noparse]40], pw
PUB start
if lcd.start(LCD_PIN, LCD_BAUD, LCD_LINES)············· ' start lcd
··· lcd.cursor(0)·················································· ·' cursor off
··· lcd.backLight(false)······································ ··· ' backlight on (if available)
··· lcd.cls···························································· ' clear the lcd
··· lcd.str(string(" ", 6))
cognew(getPulse, @stack1)···
repeat
· updateLcd(pw,0,0)
PUB getPulse
ctra[noparse][[/noparse]30..26] := %01000··················· 'set ctra to add frqa to phsa while pin is positive
ctra[noparse][[/noparse]5..0] := pin····························· 'input pin
frqa := clkfreq/1_000_000·················'add microseconds to phsa
repeat
· phsa := 0
· waitpeq(|< pin, |< pin, 0)·············· 'wait for pos pulse in
· waitpne(|< pin, |< pin, 0)·············· 'wait for pos pulse to clear
· pw := phsa··································'phsa holds pulse width in microseconds
PUB updateLcd(value,x,y)
lcd.gotoxy(x,y)
lcd.decf(value,12)
Would appreciate·it if someone with an accurate square wave generator would check this out as to how short a pulse width can be measured. More ctrx fun in the works·
Richard···
Post Edited (RichardF) : 6/29/2007 1:19:04 AM GMT
Comments
Thanks for sharing that code. In the getPulse function you add 80 (microseconds) to frqa - I'm not sure why, as each clock cycle in this case has a period of 12.5 nanoseconds (1/80mHz). Your code does work, but does not appear to read in microseconds...
I have made a slight modification to it so it reads in nanoseconds * 10. The "* 10" is so you can read to 1 decimal place without using floating point.
I tried it out with my function gen which can only go as low as 180nS... the prop measures and displays this no problems!
...Update - got it down to 37.5 nS!
Cheers,
Shane.
Post Edited (Shane De Catania) : 6/29/2007 5:58:26 AM GMT
Thanks for taking the time to check it out and improve the code.
Richard