Check program for 9DOF stick from Sparkfun.com

Dependencies:   mbed ITG3200 HMC5843 ADXL345

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "HMC5843.h"
00003 #include "ADXL345.h"
00004 #include "ITG3200.h"
00005 
00006 DigitalOut myled(LED1);
00007 
00008 HMC5843 cmp(p28, p27);      // sda, scl
00009 ADXL345 acc(p28, p27);      // sda, scl
00010 ITG3200 gyr(p28, p27);      // sda, scl
00011 Serial pc(USBTX, USBRX);    // tx, rx
00012 
00013 int main() {
00014     pc.baud(115200);
00015     //values x,y,z
00016     int readings[3];
00017     //ID Buffer
00018     char buffer[3];
00019     
00020     pc.printf("%c" ,13,10,13,10,13,10);
00021     
00022     // do init stuff
00023     //Continuous mode, , 10Hz measurement rate.
00024     // HMC5843_CONTINUOUS, HMC5843_10HZ_NORMAL HMC5843_1_0GA
00025     cmp.setDefault();
00026     //Wait some time(Need at least 5ms)
00027     wait(0.1);
00028     cmp.getAddress(buffer);
00029     pc.printf("cmp Id=%c%c%c \n\r",buffer[0],buffer[1],buffer[2]);
00030  
00031     // These are here to test whether any of the initialization fails. It will print the failure
00032     if (acc.setPowerControl(0x00)){
00033          pc.printf("acc: didn't intitialize power control\n"); 
00034          return 0;  }
00035     wait(.001);
00036     
00037     //Full resolution, +/-16g, 4mg/LSB.
00038     if(acc.setDataFormatControl(0x0B)){
00039         pc.printf("didn't set data format\n");
00040         return 0;  }
00041     wait(.001);
00042      
00043     //3.2kHz data rate.
00044     if(acc.setDataRate(ADXL345_3200HZ)){
00045         pc.printf("didn't set data rate\n");
00046         return 0;    }
00047     wait(.001);
00048      
00049     //Measurement mode.
00050     if(acc.setPowerControl(MeasurementMode)) {
00051         pc.printf("didn't set the power control to measurement\n"); 
00052         return 0;   } 
00053 
00054     pc.printf("Acc Id=%x \n\r", acc.getDeviceID());
00055     pc.printf("%c" ,13,10);
00056 
00057     //Set highest bandwidth.
00058     gyr.setLpBandwidth(LPFBW_256HZ);
00059     pc.printf("Gyro Id=%x \n\r", gyr.getWhoAmI());
00060     pc.printf("%c" ,13,10);
00061 
00062     wait(1);
00063 
00064     while (1) {
00065 
00066         cmp.readData(readings);
00067         pc.printf("C %+4i %+4i %+4i",(int16_t)readings[0],(int16_t)readings[1],(int16_t)readings[2]);
00068         wait(0.05);
00069 
00070         acc.getOutput(readings);
00071         pc.printf(" A %+5i %+5i %+5i",(int16_t)readings[0],(int16_t)readings[1],(int16_t)readings[2]);
00072         wait(0.05);
00073         
00074         pc.printf(" G %+5i %+5i %+5i",(int16_t)gyr.getGyroX(),(int16_t)gyr.getGyroY(),(int16_t)gyr.getGyroZ());
00075         pc.printf("%c" ,13,10);        
00076         wait(0.05);
00077         
00078         if (myled) {
00079             myled=0;
00080         } else {
00081             myled=1;
00082         }
00083         
00084     }
00085 }