WebbotLib AVR library
WebbotLib It just does it
  C++ documentation  C documentation

Devantech/CMPS03.h

The CMPS03 compass sensor.
cmps3pin2.jpg
The compass can be used either via the I2C pins 2 and 3 or by measuring the pulse length output on 'Pin 4 - PWM' of the board.
The device requires a 5V regulated supply so on the Axon you will need to use one of the ADC header supply pins.
Example for the Axon that uses Pin 4 of the device connected to F4 on the Axon :-
#include "sys/Axon.h"
#include "rprintf.h"
#include "Sensors/Compass/Devantech/CMPS03.h"

CMPS03 cmps03 = MAKE_CMPS03(F4);


void appInitHardware(void){
   //setup UART
   uartInit(UART1, 9600);
   rprintfInit(&uart1SendByte);
   // Initialise the compass
   compassInit(cmps03);
}
 

TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
   return 0;
}
 

// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart){
   compassRead(cmps03);
   rprintf("CMPS03 = %d\n",cmps03.compass.bearingDegrees);
   return 0;
}
To use the I2C bus instead then you only need to change the one line of code where the compass is defined to:-
CMPS03_I2C cmps03 = MAKE_CMPS03_I2C_At(0xC0);
The 0xC0 parameter is the I2C address of the device. The default value is 0xC0 but you can change this value via software to another address. Read the data sheet to find out how. Having created the device you will need to add it to an I2C bus as shown in i2cBus.h
You may also need to calibrate the compass depending upon where you are on the earth and this is also explained in the data sheet.

Valid XHTML 1.0 Transitional