At the forefront of Artificial Intelligence
  Home Articles Reviews Interviews JDK Glossary Features Discussion Search
Home » Articles » Robotics » Parallax Basic Stamp II

Basic Serial Communication with Stamp II

The STAMP II chip is not designed specifically for robots, and therefore is designed to be interfaced with just about anything. This article is a basic introduction to access the serial port with the Stamp II. In this example, we shall look at controlling two LEDs through the serial port.

The Hardware

All you need to do is wire up a 220 ohm to pin 0, this is connected to the long lead of an LED. The other end is connected to Vss. Do this with another LED, but connect it to pin 15. Remember to connect Vss and Vdd with the 3300 uF capacitor. Obviously, the pins do not matter, but they're the pins the program uses. To the right is a picture of the BOE-Bot protoboard used to create the circuit. If you have trouble, look in "Robotics!" p25., there is a diagram there that connects two LEDS to pin 0 and 14 (as well as two servos and a speaker, but ignore those).

The next step is to connect the chip to the serial port. Simply use the same cable that Parallax supplies with the STAMP chip to download programs! When using the serial port with other devices, you may have to use a male/female convertor, these should be quite easy to get at your local electronics/computer store.

The Software

While we could use Visual C++ or some other language to access the computer's serial port, we will simply use the Parallax software by accessing the serial port via the terminal window. The terminal window communicates with the Stamp chip through a dedicated I/O (pin 16) which is used for programming.

Baud Senseless

Serial communication is a lot complicated than you might imagine, since data can be transmitted at different baud rates, parities and data bits! Basically, what happens in serial communication is that data is sent at a predetermined rate (in bits/second) - this is a baud rate. Parity is used to determine transmission errors, but one bit is lost (halving your data range), so we won't bother with that. The data bit specifies how many bits in any given chunk of data. Therefore, non-parity chunks have 8 bits (in our case), and parity chunks have 7 bits.

With that out of the way, how to we communicate with the serial port? On the PC side it is handled by the Parallax software, so we don't need to worry. On the STAMP side it is handled by the SERIN (serial in) command. SERIN is quite powerful, and we definitely won't look at all it's features. In fact, we're only going to look at the features immediately useful for us.

SERIN will take up to five parameters, we will look at 3. Firstly, it takes the STAMP I/O pin number to communicate through, then the baud rate and parity settings, and finally where to put all of the data. The pin number will be the programming pin, therefore pin #16. Now, the baud rate is a little more complex. It is encoded in a 16bit number and uses the following method:

  1. Calculate baud rate by expressing the baud rate in microseconds. This is done by dividing one million by the baud rate. Only the integer part is needed. Now, for some strange reason the SERIN bit period is always 20uS longer, therefore we need to subtract 20uS from that value. This value will represent bits 0 - 11 of our number
  2. Bit 13 is set (1) if we use 7 databits and parity checking, otherwise it is reset (0).
  3. Bit 14 is for polarity (inverts all bits). Set if inverted, reset if not.
Note that bit 15 is not used. So, to code our baud rate (9600, no parity) we need to do the following:
  1. (int(1,000,000)/9600)) - 20 = 84 decimal.
  2. Bit 13 is reset.
  3. Bit 14 is reset.
We therefore end up with 84! Not that 16468 would also work since bit 15 is not used in SERIN. Now you are ready to look at the code:

The Code

led1 con 0      ' The first LED
led2 con 15     ' The second LED
progPin con 16  ' The programming pin

ledCmd var byte

low led1
low led2

 debug "Started.", cr

ReceiveLoop:
 Serin progPin, 84, [ledCmd] ' Get the serial data

 if ledCmd = 48 then led1Toggle
 if ledCmd = 49 then led2Toggle
 if ledCmd = 32 then die

 goto ReceiveLoop

led1Toggle:
 toggle led1
 goto ReceiveLoop

led2Toggle:
 toggle led2
 goto ReceiveLoop

die:
 high 0
 low  15
 pause 333
 low  0
 high 15
 pause 333
 low  0
 low  15

 debug cr, "Finished",cr

 stop

Conclusion

Once you understand the encoding scheme it is very easy to access data from the serial port. Applications of the serial port are endless, you can use the serial port to drive servos, speakers, IR transmitters, receivers, anything that can connect to the Stamp II you can now create an interface between them. Possible ideas:
  • Remote control for your computer
  • Webcam on a moveable tripod
  • Motion sensor that e-mails you when motion is detected
  • Complex robotic behaviour by processing data PC-side
Enjoy!

Submitted: 20/04/2001

Article content copyright © James Matthews, 2001.
 Article Toolbar
Print
BibTeX entry

Search

Latest News
- Generation5 10-year Anniversary (03/09/2008)
- New Generation5 Design! (09/04/2007)
- Happy New Year 2007 (02/01/2007)
- Where has Generation5 Gone?! (04/11/2005)
- NeuroEvolving Robotic Operatives (NERO) (25/06/2005)

What's New?
- Back-propagation using the Generation5 JDK (07/04/2008)
- Hough Transforms (02/01/2008)
- Kohonen-based Image Analysis using the Generation5 JDK (11/12/2007)
- Modelling Bacterium using the JDK (19/03/2007)
- Modelling Bacterium using the JDK (19/03/2007)


All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -