| ||||||||||||||
| ||||||||||||||
|
||||||||||||||
Basic Serial Communication with Stamp IIThe 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 SoftwareWhile 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:
The Codeled1 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 ConclusionOnce 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:
Submitted: 20/04/2001 Article content copyright © James Matthews, 2001.
|
|
|||||||||||||
All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -