I2C Digital Compass Programming

I was looking around Newark InOne and found a digital compass.

I read the data sheet and it looks like it will interface nicely with are Robot Controller.
My only problem is going to be figuring out how to program the I2C interface. I have read the PIC datasheet on using the I2C interface but I have not been able to get a good grasp on how I would go about programming it. Does anyone have any examples of programming this sensor or setting up an I2C interface in general?

Well you are in luck. There is a prewritten library of I2C functions that comes with the compiler. They are documented in the following link:

Also, source code for all of these functions can be found in the C18 install directory assuming you selected it when you installed C18.

Compass you say?


We tried that a few years back. Look for mention of the I.D.A.N. system.

Note the accuracy in the data sheet. Don’t confuse resolution and accuracy. I’ve been playing with a PNI compass board. I I’ve got it working to with in 1deg accuracy. However to get this accuracy the are only 2 readings per second. They’re slow devices. Enable the set reset register. The board has to be at least 1.5 away from any motors or solenoids. The more the better.
1 degree of error over the length of a playing field is allot. It may have it’s uses.

1.5 what?

Feet. More if the motors are under high load. The drill motors really put out magnetic distortions. Cims aren’t nearly as bad. Fortunately the magnetic field from the motors is not uniform.

The hardware I2C interface is unavailable because the Master Synchronous Serial Port Module is being used for the intraprocessor SPI link.


Hrmm… True… This will make things harder but not impossible. You will not be able to take advantage of the built in hardware and will have to “bitbang” the I2C interface. The good new is that I2C is synchronous and so timing isn’t critical. If you look in the document i listed earlier, you will see that they have some basic I2C bitbanging functions described in section 3.5.