January
2006, Issue 186
Electronic
Scarecrow
MAIN
ROUTINE
The
flowchart posted on the Circuit Cellar FTP site
shows the remote unit’s main routine. After initialization
and sensor tests, the unit remains in the main loop,
which reads the sensors, checks for a switch press,
checks for incoming reply packets, and then sends a
request packet if necessary.
In
order to minimize power consumption, sensors are read
only if they are installed and working properly. During
initialization, the sensor_init routine executes a presence
detection algorithm for each sensor. For the accelerometers,
the algorithm uses their built-in hardware self-test
functions. The pressure sensor’s external amplifier
has a similar self-test function even though it doesn’t
actually test the sensor. Because it uses the same input
as the MMA1260D Z-axis accelerometer, the self-test
voltage level is sufficiently different so the software
can tell which one is connected. The MC33794 e-field
sensor’s test looks at the reference input levels, while
the switch expander test reads six of the input bits
that are hard-wired to an alternating high/low pattern.
After
all the tests, pull-ups are enabled on all of the MC9S08GT16
microcontroller’s analog input pins that didn’t have
valid sensor signals. I didn’t want to set them to outputs
in case a sensor was present but failed its test. The
next step is to read and store the zero offsets for
all the sensors. Lastly, a final test on each sensor
ensures that its value is within a valid input range.
Intended to check for broken wires, these final tests
are repeated each time a ping packet is sent.
The
main loop begins by reading all the connected sensors.
The accelerometers should be checked every 50 ms to
avoid missing an event. The others can be read every
500 ms. The switch expander doesn’t need to be read
because it interrupts the MC9S08GT16 microcontroller
whenever its status changes. I wanted to be able to
change the sensor thresholds without recompiling the
code, so I put them in a structure that’s loaded at
a fixed address at the beginning of the code. (Refer
to struct THRESHOLDS in the Periph.h file posted on
the Circuit Cellar FTP site.) This made them
easy to patch.