Compass

 

Driving the Robot Electronics CPMS03 compass

Notes .

This was difficult due to 2 problems being present at once (but now it is fine!)

1) The i2c response is slower than standard dedicated i2c chips (it is a PIC)
From the Oshonsoft notes -

"For some I2C slave devices it is necessary to make a delay to make sure device is ready to respond to I2CREAD statement.
For that purpose there is I2CREAD_DELAYUS parameter that can be set by DEFINE directive and has default value of 0 microseconds"

I used 80 microseconds - it was OK at 50 but not 10 or below.
It was not necessary to slow the clock.

2) The default calibration as delivered seemed to be faulty - north was close to east and the first 90 degrees was squeezed into the first 5 degrees from zero.
The calibration routine worked fine and things were then much easier to set up.

My Oshonsoft basic compass test program on the PIc is -


'i2c-compass_delay80mu.bas
'test the robot-electronics CMPS03

TRISC = 0 'all port C outputs
Symbol sda = PORTC.4 'setSDA for i2c
Symbol scl = PORTC.3 'setSCL for i2c
'prepare to use LCD later
Define LCD_LINES = 4
Define LCD_CHARS = 20
Define I2CREAD_DELAYUS = 80 'for this slow device (50 OK, 10 fails)
'Define I2CCLOCK_STRETCH = 2 'seems we only need the delay
Lcdinit
Dim bearing_word As Word
Dim bearing_degrees As Word

loop:
'----------------------------------------keep reading the compass and show bearing on the LCD

I2CRead sda, scl, 0xc0, 0x02, bearing_word.HB, bearing_word.LB
'(if you need to you can start at 0x00 and read all the registers in one statement - byte variable names separated by commas)
WaitMs 50

bearing_degrees = bearing_word / 10

Lcdcmdout LcdClear
Lcdout "bearing degrees ", #bearing_degrees

WaitMs 50

Goto loop