arrive. Waiting for the entire command prevents buffer
overflows. The penalty is that the code can’t do
anything else while waiting for the rest of the command
Because the currently executing statement must finish
before the ISR runs, avoid statements that take a long time
to execute, such as pauses with long delays. If you need
faster response, PICBASIC supports using ISRs written in
assembly code. Another way to allow more time to
retrieve received data is to use a slower bit rate, which
results in more time between received bytes.
Some systems use hardware flow control — also called
handshaking — to prevent missed data. RS-232 has defined
hardware flow-control signals. On a PC’s port, Request
to Send (RTS) is an output, and Clear to Send (CTS) is
On a PIC, hardware flow control requires two
otherwise unused port bits and an additional RS-232
driver and receiver. The MAX3232 on the PICDEM2 Plus
board has a spare driver and receiver. You can solder
jumper-wire connections to these (Figure 1), but the chip
is surface-mount, so soldering can be tricky. Another
option is to solder another MAX3232 or similar interface
chip in a through-hole package to the board’s prototyping
area and wire the needed connections to the chip.
In Figure 1, note that the RS-232 signals are named
from the perspective of the remote PC. The PIC’s TX
output controls RS-232’s RX signal, which is an input on
the PC, and RS-232’s TX signal, which is an output on the
PC, controls the PIC’s RX input.
Some development boards wire CTS and RTS
together at the RS-232 connector. If you need to use
hardware flow control, check your board’s schematic and
cut any unwanted connections.
In a PIC using hardware flow control, program code
must bring the CTS port bit high when the port isn’t ready
to receive data. The RS-232 driver inverts the signal, so on
the cable, CTS is a negative voltage. At a PC that wants to
send data to the PIC, an RS-232 interface chip re-inverts
CTS, resulting in a logic high output. The PC must detect
the state of CTS and stop sending data when the output is
When sending data using hardware flow control,
program code must detect the state of RTS and send data
only when RTS is logic low at the PIC (and thus a positive
voltage on the RS-232 cable). PC software uses large
buffers, so when communicating with a PC, the PIC isn’t
likely to need to monitor RTS.
PICBASIC PRO and other PIC compilers can also
create software serial ports using any spare port pins.
Instead of using the chip’s hardware EUSART, the compiler
controls communications with the help of an on-chip
timer. For software ports, PICBASIC’s serin2 statement can
name a flow-control output bit that toggles automatically
as needed to prevent overflows, and serout2 can name a
flow-control input bit. Software ports can’t use the serial
port’s hardware interrupt, however.
Two other RS-232 signals are DTR and DSR. Some PC
software requires DSR to be asserted (positive RS-232
voltage) to signify the device is powered. The PICDEM
2 Plus board connects DTR to DSR on the RS-232
connector so at the remote computer, the DSR input
always follows the DTR output.
To enable using DTR and DSR for flow control or
other uses, cut the circuit board trace that connects
these signals together. Add an RS-232 driver and receiver,
and wire connections to spare port pins and the D-sub
Setting the Bit Rate
A serial port’s bit rate is the number of bits per
second the port transmits or receives. In basic data links,
the bit rate equals the baud rate, and you can use either
term as you prefer.
To set a PIC’s bit-rate clock, program code configures
FIGURE 1. A MAX3232 converts between
5V logic and RS-232 voltages.
May 2008 69