How to do this simple task
PVJohn
Posts: 60
I'm trying to TOGGLE PIN 1 after 3 counts at PIN 0, but I'm having a trouble with PBASIC. Please help.
' {$STAMP BS2px}
' {$PBASIC 2.5}
SpeedIN········ PIN···· 0········· ' Input on P0
SpeedOUT····· PIN···· 1········· ' OUTput on P1
DIR1 = 1····························· ' pin 1 in output mode
SpeedOUT = 0····················· ' pin 1 output driver low
OneSec······ CON···· $37B··········· ' / 0.287
Capture······CON···· 1000··········· ' 1 second
cycles········· VAR···· Word··········· ' counted cycles
Main:
DO
··· COUNT SpeedIN, (Capture */ OneSec), cycles
····· IF SpeedIN = 3 THEN
······· TOGGLE 1
······· SpeedIN = 0
····· ELSE
········ IF SpeedIN < 3 THEN Main
····· END IF
LOOP
END
' {$STAMP BS2px}
' {$PBASIC 2.5}
SpeedIN········ PIN···· 0········· ' Input on P0
SpeedOUT····· PIN···· 1········· ' OUTput on P1
DIR1 = 1····························· ' pin 1 in output mode
SpeedOUT = 0····················· ' pin 1 output driver low
OneSec······ CON···· $37B··········· ' / 0.287
Capture······CON···· 1000··········· ' 1 second
cycles········· VAR···· Word··········· ' counted cycles
Main:
DO
··· COUNT SpeedIN, (Capture */ OneSec), cycles
····· IF SpeedIN = 3 THEN
······· TOGGLE 1
······· SpeedIN = 0
····· ELSE
········ IF SpeedIN < 3 THEN Main
····· END IF
LOOP
END
Comments
testIn·· VAR·· Bit
oldIn····VAR·· Bit
Main:
· DO
··· testIn = SpeedIn ^ oldIn & SpeedIn
··· oldIn =·testIn
··· cycles = cycles + testIn
··· IF (cycles = 3) THEN
····· TOGGLE SpeedOut
····· cycles =·0
··· ENDIF
· LOOP
The first line scans the input and watches for a 0-to-1 change on the input, setting testIn to 1 when (and only when) that happens.· The rest is pretty straightforward.·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
PVJohn