View Full Version : BS2 serin/serout

01-19-2006, 02:11 AM
can anyone suggest any information on using serin/serout or sample code. I am trying to input a 12 bit binary serial data from an external device and display a decimal output on a parallax serial LCD display.

Jon Williams
01-19-2006, 02:15 AM
What does the stream from the device look like? Is it binary (would be two bytes to hold twelve bits)? If so, in what order do the bytes arrive? Or, is the information sent as a text string? If the latter, the BASIC Stamp has conversion functions that will work at 9600 baud and lower.

And, as always, reading the sections on SERIN and SEROUT in the manual (their detailed) will go a long way toward developing your understanding of these commands. The manual (and help file) have several code snippets to demonstrate PBASIC features.

Jon Williams
Applications Engineer, Parallax

Andy Lindsay (Parallax)
01-19-2006, 02:27 AM
Use the DEC operator. Assuming you are using a BASIC Stamp 2 and your serial LCD is connected to P14, the code might look something like this:

· value VAR Word
· SEROUT 14, 84, [22, 12]

· DO

··· GOSUB Get_12_Bit_ADC_Value
··· SEROUT 14, 84, [128, DEC5 value]
··· PAUSE 250

··· .
··· .

There are more tips and tricks of a similar sort in this post: Getting Started with the Parallax Serial LCD (http://forums.parallax.com/showthread.php?p=531520).

Post Edited (Andy Lindsay (Parallax)) : 1/18/2006 7:34:08 PM GMT

01-19-2006, 02:52 AM
the format is MSB sent first then LSB

MSB (B7 [1] indicates·MSB - B6 EVEN PARITY - B5-B0 output of A/D convereter)

LSB (B7 [0]·indicates LSB - B6 EVEN PARITY - B5-B0 output of A/D convereter)

i.e. 1001 0110·· 0101 0101 equals -21.2 in decimal

Post Edited (gc3076) : 1/18/2006 8:21:39 PM GMT

Andy Lindsay (Parallax)
01-19-2006, 05:07 AM
It looks like it should be doable in PBASIC. I wrote a subroutine to receive the bytes and parse the bits, but I'm not seeing how %010110_010101 correpsonds to -21.2? Also, links to the documentation for the ADC you are using would be helpful.

Post Edited (Andy Lindsay (Parallax)) : 1/18/2006 11:24:39 PM GMT

01-19-2006, 09:01 PM
here is a link to the rs232/12 bit communication protocol


01-20-2006, 12:23 AM

Jon Williams
01-20-2006, 12:37 AM
This is off the top of my head after looking at the data sheet (and assumes a BS2; see our template for conditional compilation directives to set the baudmode value for any BS2-family module).

Here's what the Get_Sensor routine does: It waits for two bytes and then checks the high bit of the MSB to make sure that the bytes are properly aligned.· If that's the case, then the word variable is reassembled from the two bytes that contain six data bits each.· Please study the data shee carefully and compare it to this code.

Sensor···· ·PIN··· 15

SnsrBaud··· CON··· 84

result···· ·VAR··· Word


· SERIN Sensor, SnsrBaud, [result.HIGHBYTE, result.LOWBYTE]
· IF (result.BIT15 = 0) THEN Get_Sensor
· result.BIT6 = result.BIT8
· result.BIT7 = result.BIT9
· result.HIGHBYTE = (result.HIGHBYTE & %00111111) >> 2

Jon Williams
Applications Engineer, Parallax

01-20-2006, 01:12 AM
that's it, thanks so much for the help !!

Andy Lindsay (Parallax)
01-20-2006, 04:53 AM

Here is a test program (TestSpectroTilt3.bs2 (http://forums.parallax.com/attachment.php?attachmentid=40154)) to try with your·SPECTROTILT module and Parallax Serial LCD.· Make sure to update the Pin directives for your setup.· Also, if you're not using the BASIC Stamp 2, make sure to update the $STAMP, and BaudMode directives accordingly.

The examples in the part datasheet appear to be incorrect because they truncate the D10 and D11 bits.· Either that, or the explanation is incorrect.··Assuming the explanation is correct, the binary value for -56.27 degrees·should be·1100_0110_0101_0101 and the·one for -37.7 degrees·should be·1000_1110_0011_0011.·

Regards, Andy

' -----[ Title ]--------------------------------------------------------------
' TestSpectroTilt3.bs2
' Test the SPECTROTILT module with the Parallax Serial LCD.
' Display values range from -70.14 to 70.11 degrees.

' {$STAMP BS2}
' {$PBASIC 2.5}

' -----[ I/O Pins ]-----------------------------------------------------------

LcdPin PIN 14
AdcPin PIN 15

' -----[ Constants ]----------------------------------------------------------

LcdBaudMode CON 84
AdcBaudMode CON 84

LcdStart CON 22
LcdCls CON 12
Line0 CON 128
line1 CON 148

' -----[ Variables ]----------------------------------------------------------

angle VAR Word
index VAR Nib
parity VAR Bit

' -----[ Initialization ]-----------------------------------------------------

SEROUT LcdPin, LcdBaudMode, [LcdStart, LcdCls]

' -----[ Main Routine ]-------------------------------------------------------


GOSUB Get_Spec_Tilt
angle = angle * 10 ** 22446
angle = angle - 7014

SEROUT LcdPin, LcdBaudMode, [Line0, "angle = "]
IF angle.BIT15 = 1 THEN
SEROUT LcdPin, LcdBaudMode, ["-"]
SEROUT LcdPin, LcdBaudMode, [" "]
SEROUT LcdPin, LcdBaudMode, [DEC2 ABS(angle) / 100,
DEC2 ABS(angle) // 100]



' -----[ Subroutine - Get_Spec_Tilt ]----------------------------------------


SERIN AdcPin, AdcBaudMode, [angle.HIGHBYTE, angle.LOWBYTE]
IF angle.BIT7 = 1 THEN
angle = (angle >> 8) | (angle << 8)
GOSUB Even_Parity
LOOP UNTIL parity = 0
angle.LOWBYTE = angle.LOWBYTE << 2
angle = angle >> 2 & %0000111111111111


' -----[ Subroutine - Even_Parity ]------------------------------------------


parity = 0
FOR index = 0 TO 15
parity = parity + angle.LOWBIT(index)


Post Edited (Andy Lindsay (Parallax)) : 1/22/2006 12:38:09 AM GMT

01-21-2006, 02:35 AM
still no go

01-23-2006, 07:44 PM
TestSpectroTilt3.bs2 works like a charm. Thanks for the incredible support............

01-25-2006, 03:07 AM
does anyone know if you can use the CCD Character Creator with a Parallax 2x16 serial LCD display ?

If not I tried to use the following as a test using a ASCII chart from the below link.

http://www.myke.com/lcd.htm (http://www.myke.com/lcd.htm)

' {$STAMP BS2}
' {$PBASIC 2.5}

degree VAR Byte
degree = %01111110

SEROUT 14, 84, [degree]

Post Edited (gc3076) : 1/24/2006 8:20:41 PM GMT

Chris Savage
01-25-2006, 05:19 AM
Yes, you can use the custom character creator.· The main thing is that it outputs the correct values for the character and you can just paste those into your existing DATA statement where needed.

Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

01-25-2006, 10:04 PM
can anyone suggest a link because most of the documentation I am finding seems to be targeting sending special characters to a non-serial LCD. The documentation on the display (2x16 serial LCD pn:27977) says "coming soon custom LCD animation"

Jon Williams
01-25-2006, 10:12 PM
I've attached a demo for the Parallax Serial LCD that shows how to download custom characters.· As you can see, the only difference betwee the DATA statments in this program and the output for the LCD Character Creator is the addition of the character code constant.· There was a discussion on this subject in this thread:


Jon Williams
Applications Engineer, Parallax

01-26-2006, 01:37 AM
any other code samples, less involved ?

Andy Lindsay (Parallax)
01-26-2006, 01:59 AM
The last post in this Stamps in Class thread Getting Started with the Parallax Serial LCD (http://forums.parallax.com/showthread.php?p=531520)·now has a PDF and a short example program.·