Jump to content

PBASIC Programming/Serial Communications

From Wikibooks, open books for an open world

Parallel and Serial

[edit | edit source]

If we want to send out 16 bits worth of data, as we had been doing in the last chapter, we would need to define 16 output ports, and then write our data out to the pins. Here is an example that will output a 1 on every port:

DIRS = %1111111111111111
OUTS = 255

Unfortunately, this system doesnt leave us any extra pins to play with. We don't have any available pins to use for inputs or other outputs. In cases like these, we want to use serial communications instead.

Parallel communications, what we have been doing so far, uses many wires in parallel with one another. Each data bit is transmitted on its own wire, and so to send x bits we need to use x wires. In a serial system, instead of using 16 wires, we use just 1 wire, and send all the data bits on that one wire, one at a time. The target computer, where the data is being sent to, reads each of the bits, one at a time, and stores them in a variable.

To use serial I/O, we use the SERIN and SEROUT functions

Serial Settings

[edit | edit source]

Serial I/O is obviously a little more complicated then parallel data is. For instance, we need to know what speed the data is being sent, so that the target computer can read the data at the same speed. We also need to know a few other things:

Parity
Parity adds an extra bit to every byte that we transmit. This extra bit can be used to determine if there is an error in the transmission. Parity can be set to "Odd", "Even", or "None".
Baud
"Baud" is the number of pulses that are transmitted each second. Both the sender and the receiver need to be using the same baud.
Data Bits
Data bits is the number of bits per "chunk" of information. There are commonly 8 data bits, but 7 is also possible on some hardware.

Both the sender and the receiver need to have the same values for the parity, baud, and data bits. If these are not matched, the communication will not work.

Port
This is the number of the port to be used to send or receive the data. This can be any of the regular data ports 0 - 15, or it can be the special dedicated serial port 16. Port 16 is the port that you use to program the BasicStamp. If you use port 16 you will have a fancy serial plug to use, but you won't be able to use the debug features of your BasicStamp. The sender and the receiver can use different ports.

Settings Values

[edit | edit source]

SERIN and SEROUT

[edit | edit source]

To send serial output, we use the SEROUT command. If we want to read serial data, we use the SERIN command. Because serial can send bits one after the other, we can send long data arrays at once, in addition to sending out single data bytes. We can send entire strings or arrays, if needed.