Devantech CMPS03 Compass PICAXE 28X1 i2c Sample Code

BCJKiwi

Senior Member
Code:
'###############################################
'# For PICAXE-28X1 and Devantech CMPS03 Compass #
'###############################################
#picaxe 28x1
SetFreq m8
'###############################################
 
Main:
'COMPASS CODE
' Set up i2c for device 
 hi2csetup i2cmaster,%11000000,i2cfast_8,i2cbyte    '%11000000 is device i2c bus address
Compass:
 pause 1000
' hi2csetup i2cmaster,%11000000,i2cfast_8,i2cbyte
       ' If using multiple devices of different speeds on the bus,
       ' then use this line and remove the [%11000000] in the next line.
 
 hi2cin [%11000000],0,(b22)       ' Read CMPS03 Software Revision
       ' [%11010000] ensures correct i2c is read - reqd if more than one 
       ' i2c device on bus being read in sequence
 hi2cin 2,(b27,b26)  ' Read compass bearing as a word (0-3599 for full circle)
      ' CMPS03 Register 2=HighByte, 3=LowByte. w2(b27)=highbyte, (b26)=Low byte 
 w12 = w13/10   ' /10 to get whole degrees
 pause 500
 sertxd ("FW Rev:",#b22,"  Compass Bearing:",#w12,cr,lf)
 Goto Compass  'Loop for continuous display
 
Last edited:

BCJKiwi

Senior Member
A footnote on the CMPS03;
While working on the next stage of the project incorporating the CMPS03 (rev 14), I was having difficulty configuring another i2c device (not responsive in any way).
After much trial and error I found that if the CMPS03 is wired to the i2c bus but not powered up (i.e. v+ disconnected), it 'kills' the i2c bus. So if you are working with a CMPS03, ensure it is fully connected and powered up, or, disconnect it from the bus at the preceding device - i.e. no floating wires. Other devices (e.g. DS1307) did not exhibit this behaviour.
 

smssms

New Member
CMPS03 to 7-segment display

Just thought I would add my CMPS03 code too (for comment or if it is of use to anyone).
This does work, but please bear in mind that I am totally self taught :confused: and this is my first project...

Code:
; *****************************************
; *****  4x7 segment display compass  *****
; *****************************************
;    Filename: 4x7segment+compass_v1.bas
;    Date: 31/07/10			
;    File Version: 1	
;    Written by: SmSSmS
;    Target PICAXE: 28x1	
; *****************************************

;Read a CMPS03 compass and display output as degrees on TwiLeDisp4x7s+2L 4x7 segment display (via I2C).

main:

	I2CSLAVE $C0, I2CFAST, I2CBYTE		'I2C slave address, speed and var size for CMPS03
	READI2C 1, (b0)					'Read compass bearing as a byte (0-255 = full circle)
	PAUSE 100
	LET w13 = b0*141/100				'Convert 0-255 to 0-359 (degree) graduations
	BINTOASCII w13, b24, b25, b4, b5, b6	'Convert word to individual ASCII digits

	'*** I2C 4x7 segment display (TwiLeDisp4x7s+2L) data format/protocol ***

	'b1 =    ' 7=upper LED, 6=lower LED, 5=digit mode (1), 43210=7seg. brightness (0-32)
	'b2 =    ' Brightness of LEDs 0XXXXXXX (0-127)
	'b3 =    ' Display character 1
	'b4 =    ' Display character 2
	'b5 =    ' Display character 3
	'b6 =    ' Display character 4
	'b7 =    ' Display decimal points (0000XXXX)
	'b8 =    ' Parity i.e. XOR of address and b1 to b7

	b1 = %00100010
	b2 = %00000000
	b7 = %00000000
	b8 = $06 XOR b1 XOR b2 XOR b3 XOR b4 XOR b5 XOR b6 XOR b7 	'Parity check calculation

	I2CSLAVE $06, I2CFAST, I2CBYTE 					'I2C slave address,speed,size for display
	WRITEI2C (b1, b2, b3, b4, b5, b6, b7, b8) 			'Write to the I2C display
	PAUSE 100									'Allow some time for the write

	GOTO main
 

westaust55

Moderator
First project

Congratulations for getting your first project working. :)

Not essential but as some comments:

You could move the fixed definitions for variables b1, b2 and b7 to the beginning of the program before the looping part if you wanted (ie if not ever intending to change them within the loop) which will save a small amount of time for each loop to execute (like about 1 millisecond :eek: ).

Furthermore, if never intending to chnage these values, you could insert them directly as values into the WRITEI2C and save some relatively scare byte variables.
 

smssms

New Member
OLED display for compass

For the next version of my compass, I'd like to leap forward to an OLED display. I've got myself a 4D Systems 96x64 OLED serial display and already have the compass module.
As mentioned above, I'm pretty new to all this and was just wondering if anyone had any advice or opinions on how to implement the display. I'd be happy with 5 deg resolution and would like it to look like this horizontal scrolling aircraft compass:



Would 72 simple images of the horizontal scale (i.e. an image for each position) saved on the SD card and called up as required be a good way to approach this?
 

westaust55

Moderator
The proposed display should not be too difficult if you accept a straight line for the base line of the graduations. A base image that can be quickly called up and then plot in the compass details.

I did the image on a gLCG in B&W on a LCD ex a Siemens A55 mobile phone if you wish to search for that project. That did use a set of partial images and form memory at something like 11.25 degree increments.

The 4D-System OLED displays have line drawing comamnds so maybe a bit slow with PICAXE but can be done.

Now a recommendation:
This is a finished project in the Finished Projects area that you have appended to the bottom of. Can I suggest that you start a new thread of your own in the Active projects area here:
http://www.picaxeforum.co.uk/forumdisplay.php?f=2
for any questions specific to your own project. Particularly as you have already moved away from CMPS03 module to OLED display.
That way threads on a specific topic are kept cleaner of non related information/queries. Thanks
 
Top