José Claudio / Mbed 2 deprecated ReadingGyro_ITG3205

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ITG3205.cpp Source File

ITG3205.cpp

00001 
00002 #include "ITG3205.h"
00003 
00004 ITG3205::ITG3205(PinName sda, PinName scl) : i2c(sda, scl) 
00005 {
00006     char rx;
00007     //400kHz, fast mode.
00008     i2c.frequency(400000);
00009     //===========================================================================================================
00010     // Read chip_id
00011     //===========================================================================================================
00012     rx = Read(ITG3205_WHO_AM_I);
00013     if (rx != 0x68 && rx!= 0x69)                      // Data sheet say's "The slave address of the ITG-3205
00014         printf("\ninvalid chip id %d\r\n", rx);       // devices is b110100X". This could be either, 0x68 or 0x69
00015     //===========================================================================================================
00016     // Let's reset the chip to it's default configuration
00017     //===========================================================================================================
00018     // The PWR_MGM (0x3E) is composed of six parameters, to reset is necessary to set the H_RESET (bit 7)
00019     //  PWR_MGM Register |===============================================|
00020     //    bits           |    7      6      5       4       3    2  1  0 |
00021     //                   | H_RESET SLEEP STBY_XG STBY_YG STBY_ZG CLK_SEL |
00022     //                   |    1      0      0       0       0    0  0  0 |
00023     //                   |===============================================|
00024     Write(ITG3205_PWRM, 0x80);
00025     //==========================================================================================================
00026     // Let's define the low-pass filter's bandwidth to 42Hz (DLPF_CFG<2:0> -> 011) with internal sample rate of 
00027     // 1kHz, to reduce noise in the signal, and also, let's define the gyro's sensors (FS_SELT<4:3> -> 11) to 
00028     // FullScale Range (+/- 2000º/sec).
00029     // To set the DLPF_CFG bit, we will have to acces the DLP_FS register (0x16) 
00030     //==========================================================================================================
00031     // The DLP_FS is composed of the FS_SEL (Bit 4 and 3) and the DLPF_CFG  (Bit 1 and 0)
00032     //  DPL_FS Register |================================|
00033     //    bits          | 7   6   5  |4    3| |2   1  0| |
00034     //                  | X   X   X  |FS_SEL| |DLPF_CFG| |
00035     //                  | 0   0   0   1    1   0   1  1  | -> (0x1B) Sets the gyro to FullScale Range (+/- 2000º/sec) and Low-Pass Filter 
00036     //                  |================================|           with bandwidth of 42Hz and internal sample rate of 1kHz.
00037     Write(ITG3205_DLPFS, 0x1B);
00038     //Let's check
00039     rx = Read(ITG3205_DLPFS);                                       // procedure: readed value: i.e XXX1 1110 -> The bits that we want to check are the bits 0,1,2,3 and 4, so we have to eliminate the other bits      
00040     rx &= (~0xE0); //Mask                                           //                        (AND) 0001 1111 -> (~DLPFSMask)
00041     if(rx != 0x1B)                                                  //                              ---------
00042         printf("DPL_FS register has not been written: %d\r\n", rx); //                              0001 1110 -> Final value, compare with the wanted value (this one doesn't match)
00043     //==========================================================================================================
00044     // Now let's define the sample rate to 5msec. 
00045     // To do this, a formula is applied: Fsample = Finternal/(x+1) on wich Fsample is the inverse o our sample rate, 
00046     // and Finternal is equal to 1kHz, as defined in the low-pass filter's bandwidth.
00047     // The SMPLRT register (0x15) controls the sample rate, so we must set this register to 4, as the result of x in the
00048     // equation.
00049     //==========================================================================================================
00050     /*
00051     Write(ITG3205_SMPLRT, 0x04);
00052     //Let's check
00053     rx = Read(ITG3205_SMPLRT);                                     
00054     if(rx != 0x04)                                                 
00055         printf("SMPLRT register has not been written: %d\r\n", rx); 
00056     */
00057     //==========================================================================================================
00058     // Generate interrupt when device is ready or raw data ready
00059     //==========================================================================================================
00060     // The PWR_MGM is composed of six parameters, to reset is necessary to set the H_RESET (bit 7)
00061     //  PWR_MGM Register |==========================================================================|
00062     //    bits           |   7     6        5                4         3      2       1       0     |
00063     //                   | ACTL  OPEN  LATCH_INT_EN  INT_ANYRD_2CLEAR  0  ITG_RDY_EN  0  RAW_RDY_EN |
00064     //                   |   0     0        0                0         0      1       0       1     | -> 0x05
00065     //                   |==========================================================================|
00066     //Write(ITG3205_INT_CFG, 0x05);  
00067     Write(ITG3205_INT_CFG, 0x00);  
00068     //==========================================================================================================
00069     // Back the PWR_MGM to what it was
00070     //==========================================================================================================
00071     Write(ITG3205_PWRM, 0x00);
00072 }
00073 
00074 
00075 
00076 void ITG3205::Write(char reg, char data)
00077 {
00078     char c_data[2];
00079     c_data[0] = reg;
00080     c_data[1] = data;
00081     i2c.write((ITG3205_ADDR << 1), c_data, 2); 
00082 }
00083 
00084 char ITG3205::Read(char data)
00085 {
00086     char tx = data;
00087     char rx;
00088     
00089     i2c.write((ITG3205_ADDR << 1) & 0xFE, &tx, 1); // 0xFE ensure that the MSB bit is being set to zero (RW=0 -> Writing)
00090     i2c.read((ITG3205_ADDR << 1) | 0x01, &rx, 1);  // 0x01 ensure that the MSB bit is being set to one  (RW=1 -> Reading)
00091                                              // The read/write method of I2C does this automatically, so it's useless to set manually    
00092     return rx;
00093 }
00094 /*
00095 int ITG3205::getInternalSampleRate(void){
00096 
00097     char tx = ITG3205_DLPFS;
00098     char rx;
00099     
00100     i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
00101     
00102     i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
00103     
00104     //DLPF_CFG == 0 -> sample rate = 8kHz.
00105     if(rx == 0){
00106         return 8;
00107     } 
00108     //DLPF_CFG = 1..7 -> sample rate = 1kHz.
00109     else if(rx >= 1 && rx <= 7){
00110         return 1;
00111     }
00112     //DLPF_CFG = anything else -> something's wrong!
00113     else{
00114         return -1;
00115     }
00116     
00117 }
00118 */
00119 bool ITG3205::isRawReady()
00120 {
00121     if(Read(ITG3205_INT_STATUS) == 0x01)
00122         return true;
00123     else
00124         return false;
00125         
00126 }
00127 
00128 float ITG3205::getGyroX(void)
00129 {
00130     char lsb_byte = 0;
00131     signed short msb_byte;
00132     float acc;
00133     
00134     lsb_byte = Read(ITG3205_XMSB);
00135     msb_byte = lsb_byte << 8;
00136     msb_byte |= Read(ITG3205_XLSB);
00137     acc = (float)msb_byte;
00138     return acc;
00139     /*
00140     char tx = GYRO_XOUT_H_REG;
00141     char rx[2];
00142     
00143     i2c.write((ITG3205_ADDR << 1) & 0xFE, &tx, 1);
00144     i2c.read((ITG3205_ADDR << 1) | 0x01, rx, 2);
00145     
00146     int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
00147     
00148     return output;
00149     */
00150 }
00151 
00152 float ITG3205::getGyroY(void)
00153 {
00154     char lsb_byte = 0;
00155     signed short msb_byte;
00156     float acc;
00157     
00158     lsb_byte = Read(ITG3205_YMSB);
00159     msb_byte = lsb_byte << 8;
00160     msb_byte |= Read(ITG3205_YLSB);
00161     acc = (float)msb_byte;
00162     return acc;
00163     /*
00164     char tx = GYRO_YOUT_H_REG;
00165     char rx[2];
00166     
00167     i2c.write((ITG3205_ADDR << 1) & 0xFE, &tx, 1);
00168     
00169     i2c.read((ITG3205_ADDR << 1) | 0x01, rx, 2);
00170     
00171     int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
00172     
00173     return output;
00174     */
00175 }
00176 
00177 float ITG3205::getGyroZ(void)
00178 {
00179     char lsb_byte = 0;
00180     signed short msb_byte;
00181     float acc;
00182     
00183     lsb_byte = Read(ITG3205_ZMSB);
00184     msb_byte = lsb_byte << 8;
00185     msb_byte |= Read(ITG3205_ZLSB);
00186     acc = (float)msb_byte;
00187     return acc;
00188     /*
00189     char tx = GYRO_ZOUT_H_REG;
00190     char rx[2];
00191     
00192     i2c.write((ITG3205_ADDR << 1) & 0xFE, &tx, 1);
00193     
00194     i2c.read((ITG3205_ADDR << 1) | 0x01, rx, 2);
00195     
00196     int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
00197     
00198     return output;
00199     */
00200 }