Rob Toulson / Mbed 2 deprecated PE_13-01_CompassSetup

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Program Example 13.1: HMC6352 Compass Setup and Read
00002                                                             */
00003 #include "mbed.h"
00004 // mbed objects
00005 I2C compass(p28, p27);            // sda, scl
00006 Serial pc(USBTX, USBRX);          // tx, rx
00007 // variables
00008 const int addr = 0x42;            // define the I2C write Address
00009 char cmd[3];                      // command array for read and write
00010 float pos;                        // measured position
00011 
00012 // main code
00013 int main() {
00014   cmd[0] = 0x47;                // 'G' write to RAM address
00015   cmd[1] = 0x74;                // Operation mode register address
00016   cmd[2] = 0x72;                // Op mode = 20H, no S/R, continuous 
00017   compass.write(addr,cmd, 3);   // Send operation
00018   while (1) {
00019     compass.read(addr, cmd, 2);   // read the two-byte echo result
00020     pos = 0.1 * ((cmd[0] << 8) + cmd[1]);   //convert to degrees
00021     if (pos>180){
00022        pos=pos-360;
00023     }
00024     pc.printf("deg = %.1f\n", pos);
00025     wait(0.3);                    
00026   }
00027 }
00028