伊生希 小林 / Mbed 2 deprecated cansat_main_copy

Dependencies:   mbed SDFileSystem MS5607 ADXL345_I2C FATFileSystem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HMC5883L.cpp Source File

HMC5883L.cpp

00001 #include "HMC5883L.h"
00002 
00003 HMC5883L::HMC5883L(PinName sda, PinName scl): i2c(sda, scl)
00004 {
00005     //100KHz, as specified by the datasheet.
00006     char rx;
00007 
00008     
00009     i2c.frequency(100000);
00010     //Testar depois com 400KHz
00011     //==========================================================================================================
00012     // Read chip_id
00013     //==========================================================================================================
00014     rx = Read(HMC5883L_IDENT_A);
00015     if (rx != 0x48)//ID do chip
00016         printf("\ninvalid chip id %d\r\n", rx); 
00017      
00018     //==========================================================================================================
00019     // Let's set the Configuration Register A
00020     //==========================================================================================================
00021     // This register set's the number of samples averaged per measurement output, the rate at which data is written
00022     // to all three data output registers and the measurement flow of the device.
00023     // -------------------------------------------------------
00024     // |CRA7 CRA6   CRA5   CRA4   CRA3   CRA2   CRA1   CRA0  |
00025     // |(1)  MA1(1) MA0(1) DO2(1) DO1(0) DO0(0) MS1(0) MS0(0)| -> This is the default value
00026     // -------------------------------------------------------
00027     // CRA7 -> we have to clear this bit for correct operation                                      (0)
00028     // CRA6 to CRA5 -> Let's select the maximum number of samples averaged per measurement output   (11)
00029     // CRA4 to CRA2 -> Also let's select the maximum data output rate                               (110)
00030     // CRA1 to CRA0 -> The measurement flow is defined to normal                                    (00)
00031     // -------------------------------------------------------
00032     // |CRA7 CRA6   CRA5   CRA4   CRA3   CRA2   CRA1   CRA0  |
00033     // |(0)  MA1(1) MA0(1) DO2(1) DO1(1) DO0(0) MS1(0) MS0(0)| -> This is the new value, 0x78 in hex
00034     // -------------------------------------------------------
00035     //Write(HMC5883L_CONFIG_A,0x78);
00036     //Write(HMC5883L_CONFIG_A,0x70);
00037         
00038     //==========================================================================================================
00039     // The Configuration Register B is set to 0010 0000 by default, this is a +/- 1.3 Ga sensor field range and
00040     // the gain of LSB/gauss is 1090. This is the maximum value, so let's leave it like that.
00041     //==========================================================================================================
00042     //Datasheet page 13. I will explain later
00043     //Write(HMC5883L_CONFIG_B,0x20);
00044     //Write(HMC5883L_CONFIG_B,0xA0);
00045   
00046     //==========================================================================================================
00047     // Let's set the Mode Register
00048     //==========================================================================================================
00049     // This register set's the operation mode, from continuous-measurements mode, single-measurement mode and idle mode.
00050     // We will set to Continuouse-measurement mode, so the device continuously performs measurements and places the
00051     // result in the data register
00052     // ---------------------------------------------
00053     // |MR7  MR6  MR5  MR4  MR3  MR2  MR1    MR0   |  -> This is the new value, 0x78 in hex, we are going to change
00054     // |(1)  (0)  (0)  (0)  (0)  (0)  MD1(0) MD0(1)|     the MD1 and MD0 to 00 and clear the MR7 for correct operation.
00055     // ---------------------------------------------     The final value is 0000 0000 (0x00).
00056     Write(HMC5883L_MODE,0x00);
00057 }
00058 
00059 
00060 void HMC5883L::Write(char reg_address, char data)
00061 {
00062     char tx[2];
00063     tx[0]=reg_address;
00064     tx[1]=data;
00065 
00066     i2c.write(HMC5883L_I2C_WRITE,tx,2);
00067 }
00068 
00069 char HMC5883L::Read(char data)
00070 {
00071     char tx = data;
00072     char rx;
00073 
00074     i2c.write(HMC5883L_I2C_WRITE, &tx, 1);
00075     i2c.read(HMC5883L_I2C_READ, &rx, 1);
00076     return rx;
00077 }
00078 
00079 void HMC5883L::MultiByteRead(char address, char* output, int size) 
00080 {
00081     i2c.write(HMC5883L_I2C_WRITE, &address, 1);      //tell it where to read from
00082     i2c.read(HMC5883L_I2C_READ, output, size);     //tell it where to store the data read
00083 }
00084 
00085 float HMC5883L::getMx()
00086 {
00087     //return (x * m_Scale);
00088     char lsb_byte = 0;
00089     signed short msb_byte;
00090 
00091     lsb_byte = Read(HMC5883L_X_MSB);
00092     msb_byte = lsb_byte << 8;
00093     msb_byte |= Read(HMC5883L_X_LSB);
00094     return (float)msb_byte;
00095     /*
00096     char tx[1];
00097     char rx[2];
00098 
00099 
00100     tx[0]=HMC5883L_X_MSB;
00101     i2c.write(HMC5883L_I2C_READ,tx,1);
00102     i2c.read(HMC5883L_I2C_READ,rx,2);
00103     return ((int)rx[0]<<8|(int)rx[1]);
00104     */
00105 
00106 }
00107 
00108 float HMC5883L::getMy()
00109 {
00110     //return (y * m_Scale);
00111 
00112     char lsb_byte = 0;
00113     signed short msb_byte;
00114 
00115     lsb_byte = Read(HMC5883L_Y_MSB);
00116     msb_byte = lsb_byte << 8;
00117     msb_byte |= Read(HMC5883L_Y_LSB);
00118     return (float)msb_byte;
00119 }
00120 
00121 
00122 float HMC5883L::getMz()
00123 {
00124     //return (z * m_Scale);
00125     
00126     char lsb_byte = 0;
00127     signed short msb_byte;
00128 
00129     lsb_byte = Read(HMC5883L_Z_MSB);
00130     msb_byte = lsb_byte << 8;
00131     msb_byte |= Read(HMC5883L_Z_LSB);
00132     return (float)msb_byte;
00133  }