provided by Microchip. The DEVICE_ID is
the data address for the transmitter (to be
used in the receiver for identification) and
the PREAMBLE is used to condition the
receiver's automatic gain control (AGC).
More on this later.
With that done, the first thing we want
to do is set the watchdog timer interval. You
may now ask yourself, "What is a watchdog
timer?" In the PIC (and other
microcontrollers), a watchdog timer is an
independent timer circuit that continuously
runs in the background. If it is not
periodically reset, the device will reset itself.
This is useful in environments where the PIC
may be subject to severe EMI and could
lock up, or for poorly written code that
locks itself up.
An added bonus for us is that it can be configured to
run in the background while the device is asleep, and will
subsequently wake up the device and continue execution
when it reaches its programmed interval. This allows us
to use it as a convenient power-saving method. For my
purposes, I set the watchdog timer interval to 64 seconds
(while testing, I lowered the interval to two seconds).
Before we can get to the good stuff, we still need to
do some standard PIC boilerplate. Make sure to clear
PORTA and set the appropriate pins as inputs and
outputs with the TRISA register. Always clear the port
first, as this will prevent a short circuit if a pin is
erroneously cond. After, we must enable the ADC for the
appropriate pin (RA2) and disable the others; otherwise,
they cannot be used as digital I/O.
Alright, time to set up the UART. A really nice feature
of this particular PIC is the ability to reassign the TX/RX
pins of the UART. The default is RA0/RA1, but I would
like to use RA4/RA5. The baud rate generator needs to
be set up for 2400 bps, eight-bit, high-speed,
asynchronous mode, and the value for it can be
computed with the following equation:
With our requirements, this results in a SPBRG value
of 12. Out of curiosity, we can find our true baud rate and
error rate by reorganizing the equation which results in
2403.8 bps and 0.16%, respectively. Finally, the UART
must be enabled, completing the setup process. The ADC
setup can be a bit tricky and requires some study of the
datasheet to understand. First, we must set the conversion
clock. To do this, we must first know our minimum
conversion time (TAD) which is 1 µs per the datasheet.
With a clock frequency of 500 kHz, this results in an
instruction cycle period of 2 µs. If we select a conversion
clock of FOSC/2, we will be right at the minimum.
The positive voltage reference (Vref+) must be set to
an external Vref+ (the LM4040). Next, set the appropriate
analog channel used for the ADC (AN2) and then set the
conversion data to be right-justified.
Referencing Figure 3, we get a picture of the main
program loop. The first order of business is to turn on our
external devices and then delay for at least 1 ms to allow
for them to stabilize (specifically the TMP36). Following
this, the ADC and UART TX must be enabled.
Okay, let's convert some voltage! To use the ADC in
the PIC, we must first delay for the minimum acquisition
time (TACQ). The typical value on the datasheet is 5.0 µs.
There is also an equation and example published in the
datasheet used to compute the minimum TACQ, but for
now we'll stick with 5.0 µs. Once the delay is over, start a
conversion by setting the ADGO bit and wait until the
conversion is complete.
Well, that was easy! Now, let's compute a checksum
and send some data. The checksum is a simple modular
sum which is computed by summing all the data
(DEVICE_ID and ADC result) together as an unsigned byte
(discarding overflow), and performing a two's complement
on the result. This will help us error-check on the receiver
side. Next, we must send the PREAMBLE to the receiver in
order to de-gain its AGC. The receiver link will gain-up the
AGC when it is not receiving a strong signal, which results
in a lot of noise and bad data.
In order to synchronize the transmitter and receiver and
de-gain the AGC, a preamble of alternating bits is sent
before the data. I suggest sending at least two bytes’ worth
of preamble to ensure success. After the preamble, we'll
send the device ID, the high byte of the ADC result, the low
byte, and the checksum. Before each byte, check to make
sure the UART is not currently sending data. Also, after the
final byte, verify that the UART has sent all the data.
Time to go to sleep! Disable the UART TX and ADC
and switch off the external devices. Finally, put the PIC to
January 2014 31
■ FIGURE 3.
SPBRG = (desired baud rate 16) — 1