Shop OBEX P1 Docs P2 Docs Learn Events
Code snippet: BS2 series; serial port parameters using constants — Parallax Forums

Code snippet: BS2 series; serial port parameters using constants

ArchiverArchiver Posts: 46,084
edited 2001-10-03 15:57 in General Discussion
Hi fellow Stampers,
I've been fiddling on a few projects with my BS2e and BS2sx that use serial
I/O. Serial I/O is pretty well documented in the Basic Stamp programming
manual; however "getting it right" can be daunting for even moderately
experienced programmers.

The following "snippet" of code will do the "grunt work" of figuring out the
correct commands to configure your Stamp for serial I/O; it also makes it a
snap to change ports at a later date. Since I'm using constants, no Stamp
memory or variables are used in your Stamp

'
begin code snippet: serial.bas
'
' Serial port constants & calculations

OutPort CON 10 ' Constant: Output port to send messages to
InPort CON 11 ' Constant: Input port to read user data from
DesBaud CON 24 ' Desired baud rate/100; 3=300, 6=600, 12=1200,24=2400
BS2 CON 10000 ' Constant for BS2, BS2e ***
BS2sxp CON 25000 ' Constant for BS2sx, BS2p ***
Parity CON 0 ' Use 0 for 8-bit, no parity; 8192 for 7-bit, even parity
Polarity CON 16384 ' Use 0 for noninverted, 16384 for inverted
DrivenOpen CON 0 ' Use 0 for driven, 32768 for open
SerPortParm CON BS2sxp / DesBaud -20 | Parity | Polarity | DrivenOpen '
Calc port parms
'
end code snippet: serial.bas
'

Sample use:
' 111111 output port bits
' 54321098
dirh = %00000100 ' Set direction of upper 8 ports data flow; 10=output,
all others input

serout OutPort,SerPortParm,[noparse][[/noparse]"Syntax:",10,CR]
serout OutPort,SerPortParm,[noparse][[/noparse]"prompting message",10,CR]
serin InPort,SerPortParm,[noparse][[/noparse]DEC InputNum] ' Get orders from terminal.

Enjoy!
Steve
Sign In or Register to comment.