PDA

View Full Version : Digital Encoder Problems



blittled
07-02-2009, 12:44 PM
I'm using the Boe-Bot Digital Encoder with 10k pull up resistors to 3.3V on the input line and the following code to read them:

Propeller



con
_clkmode = xtal1 + pll16x 'Adjusted for ProtoBoard
_xinfreq = 5_000_000
BaudRate = 9200
TxPin = |< 1
RxPin = |< 0

var
long CntStack[40]
Word counterL

obj
Comms : "JDCogSerial"

pub start | val, valHi, valLo
CommPtr := Comms.start(RxPin, TxPin, BaudRate) ' Start Encoder counting
cognew (Count(@counterL, 2),@CntStack)
val := Word[@counterL]
valHi := val/256
valLo := val - valHi*256
byte[@Buffer][ 0 ] := valHi
byte[@Buffer][ 1 ] := valLo
SendBuffer

PUB SendBuffer | index
index~
Comms.Str(string("!STBD"))
repeat until index == 2
Comms.tx(byte[@Buffer][index])
index++

Pub Count(cntrAddr, pin) | status
dira[pin] := 0
word [cntrAddr] := 0
status := ina[pin]
Repeat
waitpne(status, pin, 0)
status := ina[pin]
word [cntrAddr]++



On the receiving end instead of incremental numbers I get random large numbers like 50768, 1238, 17301 when using

Basic Stamp



mbuff VAR Byte(2)
DO
SERIN 8,84, [WAIT("STBD"),STR mbuff\2]
DEBUG DEC mbuff(0)*256 + mbuff(1)
LOOP


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
What electronics need - MORE POWER!!!!!!!

MagIO2
07-02-2009, 01:03 PM
con
_clkmode = xtal1 + pll16x 'Adjusted for ProtoBoard
_xinfreq = 5_000_000
BaudRate = 9200
TxPin = |< 1
RxPin = |< 0

var
long CntStack[40]
Word counterL

obj
Comms : "JDCogSerial"

pub start | val, valHi, valLo
CommPtr := Comms.start(RxPin, TxPin, BaudRate) ' Start Encoder counting
cognew (Count(@counterL, 2),@CntStack)
val := Word[@counterL]
valHi := val/256
valLo := val - valHi*256
byte[@Buffer][ 0 ] := valHi byte[@val][1]
byte[@Buffer][ 1 ] := valLo byte[@val][0]
SendBuffer

PUB SendBuffer | index
index~
Comms.Str(string("!STBD"))
repeat until index == 2
Comms.tx(byte[@Buffer][index])
index++

Pub Count(cntrAddr, pin) | status, mask
dira[pin] := 0 ' simply not needed, as cognew always starts a cog with dira set to 0 completely
word [cntrAddr] := 0

mask := |< pin
status := ina[pin] & mask
Repeat
waitpne(status, pinmask, 0)
status := ina[pin] & mask
word [cntrAddr]++


In red you see the problem! Waitpne expects a bitmask, but what you gave it is the bit-number, so actually it is watching the TxPin which is heavyly used by the serial interface.

PS: forgot something ... see the purple correction. Know the difference? ina[pin] is shifting bit number pin of ina to bit 0 of status.

Post Edited (MagIO2) : 7/2/2009 5:22:55 AM GMT