ICM20602_I2C(invensense)---stm32f767zi

Dependents:   IMU_fusion_9DOF

Fork of NTOUEE-mbed-I2C_MPU6500 by Richard Kuo

Committer:
sarahbest
Date:
Wed Jul 19 07:56:49 2017 +0000
Revision:
2:74690f762c0f
icm20602 ouput 7 variables: acc 3, gyro 3, temp

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sarahbest 2:74690f762c0f 1 #include "mbed.h"
sarahbest 2:74690f762c0f 2 #include "icm20602_i2c.h"
sarahbest 2:74690f762c0f 3 //Serial pc1(USBTX,USBRX);
sarahbest 2:74690f762c0f 4 I2C ICM20602_i2c(D14,D15); // I2C0_SDA, I2C0_SCL
sarahbest 2:74690f762c0f 5
sarahbest 2:74690f762c0f 6 // Acc Full Scale Range +-2G 4G 8G 16G
sarahbest 2:74690f762c0f 7 enum Ascale
sarahbest 2:74690f762c0f 8 {
sarahbest 2:74690f762c0f 9 AFS_2G=0,
sarahbest 2:74690f762c0f 10 AFS_4G,
sarahbest 2:74690f762c0f 11 AFS_8G,
sarahbest 2:74690f762c0f 12 AFS_16G
sarahbest 2:74690f762c0f 13 };
sarahbest 2:74690f762c0f 14
sarahbest 2:74690f762c0f 15 // Gyro Full Scale Range +-250 500 1000 2000 Degrees per second
sarahbest 2:74690f762c0f 16 enum Gscale
sarahbest 2:74690f762c0f 17 {
sarahbest 2:74690f762c0f 18 GFS_250DPS=0,
sarahbest 2:74690f762c0f 19 GFS_500DPS,
sarahbest 2:74690f762c0f 20 GFS_1000DPS,
sarahbest 2:74690f762c0f 21 GFS_2000DPS
sarahbest 2:74690f762c0f 22 };
sarahbest 2:74690f762c0f 23
sarahbest 2:74690f762c0f 24 // Scale resolutions per LSB for the sensors
sarahbest 2:74690f762c0f 25 float aRes, gRes;
sarahbest 2:74690f762c0f 26 int Ascale = AFS_2G;
sarahbest 2:74690f762c0f 27 int Gscale = GFS_1000DPS;
sarahbest 2:74690f762c0f 28
sarahbest 2:74690f762c0f 29 void ICM20602_WriteByte(uint8_t ICM20602_reg, uint8_t ICM20602_data)
sarahbest 2:74690f762c0f 30 {
sarahbest 2:74690f762c0f 31 char data_out[2];
sarahbest 2:74690f762c0f 32 data_out[0]=ICM20602_reg;
sarahbest 2:74690f762c0f 33 data_out[1]=ICM20602_data;
sarahbest 2:74690f762c0f 34 ICM20602_i2c.write(ICM20602_slave_addr, data_out, 2, 0);
sarahbest 2:74690f762c0f 35 }
sarahbest 2:74690f762c0f 36
sarahbest 2:74690f762c0f 37 uint8_t ICM20602_ReadByte(uint8_t ICM20602_reg)
sarahbest 2:74690f762c0f 38 {
sarahbest 2:74690f762c0f 39 char data_out[1], data_in[1];
sarahbest 2:74690f762c0f 40 data_out[0] = ICM20602_reg;
sarahbest 2:74690f762c0f 41 ICM20602_i2c.write(ICM20602_slave_addr, data_out, 1, 1);
sarahbest 2:74690f762c0f 42 ICM20602_i2c.read(ICM20602_slave_addr, data_in, 1, 0);
sarahbest 2:74690f762c0f 43 return (data_in[0]);
sarahbest 2:74690f762c0f 44 }
sarahbest 2:74690f762c0f 45
sarahbest 2:74690f762c0f 46 // Communication test: WHO_AM_I register reading
sarahbest 2:74690f762c0f 47 void ICM20602::whoAmI()
sarahbest 2:74690f762c0f 48 {
sarahbest 2:74690f762c0f 49 uint8_t whoAmI = ICM20602_ReadByte(ICM20602_WHO_AM_I); // Should return 0x68
sarahbest 2:74690f762c0f 50 pc.printf("I AM ICM20602: 0x%x \r\n",whoAmI);
sarahbest 2:74690f762c0f 51
sarahbest 2:74690f762c0f 52 if(whoAmI==0x12)//0x68)
sarahbest 2:74690f762c0f 53 {
sarahbest 2:74690f762c0f 54 pc.printf("ICM20602 is online... \r\n");
sarahbest 2:74690f762c0f 55 // led2=1;
sarahbest 2:74690f762c0f 56 // ledToggle(2);
sarahbest 2:74690f762c0f 57 }
sarahbest 2:74690f762c0f 58 else
sarahbest 2:74690f762c0f 59 {
sarahbest 2:74690f762c0f 60 pc.printf("Could not connect to ICM20602 \r\nCheck the connections... \r\n");
sarahbest 2:74690f762c0f 61 // toggler1.attach(&toggle_led1,0.1); // toggles led1 every 100 ms
sarahbest 2:74690f762c0f 62 }
sarahbest 2:74690f762c0f 63
sarahbest 2:74690f762c0f 64 }
sarahbest 2:74690f762c0f 65
sarahbest 2:74690f762c0f 66 void ICM20602::init()
sarahbest 2:74690f762c0f 67 {
sarahbest 2:74690f762c0f 68 ICM20602_i2c.frequency(400000);
sarahbest 2:74690f762c0f 69 ICM20602_WriteByte(ICM20602_PWR_MGMT_1, 0x00); // CLK_SEL=0: internal 8MHz, TEMP_DIS=0, SLEEP=0
sarahbest 2:74690f762c0f 70 ICM20602_WriteByte(ICM20602_SMPLRT_DIV, 0x07); // Gyro output sample rate = Gyro Output Rate/(1+SMPLRT_DIV)
sarahbest 2:74690f762c0f 71 ICM20602_WriteByte(ICM20602_CONFIG, 0x01); //176Hz // set TEMP_OUT_L, DLPF=3 (Fs=1KHz):0x03
sarahbest 2:74690f762c0f 72 // ICM20602_WriteByte(ICM20602_GYRO_CONFIG, 0x00); // bit[4:3] 0=+-250d/s,1=+-500d/s,2=+-1000d/s,3=+-2000d/s :0x18
sarahbest 2:74690f762c0f 73 // ICM20602_WriteByte(ICM20602_ACCEL_CONFIG, 0x00);// bit[4:3] 0=+-2g,1=+-4g,2=+-8g,3=+-16g, ACC_HPF=On (5Hz):0x01
sarahbest 2:74690f762c0f 74 setAccRange(Ascale);
sarahbest 2:74690f762c0f 75 setGyroRange(Gscale);
sarahbest 2:74690f762c0f 76 }
sarahbest 2:74690f762c0f 77
sarahbest 2:74690f762c0f 78 int16_t ICM20602::getAccXvalue()
sarahbest 2:74690f762c0f 79 {
sarahbest 2:74690f762c0f 80 uint8_t LoByte, HiByte;
sarahbest 2:74690f762c0f 81 LoByte = ICM20602_ReadByte(ICM20602_ACCEL_XOUT_L); // read Accelerometer X_Low value
sarahbest 2:74690f762c0f 82 HiByte = ICM20602_ReadByte(ICM20602_ACCEL_XOUT_H); // read Accelerometer X_High value
sarahbest 2:74690f762c0f 83 return((HiByte<<8) | LoByte);
sarahbest 2:74690f762c0f 84 // pc1.printf("accx:%d,%d\r\n",HiByte,LoByte); // send data to matlab
sarahbest 2:74690f762c0f 85 }
sarahbest 2:74690f762c0f 86
sarahbest 2:74690f762c0f 87 int16_t ICM20602::getAccYvalue()
sarahbest 2:74690f762c0f 88 {
sarahbest 2:74690f762c0f 89 uint8_t LoByte, HiByte;
sarahbest 2:74690f762c0f 90 LoByte = ICM20602_ReadByte(ICM20602_ACCEL_YOUT_L); // read Accelerometer X_Low value
sarahbest 2:74690f762c0f 91 HiByte = ICM20602_ReadByte(ICM20602_ACCEL_YOUT_H); // read Accelerometer X_High value
sarahbest 2:74690f762c0f 92 return ((HiByte<<8) | LoByte);
sarahbest 2:74690f762c0f 93 }
sarahbest 2:74690f762c0f 94
sarahbest 2:74690f762c0f 95 int16_t ICM20602::getAccZvalue()
sarahbest 2:74690f762c0f 96 {
sarahbest 2:74690f762c0f 97 uint8_t LoByte, HiByte;
sarahbest 2:74690f762c0f 98 LoByte = ICM20602_ReadByte(ICM20602_ACCEL_ZOUT_L); // read Accelerometer X_Low value
sarahbest 2:74690f762c0f 99 HiByte = ICM20602_ReadByte(ICM20602_ACCEL_ZOUT_H); // read Accelerometer X_High value
sarahbest 2:74690f762c0f 100 return ((HiByte<<8) | LoByte);
sarahbest 2:74690f762c0f 101 }
sarahbest 2:74690f762c0f 102
sarahbest 2:74690f762c0f 103 int16_t ICM20602::getGyrXvalue()
sarahbest 2:74690f762c0f 104 {
sarahbest 2:74690f762c0f 105 uint8_t LoByte, HiByte;
sarahbest 2:74690f762c0f 106 LoByte = ICM20602_ReadByte(ICM20602_GYRO_XOUT_L); // read Accelerometer X_Low value
sarahbest 2:74690f762c0f 107 HiByte = ICM20602_ReadByte(ICM20602_GYRO_XOUT_H); // read Accelerometer X_High value
sarahbest 2:74690f762c0f 108 return ((HiByte<<8) | LoByte);
sarahbest 2:74690f762c0f 109 }
sarahbest 2:74690f762c0f 110
sarahbest 2:74690f762c0f 111 int16_t ICM20602::getGyrYvalue()
sarahbest 2:74690f762c0f 112 {
sarahbest 2:74690f762c0f 113 uint8_t LoByte, HiByte;
sarahbest 2:74690f762c0f 114 LoByte = ICM20602_ReadByte(ICM20602_GYRO_YOUT_L); // read Accelerometer X_Low value
sarahbest 2:74690f762c0f 115 HiByte = ICM20602_ReadByte(ICM20602_GYRO_YOUT_H); // read Accelerometer X_High value
sarahbest 2:74690f762c0f 116 return ((HiByte<<8) | LoByte);
sarahbest 2:74690f762c0f 117 }
sarahbest 2:74690f762c0f 118
sarahbest 2:74690f762c0f 119 int16_t ICM20602::getGyrZvalue()
sarahbest 2:74690f762c0f 120 {
sarahbest 2:74690f762c0f 121 uint8_t LoByte, HiByte;
sarahbest 2:74690f762c0f 122 LoByte = ICM20602_ReadByte(ICM20602_GYRO_ZOUT_L); // read Accelerometer X_Low value
sarahbest 2:74690f762c0f 123 HiByte = ICM20602_ReadByte(ICM20602_GYRO_ZOUT_H); // read Accelerometer X_High value
sarahbest 2:74690f762c0f 124 return ((HiByte<<8) | LoByte);
sarahbest 2:74690f762c0f 125 }
sarahbest 2:74690f762c0f 126
sarahbest 2:74690f762c0f 127 int16_t ICM20602::getIMUTemp()
sarahbest 2:74690f762c0f 128 {
sarahbest 2:74690f762c0f 129 uint8_t LoByte, HiByte;
sarahbest 2:74690f762c0f 130 LoByte = ICM20602_ReadByte(ICM20602_TEMP_OUT_L); // read Accelerometer X_Low value
sarahbest 2:74690f762c0f 131 HiByte = ICM20602_ReadByte(ICM20602_TEMP_OUT_H); // read Accelerometer X_High value
sarahbest 2:74690f762c0f 132 return ((HiByte<<8) | LoByte);
sarahbest 2:74690f762c0f 133 }
sarahbest 2:74690f762c0f 134
sarahbest 2:74690f762c0f 135
sarahbest 2:74690f762c0f 136 // Calculates Acc resolution
sarahbest 2:74690f762c0f 137 float ICM20602::setAccRange(int Ascale)
sarahbest 2:74690f762c0f 138 {
sarahbest 2:74690f762c0f 139 switch(Ascale)
sarahbest 2:74690f762c0f 140 {
sarahbest 2:74690f762c0f 141 case AFS_2G:
sarahbest 2:74690f762c0f 142 aRes = 2.0/32768.0;
sarahbest 2:74690f762c0f 143 break;
sarahbest 2:74690f762c0f 144 case AFS_4G:
sarahbest 2:74690f762c0f 145 aRes = 4.0/32768.0;
sarahbest 2:74690f762c0f 146 break;
sarahbest 2:74690f762c0f 147 case AFS_8G:
sarahbest 2:74690f762c0f 148 aRes = 8.0/32768.0;
sarahbest 2:74690f762c0f 149 break;
sarahbest 2:74690f762c0f 150 case AFS_16G:
sarahbest 2:74690f762c0f 151 aRes = 16.0/32768.0;
sarahbest 2:74690f762c0f 152 break;
sarahbest 2:74690f762c0f 153 }
sarahbest 2:74690f762c0f 154 ICM20602_WriteByte(ICM20602_ACCEL_CONFIG, Ascale<<3);// bit[4:3] 0=+-2g,1=+-4g,2=+-8g,3=+-16g, ACC_HPF=On (5Hz)
sarahbest 2:74690f762c0f 155 return aRes;
sarahbest 2:74690f762c0f 156 }
sarahbest 2:74690f762c0f 157
sarahbest 2:74690f762c0f 158 // Calculates Gyro resolution
sarahbest 2:74690f762c0f 159 float ICM20602::setGyroRange(int Gscale)
sarahbest 2:74690f762c0f 160 {
sarahbest 2:74690f762c0f 161 switch(Gscale)
sarahbest 2:74690f762c0f 162 {
sarahbest 2:74690f762c0f 163 case GFS_250DPS:
sarahbest 2:74690f762c0f 164 gRes = 250.0/32768.0;
sarahbest 2:74690f762c0f 165 break;
sarahbest 2:74690f762c0f 166 case GFS_500DPS:
sarahbest 2:74690f762c0f 167 gRes = 500.0/32768.0;
sarahbest 2:74690f762c0f 168 break;
sarahbest 2:74690f762c0f 169 case GFS_1000DPS:
sarahbest 2:74690f762c0f 170 gRes = 1000.0/32768.0;
sarahbest 2:74690f762c0f 171 break;
sarahbest 2:74690f762c0f 172 case GFS_2000DPS:
sarahbest 2:74690f762c0f 173 gRes = 2000.0/32768.0;
sarahbest 2:74690f762c0f 174 break;
sarahbest 2:74690f762c0f 175 }
sarahbest 2:74690f762c0f 176 ICM20602_WriteByte(ICM20602_GYRO_CONFIG, Gscale<<3); // bit[4:3] 0=+-250d/s,1=+-500d/s,2=+-1000d/s,3=+-2000d/s
sarahbest 2:74690f762c0f 177 return gRes;
sarahbest 2:74690f762c0f 178 }