Gyro_MPU3300

Dependencies:   Gyro_MPU3300 mbed

Dependents:   Gyro_MPU3300

Fork of MPU3300 by GOPA KUMAR K C

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include<mbed.h>
00002 #include "MPU3300.h"                 //header file which includes all the register addresses and
00003                     //Bit configuration of those registers
00004 SPI spi (PTD6,PTD7,PTD5) ;      // MOSI,MISO, CLOCK pins being used on                                                                                              //microcontroller(in order)     
00005 DigitalOut ssn (PTA13);         //Slave Select pin 
00006 Serial pc(USBTX,USBRX);
00007 InterruptIn dr(PTD2);       //Interrupt pin 
00008 Ticker tr;              //Ticker function to give values for limited amount of time
00009 uint8_t trFlag;         //ticker Flag 
00010 uint8_t drFlag;         //data-ready interrupt flag
00011 void trSub();               
00012 void drSub(); 
00013 void init_gyro();       
00014 void execute_gyro();
00015 int main(void)          
00016 {
00017     init_gyro();                //Initializing Gyroscope registers and setting them to our required configuration
00018     execute_gyro();         //Executing gyroscope for measuring angular rates
00019     return 0;
00020 }
00021 void drSub()            //In this function we setting data-ready flag to 1              
00022 {
00023     drFlag=1;
00024 }
00025 void trSub()                    //In this function we are setting ticker flag to 0
00026 {
00027     trFlag=0;
00028 }
00029 void init_gyro()
00030 {
00031     uint8_t response;               
00032     ssn=1;                  //Deselecting the chip 
00033     spi.format(8,3);                // Spi format is 8 bits, and clock mode 3 
00034     spi.frequency(1000000);     //frequency to be set as 1MHz
00035     drFlag=0;                   //Intially defining data-ready flag to be 0 
00036     dr.mode(PullDown);          
00037     dr.rise(&drSub);
00038     __disable_irq();
00039     
00040 /*Following the above mentioned algorithm for initializing the register and changing its configuration*/
00041     ssn=0;                      //Selecting chip(Mpu-3300)
00042     spi.write(USER_CTRL|READFLAG);   //sending USER_CTRL address with read bit
00043     response=spi.write(DUMMYBIT);   //sending dummy bit to get default values of the register
00044                         
00045     ssn=1;                  //Deselecting the chip  
00046     wait(0.1);                  //waiting according the product specifications 
00047     
00048     ssn=0;                  //again selecting the chip  
00049     spi.write(USER_CTRL);           //sending USER_CTRL address without read bit 
00050     spi.write(response|BIT_I2C_IF_DIS);  //disabling the I2C mode in the register
00051     ssn=1;                  //deselecting the chip 
00052     wait(0.1);                  // waiting for 100ms before going for another register 
00053     
00054     ssn=0;
00055     spi.write(PWR_MGMT_1|READFLAG); //Power Management register-1 
00056     response=spi.write(DUMMYBIT);
00057     ssn=1;
00058     wait(0.1);
00059         
00060     ssn=0;
00061     spi.write(PWR_MGMT_1);
00062     response=spi.write(response|BIT_CLKSEL_X);  //Selecting the X axis gyroscope as clock as mentioned above 
00063     ssn=1;                          
00064     wait(0.1);
00065     
00066     ssn=0;
00067     spi.write(GYRO_CONFIG|READFLAG); //sending GYRO_CONFIG address with read bit
00068     response=spi.write(DUMMYBIT);
00069     ssn=1;
00070     wait(0.1);
00071     
00072     ssn=0;
00073     spi.write(GYRO_CONFIG); //sending GYRO_CONFIG address to write to register
00074     spi.write(response&(~(BITS_FS_SEL_3|BITS_FS_SEL_4))); //selecting a full scale mode of +/=225 deg/sec
00075     ssn=1;
00076     wait(0.1);
00077     
00078     ssn=0;
00079     spi.write(CONFIG|READFLAG); //sending CONFIG address with read bit
00080     response=spi.write(DUMMYBIT);
00081     ssn=1;
00082     wait(0.1);
00083     
00084     ssn=0;
00085     spi.write(CONFIG); //sending CONFIG address to write to register
00086     spi.write(response|BITS_DLPF_CFG); //selecting a bandwidth of 42 hz and delay of 4.8ms
00087     ssn=1;
00088     wait(0.1);
00089     
00090     ssn=0;
00091     spi.write(SMPLRT_DIV|READFLAG); //sending SMPLRT_DIV address with read bit
00092     response=spi.write(DUMMYBIT);
00093     ssn=1;
00094     wait(0.1);
00095         
00096     ssn=0;
00097     spi.write(SMPLRT_DIV); //sending SMPLRT_DIV address to write to register
00098     spi.write(response&BITS_SMPLRT_DIV); //setting the sampling rate division to be 0 to make sample rate = gyroscopic output rate
00099     ssn=1;
00100     wait(0.1);
00101     
00102     ssn=0;
00103     spi.write(INT_ENABLE|READFLAG);       //sending address of INT_ENABLE with readflag
00104     response=spi.write(DUMMYBIT);              //sending dummy byte to get the default values of the
00105                                                                           // regiser
00106     ssn=1;   
00107     wait(0.1);
00108     
00109     ssn=0;
00110     spi.write(INT_ENABLE);                           //sending INT_ENABLE address to write to register
00111     spi.write(response|BIT_DATA_RDY_ENABLE);  //Enbling data ready interrupt
00112     ssn=1;
00113     wait(0.1);
00114     
00115     __enable_irq();
00116 }
00117 
00118 void execute_gyro()
00119 {
00120     uint8_t response;
00121     uint8_t MSB,LSB;
00122     int16_t bit_data;
00123     float data[3],error[3]={0,0,0}; //declaring error array to add to the values when required
00124     float senstivity = 145.6;     //senstivity is 145.6 for full scale mode of +/-225 deg/sec
00125     ssn=0;
00126     spi.write(PWR_MGMT_1|READFLAG); //sending address of INT_ENABLE with readflag
00127     response=spi.write(DUMMYBIT); //
00128     ssn=1;
00129     wait(0.1);
00130         
00131     ssn=0;
00132     spi.write(PWR_MGMT_1); //sending PWR_MGMT_1 address to write to register
00133     response=spi.write(response&(~(BIT_SLEEP))); //waking up the gyroscope from sleep
00134     ssn=1;
00135     wait(0.1);
00136     
00137     trFlag=1;
00138     tr.attach(&trSub,1); //executes the function trSub afer 1sec
00139     while(trFlag)
00140     {
00141         if(drFlag==1)
00142         {
00143             ssn=0;
00144             spi.write(GYRO_XOUT_H|READFLAG); //sending address of PWR_MGMT_1 with readflag
00145             for(int i=0;i<3;i++)
00146             {
00147                 MSB = spi.write(DUMMYBIT); //reading the MSB values of x,y and z respectively
00148                 LSB = spi.write(DUMMYBIT); //reading the LSB values of x,y and z respectively
00149                 bit_data= ((int16_t)MSB<<8)|LSB; //concatenating to get 16 bit 2's complement of the required gyroscope values
00150                 data[i]=(float)bit_data;
00151                 data[i]=data[i]/senstivity; //dividing with senstivity to get the readings in deg/sec
00152                 data[i]+=error[i]; //adding with error to remove offset errors
00153             }
00154             ssn=1;      
00155             for (int i=0;i<3;i++)
00156             {
00157                 printf("%f\t",data[i]); //printing the angular velocity values
00158             }
00159             printf("\n");
00160             break;
00161         }
00162             drFlag=0;
00163     }
00164     ssn=0;
00165     spi.write(PWR_MGMT_1|READFLAG); //sending address of PWR_MGMT_1 with readflag
00166     response=spi.write(DUMMYBIT);
00167     ssn=1;
00168     wait(0.1);
00169         
00170     ssn=0;
00171     spi.write(PWR_MGMT_1); //sending PWR_MGMT_1 address to write to register
00172     response=spi.write(response|BIT_SLEEP); //setting the gyroscope in sleep mode
00173     ssn=1;
00174     wait(0.1);
00175 }