Connecting the CMPS03 to the BS2
Introduction
The compass module uses either a PWM output or the I2C bus for communications.
This example uses the I2C Bus. The SDA
(data) and SCL (clock) lines are connected to pins P8 and P9 on the BS2. These
are the pins used by the BS2p for the I2CIN and I2COUT commands. The I2CIN and I2COUT
commands do not work correctly on early BS2p modules as they did not implement
the I2C SCL bus hold feature. Later BS2p modules (from around late 2002 onwards)
are fine. If you have an old BS2p or one of the other BS2 variants, you can
still use the following code.
This example uses a combination of bit
bashing and the SHIFTIN and SHIFTOUT commands. It has been tested with the BS2 and BS2p.
The BS2 internal 5v regulator is not suitable for powering much external
circuitry. I therefore recommend you use a separate 5v regulator.
Circuit Schematic for connecting the BS2 Stamp to the CMPS03 Compass Module using the I2C bus
Note the switch connecting pin 6 to Ground. This is all that is needed for calibration of Rev7 Firmware.
Software for BS2, BS2e, BS2sx and BS2p
The sample code below displays the compass bearing as both BRAD's (0-255) and
Degrees (0-359) in a Debug window on the PC.
It can be downloaded here
'{$STAMP BS2}
'*********************************************************** loop var
byte
' just a looping counter Main: goto main '-------------------------------------------------------------------------------------------- I2cByteWrite:
' writes I2cData.lowbyte to I2cReg at I2cAddr I2cWordWrite:
' writes I2cData to I2cReg at I2cAddr I2CByteRead: I2CWordRead: I2cOutByte: I2cInByte: I2cStop:
' I2C stop bit sequence |