Devantech Magnetic Compass - CMPS03
Background Info -- I thought it would be cool to give our power wheels robot a digital compass to help navigate. For $45 + shipping, we picked up a Devantech Magnetic Compass from Acroname. The compass has two different ways to transmit data. One way uses the I2C interface which is a popular type of serial communications. The compass also has a pin that sends a high pulse that is directly related to the direction the sensor is facing (PWM). Now there weren't already drivers for the Handyboard to communicate with this compass, so I thought it would be easier to use the Handyboard's internal TIC (Timer Input Capture) feature to measure the length of the PWM signal.
The Compass's PWM signal length is 1mS to 37mS. This corresponds to 0 degrees to 360 degrees.
How My Driver Works -- digital input 7 is directly connected to one of the 68hc11's TIC units. I enable an interrupt when the signal on digital input 7 goes from low to high. Then I save the time that the low to high transition happened. Then I enable the TIC unit to respond to a high to low transition and I wait for this to happen. Then the second time minus the first time is the length of the pulse in E clocks ( 1 E clock = 0.5uS ). I convert this number to milliseconds. Then convert to degrees by subtracting one and multiplying by 10. Its actually a little bit more complicated because the internal timer that is measuring the pulse will wrap around every 32mS and the max pulse can be 37mS. If you look at the source code you can see how I keep track of the wrap around. Also, the documentation for the compass says that it is supposed to be accurate to a tenth of a degree, however I think that the pulse width modulation signal is only accurate to a degree which should be more then enough accuracy.
How to Connect the Compass --
How to Use My Driver -- Download alc_cmps03.ic and alc_cmps03.icb. Put
those files into the same folder as your IC program that you want to use the
compass with. At the top of your program add the line:
Then in your program add calls to compass(). For example you could do this to continuously print out the compass's baring:
while(1) printf( "%d\n", compass() );
Download My Driver --
CMPS03 Compass Links
Devantech - CMPS03 - Robot Compass Module Documentation
Purchase the compass at Acroname