Digital Compass using Devantech CMPS03

Do u need a digital compass for your app?
Here we go, let's put interface a digital compass w/ CUBLOC CB280.


Required Parts ;

1 CUBLOC CB280 (or some type of microcontroller)
1 CUBLOC Study Board (or some type of development board)
1 Devantech CMPS03 (available from www.acroname.com in US)
2 4.7K resistors
1 CSG-4S (Seven Segment Module)


I was searching through google the other day, and I wanted to build a digital compass for my car.
So I figured I'd first start messing with it by using a CUBLOC Study Board
since it has a nice little breadboard for proto-typing.


CMP03 Pin9  <-->      GND             CMP03 Pin1  <-->  5V

  CMP03 Pin2  <-->      P31              CMP03 Pin3  <-->  P30

    SGN SCL      <-->      P31                SGN SDA     <-->   P30

  SGN GND     <-->      GND               SGN 5V        <-->  5V


Please make sure to connect Pull-Up resistors 4.7K to P31 and P30 as shown below:

Wait a Minute, MAX! What are the resistors for?

Ah, since we are using I2C protocol,
and I2C SCL and SDA pins need 4.7K Pull-up resistors in order for the protocol to work well.

(Close-up of connections on the study board to CMPS module)

I've hooked up a CGS-4S module with the CMPS module together.
That is the beauty of I2C, you can connect multiple devices in one bus.

The following is pin out for the CMPS03 compass module.
We have connected SDA and SCL to P30 and P31 of our CUBLOC CB280 module.

Above is the I2C chart for the CMPS03 module.
As you can see, the compass uses 0xC0 as its address.  

We can first send 0xC0, then send the value of register,
followed by 0xC1 and then we can read one byte of data from the CMPS03.

Looks fairly simple right?  Let's look at more in detail what these registers do.

As you can see, register 1 will return a value between 0 to 255 for a full circle.  

Register 2 and 3 will return between 0 and 3599 as a tenth of a degree.

Next, we will use CUBLOC BASIC to read both register 1, 2, and 3 to return
the current values of the digital compass.


Sub CompassSend()
Dim Value As Byte
Dim Degree As Integer

           Set I2c 30,31
' Read register 1 - value 0 to 255
If I2cwrite(&hc0)=1 Then Debug "ERROR1",cr
If I2cwrite(&h01)=1 Then Debug "ERROR2",cr
If I2cwrite(&hc1)=1 Then Debug "ERROR3",cr
' Read register 2 & 3 - value 0 to 3599
           '  - 3564 means 356.4 degrees
If I2cwrite(&hc0)=1 Then Debug "ERROR1",cr
If I2cwrite(&h02)=1 Then Debug "ERROR2",cr
If I2cwrite(&hc1)=1 Then Debug "ERROR3",cr
If I2cwrite(&hc0)=1 Then Debug "ERROR1",cr
If I2cwrite(&h03)=1 Then Debug "ERROR2",cr
If I2cwrite(&hc1)=1 Then Debug "ERROR3",cr
Debug Dec degree/10," ",Dec Value,cr
'Output to seven segment display!_____________
csgdec 0, DEGREE/10                      

End Sub         


As you can see in the ComapassSend function,
we simply follow the I2C chart for the CMPS03 Compass module.  

We will read register 1 and get a value between 0 and 255.
Next, we will read register 2 and 3, each high byte and low byte of a 16-bit integer, respectively.

This will return a value between 0 and 3599. 
degree.byte1=I2cread(0)  will store register 2 in the high byte of degree integer variable.
degree.lowbyte=I2cread(0)  will store register 3 in the low byte of degree integer variable.

The rest I think you will understand very easily just by looking at it.
Here, we use
If Then Debug “ERROR”  to check for acknowledgement of the I2C device, CMPS03 module.

The csgdec function is simply a system function for outputting to the seven segment display.

Simply compile and download this program to your CB280 module now.

If you start rotating your study board,
you will see that the seven segment display will change values in degrees between 0 and 360.

(digital compass testing completed)

If you don't have a CSG-4S module or cannot afford one,
you can use the debug screen to see the following:

The first value is in degrees between 0 and 360 and second value is
value between 0 and 255from register 1.

 Now you have become a master of I2C protocol,
please go out there and put a compass on your app now.

Now all I have left to do is to convert 12V DC power of my car to 5V
so I can put my CB280 module and CMPS03 on the dashboard.


If you are having any trouble with above app, please e-mail max@zedomax.com

If you have any cool apps you'd like to share, please e-mail to max@zedomax.com




CUBLOC CB280 Source Download