A Deeper Look
Referencing the schematic (Figure 1) and the
breadboard circuit (Figure 2), the PIC (U1) is relatively
straightforward to wire up. A ceramic 0.1 µF capacitor
(C1) is used across VDD and GND to filter out any power
supply transients and noise. Pin RA5 is connected to VDD
through a 10 kΩ resistor (R1) to prevent the pin from
floating, thereby preventing excess power consumption.
This pin will be assigned as the UART RX pin which will
not be used in our design. Another 10 kΩ resistor (R2) is
used to tie MCLR to VDD. While this is technically
unnecessary due to an internal weak pull-up in the PIC, it
helps with noise immunity.
The transmitter link (U2) requires nothing special; just
connect the appropriate pins as shown in the schematic.
The Data In pin connects directly to pin RA4 (UART TX)
of the PIC with no pull-up resistor required.
The LM4040 (D1) requires a shunt resistor (R4) to
maintain the appropriate bias current while taking into
consideration the load current which, in this case, is the
PIC input leakage current. Referring to the example in
the LM4040 datasheet, we can compute the minimum
and maximum values for R4. It is already known that VDD
will be between 2.7V- 4.5V and VZ is 2.048V. The range
for the load current (IL) is 5-125 nA (from the PIC
datasheet) and the range for the bias current (IZ) is 75 µA
to 15 mA. This yields a maximum resistance for R4 of
8,678 Ω and a minimum resistance of 164 Ω. In the
interest of preserving the batteries (and more importantly,
because I had it in my toolbox), I chose R4 to be 7. 5 kΩ.
This would set IZ between 87 µA and 327 µA. The
TMP36 (U3) is a no-brainer. A ceramic 0.1 µF capacitor
(C2) was added between +VS and GND for noise
immunity, per the recommendation of the datasheet.
Now it's time to exercise our knowledge of
transistors! To compute the base
resistance (R3) required to saturate Q1
and thus use it as a switch, we must
consider the case of the batteries
being almost completely discharged,
providing only 2.7V. This would bring
the collector load (IC) to a minimum.
Referencing the sidebar, we can
now compute the required maximum
resistance for R3. Using the 2N3904
datasheet to find the value of VBE
when the device is saturated
(approximately 0.75V), I compute R3
as 5039 Ω ... or 4. 7 kΩ for a nearest
standard value. This will guarantee the
saturation of the transistor for the
entire useable battery voltage range.
The Softer Side
Now it's time to write some code ... or at least it's
time for me to explain the code that I wrote! Due to the
fact that the PIC12F1822 is in the enhanced mid-range
family of microcontrollers, I've elected to use the C
language (using Microchip's free XC8 compiler) to write
the software. This certainly makes coding easier, but keep
in mind that using assembly on this device could possibly
create faster leaner code. However, that's not a huge
concern for this application.
Referencing temperature_transmitter.c, the first order
of business is configuring the PIC appropriately by setting
the desired device configuration words:
• Internal/External Switch-Over (IESO) — disabled
(two-speed start-up disabled)
• Oscillator (FOSC) — internal
• Fail-safe Clock Monitor (FCMEN) — disabled
(only useful with an external oscillator)
• PLL (PLLEN) — disabled
• Brown-Out Reset Voltage (BORV) — high ( 2.7V typical)
• Low Voltage Programming (LPV) — disabled
There are several more configuration words for this
device, but I've left them at their defaults. Of note, we
are using the brown-out reset capability of the PIC which
will hold the device in reset when it drops below a set
threshold; in this case, 2.7V. All configuration words can
be referenced in the XC8 documentation
( pic_chipinfo.html, supplied by Microchip).
Next up are some macros used in the source code.
To save power (a faster clock equals more power
consumption) but maintain a reasonable instruction
execution clock (FOSC/4), the oscillator is left at its reset
value of 500 kHz. To reflect this is _XTAL_FREQ, which is
required to be defined in order to use the delay functions
30 January 2014
■ FIGURE 2.