The Hitachi HM55B Compass Module produced by Parallax is a dual-axis magnetic field sensor, that is, the electronic equivalent of a traditional compass.
Since it comes in a standard 6-pin DIP package and can be operated at 5V, interfacing it with our FOX Board is a snap: all that is required is some wiring.
A falling edge (a transition from high to low) on /Enable pin will tell the chip to expect a command, that will be ended by a raising edge, again on /Enable. The commands are sent out serially one bit a time by raising Clock and then setting the required level on Dout pin. Same goes for reading from Din: raise Clock, read level, lower Clock.
It is also possible to connect Dout and Din, and save one IO pin for connecting other hardware.
To obtain a measure of the magnetic field, you need to send a RESET followed by a START MEASURE command. Then, you loop asking for status until the device signals that the measurement is complete, at which point you can get the x,y data from Din as two 11 bit quantities.
Let's have a look at code to interface with this chip and read it's data. @source='parallax.c' The main() function is pretty simple. Function open_ports() takes care of opening the needed IO ports and setting the pins directions; the #define directives inform it about which ports and pins will be needed.
In this case, we are using a 3-wire configuration with the following pins:
| /Enable | OG5 |
|---|---|
| Clock | OG3 |
| Din,Dout | PB6 |
The core of our implementation is, of course, inside function measure().
Here the only peculiarity is how we take care of negative values for x and y, which are 11 bit quantities inside a 32 bit integer storage: if their sign bit (bit 10) is set, we need to fill with 1s the bits from 11 to 31.
Finally, here's the implementation for the functions that really drive our pins.
This is the Makefile for compiling this code: @source='Makefile'
To compile create a directory parallax inside the dir apps. Then copy inside it the source code parallax.c and the Makefile. Move into apps/parallax directory and type:
# make cris-axis-linux-gnu # make
Uncomment the CFLAGS line inside Makefile to obtain a debug version of the program.