View Full Version : Simple_Serial.spin Bug

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.

09-11-2007, 07:05 AM
Yes that IS a bug! It says on p. 286/287 of the Manual:

"All local variables ....are left uninitialized upon each call to the method."

Ken Peterson
09-11-2007, 08:07 AM
hmmm....I was having problems with Simple Serial giving me garbage and I thought it was because my code wasn't keeping up with the input data rate. I'll have to go back and revisit that.....


The more I know, the more I know I don't know. Is this what they call Wisdom?