hippy
09-11-2007, 05:57 AM
It took hours to locate, so I'd best save others the trouble ...
PUB rx | t, b
'' Receive a byte; blocks program until byte received
if started and rxOkay
b := 0 ' <=== ADD THIS
dira[sin]~ ' make rx pin an input
waitpeq(inverted & |< sin, |< sin, 0) ' wait for start bit
t := cnt + bitTime >> 1 ' sync + 1/2 bit
repeat 8
waitcnt(t += bitTime) ' wait for middle of bit
b := ina[sin] << 7 | b >> 1 ' sample bit
waitcnt(t + bitTime) ' allow for stop bit
return (b ^ inverted) & $FF ' adjust for mode and strip off high bits
If the 'b:=0' isn't added, you'll get corrupt characters under some circumstances, but not all. I presume it depends on how the stack has been used before rx is called and that local variables are not initialised to zero. I'd also recommend adding a 'waitpne(inverted & |< sin, |< sin, 0)' before the waitpeq just to make sure the line has returned to idle before looing for the start bit.
PUB rx | t, b
'' Receive a byte; blocks program until byte received
if started and rxOkay
b := 0 ' <=== ADD THIS
dira[sin]~ ' make rx pin an input
waitpeq(inverted & |< sin, |< sin, 0) ' wait for start bit
t := cnt + bitTime >> 1 ' sync + 1/2 bit
repeat 8
waitcnt(t += bitTime) ' wait for middle of bit
b := ina[sin] << 7 | b >> 1 ' sample bit
waitcnt(t + bitTime) ' allow for stop bit
return (b ^ inverted) & $FF ' adjust for mode and strip off high bits
If the 'b:=0' isn't added, you'll get corrupt characters under some circumstances, but not all. I presume it depends on how the stack has been used before rx is called and that local variables are not initialised to zero. I'd also recommend adding a 'waitpne(inverted & |< sin, |< sin, 0)' before the waitpeq just to make sure the line has returned to idle before looing for the start bit.