Advertisement
bluesky8059

MPU9250_SPI

May 30th, 2016
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.60 KB | None | 0 0
  1. /*********************************************************************
  2. Function : InitialSensor
  3. Purpose : initialization
  4. Parameter : none
  5. Return value : none
  6. *********************************************************************/
  7. void InitialSensor(int sample_rate_div,int low_pass_filter)
  8. {
  9.   U8 MPU_Init_Data[][3] = {
  10.         //{PWR_MGMT_1,        0x80, 1},     // Reset
  11.         {PWR_MGMT_1,        0x01, 100},     // Clock Source  
  12.         {PWR_MGMT_2,        0x00, 100},     // Enable Acc & Gyro
  13.         {CONFIG,            low_pass_filter, 100},
  14.         {GYRO_CONFIG,       0x18, 100},
  15.         {ACCEL_CONFIG,      0x08, 100},
  16.         {ACCEL_CONFIG_2,    0x09, 100},
  17.         {INT_PIN_CFG,       0x22, 100},
  18.        
  19.         {USER_CTRL,         0x20, 100},
  20.         {I2C_MST_CTRL,      0x0D, 100},
  21.        
  22.         {I2C_SLV0_ADDR, AK8963_I2C_ADDR, 100},
  23.        
  24.         {I2C_SLV0_REG, AK8963_CNTL2, 100},
  25.         {I2C_SLV0_DO,       0x01, 100},
  26.         {I2C_SLV0_CTRL,     0x81, 100},
  27.        
  28.         {I2C_SLV0_REG, AK8963_CNTL1, 100},
  29.         {I2C_SLV0_DO,       0x12, 100},
  30.         {I2C_SLV0_CTRL,     0x81, 100}
  31. };
  32.  
  33.     U8 i,j,u8InitCnt, id;
  34.     U8 u8len = sizeof(MPU_Init_Data) / 3;
  35.     unsigned char testAcc, testGyro;
  36.    
  37.     if(g_Main.SysPar.SysID > 0)
  38.     {
  39.         u8InitCnt = 1;
  40.     }
  41.     else
  42.     {
  43.         u8InitCnt = 18;
  44.     }
  45.    
  46.     SPI_CLK_SLOW();
  47.    
  48.     for(j=0; j<u8InitCnt; j++)
  49.     {
  50.         for(i=0; i<u8len; i++)
  51.         {
  52.             WriteReg(j,MPU_Init_Data[i][0], MPU_Init_Data[i][1]);
  53.             MPU_Init_Data[i][1] = 0;
  54.             // delay
  55.             Delayms(MPU_Init_Data[i][2]);
  56.         }
  57.         calib_mag(j);
  58.         set_acc_scale(j, BITS_FS_4G);
  59.         set_gyro_scale(j, BITS_FS_2000DPS);
  60.         id = AK8963_whoami(j);   //Only read 0x00
  61.     }
  62. }
  63.  
  64. /*********************************************************************
  65. Function : set_acc_scale
  66. Purpose : MPU9250's set acc scale
  67. Parameter :  int value
  68. Return value : none
  69. *********************************************************************/
  70. unsigned int set_acc_scale(U8 num, int scale)
  71. {
  72.   unsigned int temp_scale;
  73.     WriteReg(num, ACCEL_CONFIG, scale);
  74.    
  75.     switch (scale){
  76.         case BITS_FS_2G:
  77.             g_Main.acc_divider=16384;
  78.         break;
  79.         case BITS_FS_4G:
  80.             g_Main.acc_divider=8192;
  81.         break;
  82.         case BITS_FS_8G:
  83.             g_Main.acc_divider=4096;
  84.         break;
  85.         case BITS_FS_16G:
  86.             g_Main.acc_divider=2048;
  87.         break;  
  88.     }
  89.     temp_scale = ReadReg(num, ACCEL_CONFIG);
  90.    
  91.     switch (temp_scale){
  92.         case BITS_FS_2G:
  93.             temp_scale=2;
  94.         break;
  95.         case BITS_FS_4G:
  96.             temp_scale=4;
  97.         break;
  98.         case BITS_FS_8G:
  99.             temp_scale=8;
  100.         break;
  101.         case BITS_FS_16G:
  102.             temp_scale=16;
  103.         break;  
  104.     }
  105.     return temp_scale;
  106. }
  107.  
  108. /*-----------------------------------------------------------------------------------------------
  109.                                 GYROSCOPE SCALE
  110. usage: call this function at startup, after initialization, to set the right range for the
  111. gyroscopes. Suitable ranges are:
  112. BITS_FS_250DPS
  113. BITS_FS_500DPS
  114. BITS_FS_1000DPS
  115. BITS_FS_2000DPS
  116. returns the range set (250,500,1000 or 2000)
  117. -----------------------------------------------------------------------------------------------*/
  118. unsigned int set_gyro_scale(U8 num, int scale){
  119.     unsigned int temp_scale;
  120.      WriteReg(num, GYRO_CONFIG, scale);
  121.     switch (scale){
  122.         case BITS_FS_250DPS:
  123.             g_Main.gyro_divider=131;
  124.         break;
  125.         case BITS_FS_500DPS:
  126.             g_Main.gyro_divider=65.5;
  127.         break;
  128.         case BITS_FS_1000DPS:
  129.             g_Main.gyro_divider=32.8;
  130.         break;
  131.         case BITS_FS_2000DPS:
  132.             g_Main.gyro_divider=16.4;
  133.         break;  
  134.     }
  135.     //temp_scale=WriteReg(MPUREG_GYRO_CONFIG|READ_FLAG, 0x00);
  136.    
  137.     temp_scale = ReadReg(num, GYRO_CONFIG);
  138.     switch (temp_scale){
  139.         case BITS_FS_250DPS:
  140.             temp_scale=250;
  141.         break;
  142.         case BITS_FS_500DPS:
  143.             temp_scale=500;
  144.         break;
  145.         case BITS_FS_1000DPS:
  146.             temp_scale=1000;
  147.         break;
  148.         case BITS_FS_2000DPS:
  149.             temp_scale=2000;
  150.         break;  
  151.     }
  152.     return temp_scale;
  153. }
  154.  
  155. /*********************************************************************
  156. Function : MPU9250::AK8963_whoami
  157. Purpose : MPU9250's Calibration
  158. Parameter :  dest1(Acc), dest2(Gyro)
  159. Return value : none
  160. *********************************************************************/
  161. uint8_t AK8963_whoami(U8 num){
  162.     uint8_t response;
  163.     WriteReg(num, I2C_SLV0_ADDR,AK8963_I2C_ADDR|READ_FLAG); //Set the I2C slave addres of AK8963 and set for read.
  164.     WriteReg(num, I2C_SLV0_REG, AK8963_WIA); //I2C slave 0 register address from where to begin data transfer
  165.     WriteReg(num, I2C_SLV0_CTRL, 0x81); //Read 1 byte from the magnetometer
  166.  
  167.     //WriteReg(MPUREG_I2C_SLV0_CTRL, 0x81);    //Enable I2C and set bytes
  168.     Delayms(100);
  169.     WriteReg(num, EXT_SENS_DATA_00|READ_FLAG, 0x00);    //Read I2C
  170.     response = ReadReg(num, EXT_SENS_DATA_00);
  171.     //response=WriteReg(MPUREG_I2C_SLV0_DO, 0x00);    //Read I2C
  172.  
  173.     return response;
  174. }
  175.  
  176. /*********************************************************************
  177. Function : calib_mag
  178. Purpose : MPU9250's Magnetometer Calibration
  179. Parameter :  none
  180. Return value : none
  181. *********************************************************************/
  182. void calib_mag(U8 num){
  183.     uint8_t response[3];
  184.     float data;
  185.     int i;
  186.  
  187.     WriteReg(num, I2C_SLV0_ADDR,AK8963_I2C_ADDR|READ_FLAG); //Set the I2C slave addres of AK8963 and set for read.
  188.     WriteReg(num, I2C_SLV0_REG, AK8963_ASAX); //I2C slave 0 register address from where to begin data transfer
  189.     WriteReg(num, I2C_SLV0_CTRL, 0x83); //Read 3 bytes from the magnetometer
  190.  
  191.     //WriteReg(MPUREG_I2C_SLV0_CTRL, 0x81);    //Enable I2C and set bytes
  192.     //delayMicroseconds(1000);
  193.     //response[0]=WriteReg(MPUREG_EXT_SENS_DATA_01|READ_FLAG, 0x00);    //Read I2C
  194.    
  195.     CS_Func(LOW,num);
  196.     if(num == 0)
  197.     {
  198.         UCB1BurstRead(EXT_SENS_DATA_00, 3, &response[0]); // read FIFO sample count
  199.     }
  200.     else
  201.     {
  202.         UCA0BurstRead(EXT_SENS_DATA_00, 3, &response[0]);
  203.     }
  204.     CS_Func(HIGH,num);
  205.     //ReadRegs(MPUREG_EXT_SENS_DATA_00,response,3);
  206.    
  207.     //response=WriteReg(MPUREG_I2C_SLV0_DO, 0x00);    //Read I2C
  208.     for(i = 0; i < 3; i++) {
  209.         data=response[i];
  210.         g_Main.Magnetometer_ASA[i] = ((data-128)/256+1)*Magnetometer_Sensitivity_Scale_Factor;
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement