an on-chip timer with a period as close as possible to the
width of one bit at the desired bit rate. For example, at
9600 bits per second (bps), each bit is 104 microseconds.
wide. The CPU’s clock source (FOSC) and values in
registers determine the timer’s period.
In the TXSTA register, the BRGH bit selects a
multiplier to use in configuring the timer. In the BAUDCON register, the BRG16 bit determines whether an eight-or 16-bit value sets the timer’s period. The SPBRG register
stores the value’s low eight bits. For 16-bit values, the
SPBRGH register stores the upper eight bits. (Not all PICs
support using 16-bit values.)
Often, an exact match to the desired bit rate isn’t
possible. The chip’s crystal or other timing source can also
introduce error by varying slightly from its rated frequency.
In general, the bit rates on both ends of the line can differ
by up to about 3% without causing errors. Because you
typically don’t know the accuracy of the other end’s
clock, it makes sense to use settings that give the closest
A quick way to obtain values for a desired bit rate
is to consult the tables in the chip’s datasheet. For the
device’s FOSC value and desired bit rate, select the values
that give the smallest error.
For example, for a PICDEM 2 Plus board with FOSC =
4 MHz and desired bit rate of 9600 bps, setting BRGH =
1 and BRG16 = 0 gives a close match with a bit rate of
9615 bps. With BRGH = 0 and BRG16 = 0, the best
match is 8929 bps, an error of almost 7%.
PICBASIC’s hser_baud statement checks the value of
BRGH, assumes BRG16 = 0, and sets SPBRG for the
closest match to the requested bit rate. To use a 16-bit
timer value, set BRG16 and load the SPBRG and SPBRGH
registers directly instead of using hser_baud:
FIGURE 2. An RS-422 interface can
use longer cables than RS-232.
70 May 2008
‘ Enable the transmitter and set BRGH = 1.
define HSER_TXSTA 24h
‘ Set BRG16 (BAUDCON.3) = 1.
baudcon = 8
‘ Load SPBRG and SPBRGH for the desired bit rate.
‘ (Use 19fh for 2400 bps.)
define hser_spbrgh = 1
define hser_spbrg = 9fh
If your circuit’s FOSC value isn’t listed in the tables,
the datasheet provides formulas for selecting values.
Extending a Link With RS-422
RS-232 is a rugged interface suitable for cables of up
to around 100 ft using off-the-shelf, unshielded cables.
The exact limit varies with the cable’s capacitance. To help
prevent data errors, the interface uses wide voltage swings
and limits the slew rate — or rate of change — at the outputs.
If you need a cable of up to 4,000 feet, RS-422 is an
option. Program code that communicates via an RS-232
port can also communicate via RS-422 with no changes.
RS-422 uses balanced lines, where each signal has a
dedicated pair of wires. The voltage on one wire equals
the complement of the voltage on the other wire. The
receiver detects the voltage difference between the lines.
Balanced lines are electrically quiet because most noise
is common to both wires and thus cancels out.
For best performance with RS-422, use twisted-pair
cable. Each pair is two insulated wires that twist around
each other every inch or so for the length of the cable.
Connect each signal’s two lines to wires in the same pair.
The cable should
have a characteristic
impedance of 90 to 150
impedance is a measure
of the impedance of an
infinite line. The value
doesn’t change with
the cable’s length. For
RS-422, a good choice
is network cable such
as Category 5e, which
has a characteristic
impedance of 100 ohms.
interface must have a
ground line or other
connection to carry the
current that results from
any mismatch between
the drivers on a line.
Figure 2 shows an