├── README.md └── main.c /README.md: -------------------------------------------------------------------------------- 1 | # MPU6050-TM4C123G-TIVA-C-Launchpad 2 | For more information, check this link: 3 | https://microcontrollerslab.com/mpu6050-gyroscope-accelerometer-sensor-interfacing-with-tm4c123g-tiva-c-launchpad/ 4 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include "TM4C123GH6PM.h" 2 | #include 3 | #include 4 | #include 5 | #define XG_OFFS_TC 0x00 6 | #define YG_OFFS_TC 0x01 7 | #define ZG_OFFS_TC 0x02 8 | #define X_FINE_GAIN 0x03 9 | #define Y_FINE_GAIN 0x04 10 | #define Z_FINE_GAIN 0x05 11 | #define XA_OFFS_H 0x06 12 | #define XA_OFFS_L_TC 0x07 13 | #define YA_OFFS_H 0x08 14 | #define YA_OFFS_L_TC 0x09 15 | #define ZA_OFFS_H 0x0A 16 | #define ZA_OFFS_L_TC 0x0B 17 | #define XG_OFFS_USRH 0x13 18 | #define XG_OFFS_USRL 0x14 19 | #define YG_OFFS_USRH 0x15 20 | #define YG_OFFS_USRL 0x16 21 | #define ZG_OFFS_USRH 0x17 22 | #define ZG_OFFS_USRL 0x18 23 | #define SMPLRT_DIV 0x19 24 | #define CONFIG 0x1A 25 | #define GYRO_CONFIG 0x1B 26 | #define ACCEL_CONFIG 0x1C 27 | #define FF_THR 0x1D 28 | #define FF_DUR 0x1E 29 | #define MOT_THR 0x1F 30 | #define MOT_DUR 0x20 31 | #define ZRMOT_THR 0x21 32 | #define ZRMOT_DUR 0x22 33 | #define FIFO_EN 0x23 34 | #define I2C_MST_CTRL 0x24 35 | #define I2C_SLV0_ADDR 0x25 36 | #define I2C_SLV0_REG 0x26 37 | #define I2C_SLV0_CTRL 0x27 38 | #define I2C_SLV1_ADDR 0x28 39 | #define I2C_SLV1_REG 0x29 40 | #define I2C_SLV1_CTRL 0x2A 41 | #define I2C_SLV2_ADDR 0x2B 42 | #define I2C_SLV2_REG 0x2C 43 | #define I2C_SLV2_CTRL 0x2D 44 | #define I2C_SLV3_ADDR 0x2E 45 | #define I2C_SLV3_REG 0x2F 46 | #define I2C_SLV3_CTRL 0x30 47 | #define I2C_SLV4_ADDR 0x31 48 | #define I2C_SLV4_REG 0x32 49 | #define I2C_SLV4_DO 0x33 50 | #define I2C_SLV4_CTRL 0x34 51 | #define I2C_SLV4_DI 0x35 52 | #define I2C_MST_STATUS 0x36 53 | #define INT_PIN_CFG 0x37 54 | #define INT_ENABLE 0x38 55 | #define DMP_INT_STATUS 0x39 56 | #define INT_STATUS 0x3A 57 | #define ACCEL_XOUT_H 0x3B 58 | #define ACCEL_XOUT_L 0x3C 59 | #define ACCEL_YOUT_H 0x3D 60 | #define ACCEL_YOUT_L 0x3E 61 | #define ACCEL_ZOUT_H 0x3F 62 | #define ACCEL_ZOUT_L 0x40 63 | #define TEMP_OUT_H 0x41 64 | #define TEMP_OUT_L 0x42 65 | #define GYRO_XOUT_H 0x43 66 | #define GYRO_XOUT_L 0x44 67 | #define GYRO_YOUT_H 0x45 68 | #define GYRO_YOUT_L 0x46 69 | #define GYRO_ZOUT_H 0x47 70 | #define GYRO_ZOUT_L 0x48 71 | #define EXT_SENS_DATA_00 0x49 72 | #define EXT_SENS_DATA_01 0x4A 73 | #define EXT_SENS_DATA_02 0x4B 74 | #define EXT_SENS_DATA_03 0x4C 75 | #define EXT_SENS_DATA_04 0x4D 76 | #define EXT_SENS_DATA_05 0x4E 77 | #define EXT_SENS_DATA_06 0x4F 78 | #define EXT_SENS_DATA_07 0x50 79 | #define EXT_SENS_DATA_08 0x51 80 | #define EXT_SENS_DATA_09 0x52 81 | #define EXT_SENS_DATA_10 0x53 82 | #define EXT_SENS_DATA_11 0x54 83 | #define EXT_SENS_DATA_12 0x55 84 | #define EXT_SENS_DATA_13 0x56 85 | #define EXT_SENS_DATA_14 0x57 86 | #define EXT_SENS_DATA_15 0x58 87 | #define EXT_SENS_DATA_16 0x59 88 | #define EXT_SENS_DATA_17 0x5A 89 | #define EXT_SENS_DATA_18 0x5B 90 | #define EXT_SENS_DATA_19 0x5C 91 | #define EXT_SENS_DATA_20 0x5D 92 | #define EXT_SENS_DATA_21 0x5E 93 | #define EXT_SENS_DATA_22 0x5F 94 | #define EXT_SENS_DATA_23 0x60 95 | #define MOT_DETECT_STATUS 0x61 96 | #define I2C_SLV0_DO 0x63 97 | #define I2C_SLV1_DO 0x64 98 | #define I2C_SLV2_DO 0x65 99 | #define I2C_SLV3_DO 0x66 100 | #define I2C_MST_DELAY_CTRL 0x67 101 | #define SIGNAL_PATH_RESET 0x68 102 | #define MOT_DETECT_CTRL 0x69 103 | #define USER_CTRL 0x6A 104 | #define PWR_MGMT_1 0x6B 105 | #define PWR_MGMT_2 0x6C 106 | #define BANK_SEL 0x6D 107 | #define MEM_START_ADDR 0x6E 108 | #define MEM_R_W 0x6F 109 | #define DMP_CFG_1 0x70 110 | #define DMP_CFG_2 0x71 111 | #define FIFO_COUNTH 0x72 112 | #define FIFO_COUNTL 0x73 113 | #define FIFO_R_W 0x74 114 | #define WHO_AM_I 0x75 115 | void I2C3_Init(void); 116 | char I2C3_Wr(int slaveAddr, char memAddr, char data); 117 | char I2C3_Rd(int slaveAddr, char memAddr, int byteCount, char* data); 118 | void Delay(unsigned long counter); 119 | void uart5_init(void); 120 | void UART5_Transmitter(unsigned char data); 121 | void printstring(char *str); 122 | void MPU6050_Init(void); 123 | 124 | char msg[20]; 125 | int main(void) 126 | { 127 | int accX, accY, accZ, GyroX, GyroY, GyroZ, Temper; 128 | float AX, AY, AZ, t, GX, GY, GZ; 129 | char sensordata[14]; 130 | I2C3_Init(); 131 | Delay(1000); 132 | MPU6050_Init(); 133 | Delay(1000); 134 | uart5_init(); 135 | while(1) 136 | { 137 | I2C3_Rd(0x68,ACCEL_XOUT_H, 14, sensordata); 138 | accX = (int) ( (sensordata[0] << 8 ) |sensordata[1] ); 139 | accY = (int) ( (sensordata[2] << 8 ) |sensordata[3] ); 140 | accZ = (int) ( (sensordata[4] << 8 ) |sensordata[5] ); 141 | Temper = (int) ( (sensordata[6] << 8 ) |sensordata[7] ); 142 | GyroX = (int) ( (sensordata[8] << 8 ) |sensordata[9] ); 143 | GyroY = (int) ( (sensordata[10] << 8 ) |sensordata[11] ); 144 | GyroZ = (int) ( (sensordata[12] << 8 ) |sensordata[13] ); 145 | 146 | 147 | // Convert The Readings 148 | AX = (float)accX/16384.0; 149 | AY = (float)accY/16384.0; 150 | AZ = (float)accZ/16384.0; 151 | GX = (float)GyroX/131.0; 152 | GY = (float)GyroX/131.0; 153 | GZ = (float)GyroX/131.0; 154 | t = ((float)Temper/340.00)+36.53; 155 | sprintf(msg,"Gx = %.2f \t",GX); 156 | printstring(msg); 157 | sprintf(msg,"Gy = %.2f \t",GY); 158 | printstring(msg); 159 | sprintf(msg,"Gz = %.2f \t",GZ); 160 | printstring(msg); 161 | sprintf(msg,"Ax = %.2f \t",AX); 162 | printstring(msg); 163 | sprintf(msg,"Ay = %.2f \t",AY); 164 | printstring(msg); 165 | sprintf(msg,"Ax = %.2f \r\n",AZ); 166 | printstring(msg); 167 | // sprintf(msg,"Temp = %.2f \r\n",t); 168 | // printstring(msg); 169 | Delay(1000); 170 | } 171 | } 172 | 173 | void MPU6050_Init(void) 174 | { 175 | I2C3_Wr(0x68,SMPLRT_DIV, 0x07); 176 | I2C3_Wr(0x68,PWR_MGMT_1, 0x01); 177 | I2C3_Wr(0x68,CONFIG, 0x00); 178 | I2C3_Wr(0x68,ACCEL_CONFIG,0x00); 179 | I2C3_Wr(0x68,GYRO_CONFIG,0x18); 180 | I2C3_Wr(0x68,INT_ENABLE, 0x01); 181 | 182 | } 183 | 184 | void uart5_init(void) 185 | { 186 | 187 | SYSCTL->RCGCUART |= 0x20; /* enable clock to UART5 */ 188 | SYSCTL->RCGCGPIO |= 0x10; /* enable clock to PORTE for PE4/Rx and RE5/Tx */ 189 | Delay(1); 190 | /* UART0 initialization */ 191 | UART5->CTL = 0; /* UART5 module disbable */ 192 | UART5->IBRD = 104; /* for 9600 baud rate, integer = 104 */ 193 | UART5->FBRD = 11; /* for 9600 baud rate, fractional = 11*/ 194 | UART5->CC = 0; /*select system clock*/ 195 | UART5->LCRH = 0x60; /* data lenght 8-bit, not parity bit, no FIFO */ 196 | UART5->CTL = 0x301; /* Enable UART5 module, Rx and Tx */ 197 | 198 | /* UART5 TX5 and RX5 use PE4 and PE5. Configure them digital and enable alternate function */ 199 | GPIOE->DEN = 0x30; /* set PE4 and PE5 as digital */ 200 | GPIOE->AFSEL = 0x30; /* Use PE4,PE5 alternate function */ 201 | GPIOE->AMSEL = 0; /* Turn off analg function*/ 202 | GPIOE->PCTL = 0x00110000; /* configure PE4 and PE5 for UART */ 203 | 204 | } 205 | void I2C3_Init(void) 206 | { 207 | SYSCTL->RCGCGPIO |= 0x00000008 ; // Enable the clock for port D 208 | SYSCTL->RCGCI2C |= 0x00000008 ; // Enable the clock for I2C 3 209 | GPIOD->DEN |= 0x03; // Assert DEN for port D 210 | // Configure Port D pins 0 and 1 as I2C 3 211 | GPIOD->AFSEL |= 0x00000003 ; 212 | GPIOD->PCTL |= 0x00000033 ; 213 | GPIOD->ODR |= 0x00000002 ; // SDA (PD1 ) pin as open darin 214 | I2C3->MCR = 0x0010 ; // Enable I2C 3 master function 215 | /* Configure I2C 3 clock frequency 216 | (1 + TIME_PERIOD ) = SYS_CLK /(2* 217 | ( SCL_LP + SCL_HP ) * I2C_CLK_Freq ) 218 | TIME_PERIOD = 16 ,000 ,000/(2(6+4) *100000) - 1 = 7 */ 219 | I2C3->MTPR = 0x07 ; 220 | } 221 | 222 | /* Wait until I2C master is not busy and return error code */ 223 | /* If there is no error, return 0 */ 224 | static int I2C_wait_till_done(void) 225 | { 226 | while(I2C3->MCS & 1); /* wait until I2C master is not busy */ 227 | return I2C3->MCS & 0xE; /* return I2C error code */ 228 | } 229 | 230 | /* Write one byte only */ 231 | /* byte write: S-(saddr+w)-ACK-maddr-ACK-data-ACK-P */ 232 | char I2C3_Wr(int slaveAddr, char memAddr, char data) 233 | { 234 | 235 | char error; 236 | 237 | /* send slave address and starting address */ 238 | I2C3->MSA = slaveAddr << 1; 239 | I2C3->MDR = memAddr; 240 | I2C3->MCS = 3; /* S-(saddr+w)-ACK-maddr-ACK */ 241 | 242 | error = I2C_wait_till_done(); /* wait until write is complete */ 243 | if (error) return error; 244 | 245 | /* send data */ 246 | I2C3->MDR = data; 247 | I2C3->MCS = 5; /* -data-ACK-P */ 248 | error = I2C_wait_till_done(); /* wait until write is complete */ 249 | while(I2C3->MCS & 0x40); /* wait until bus is not busy */ 250 | error = I2C3->MCS & 0xE; 251 | if (error) return error; 252 | 253 | return 0; /* no error */ 254 | } 255 | char I2C3_Rd(int slaveAddr, char memAddr, int byteCount, char* data) 256 | { 257 | char error; 258 | 259 | if (byteCount <= 0) 260 | return -1; /* no read was performed */ 261 | 262 | /* send slave address and starting address */ 263 | I2C3->MSA = slaveAddr << 1; 264 | I2C3->MDR = memAddr; 265 | I2C3->MCS = 3; /* S-(saddr+w)-ACK-maddr-ACK */ 266 | error = I2C_wait_till_done(); 267 | if (error) 268 | return error; 269 | 270 | /* to change bus from write to read, send restart with slave addr */ 271 | I2C3->MSA = (slaveAddr << 1) + 1; /* restart: -R-(saddr+r)-ACK */ 272 | 273 | if (byteCount == 1) /* if last byte, don't ack */ 274 | I2C3->MCS = 7; /* -data-NACK-P */ 275 | else /* else ack */ 276 | I2C3->MCS = 0xB; /* -data-ACK- */ 277 | error = I2C_wait_till_done(); 278 | if (error) return error; 279 | 280 | *data++ = I2C3->MDR; /* store the data received */ 281 | 282 | if (--byteCount == 0) /* if single byte read, done */ 283 | { 284 | while(I2C3->MCS & 0x40); /* wait until bus is not busy */ 285 | return 0; /* no error */ 286 | } 287 | 288 | /* read the rest of the bytes */ 289 | while (byteCount > 1) 290 | { 291 | I2C3->MCS = 9; /* -data-ACK- */ 292 | error = I2C_wait_till_done(); 293 | if (error) return error; 294 | byteCount--; 295 | *data++ = I2C3->MDR; /* store data received */ 296 | } 297 | 298 | I2C3->MCS = 5; /* -data-NACK-P */ 299 | error = I2C_wait_till_done(); 300 | *data = I2C3->MDR; /* store data received */ 301 | while(I2C3->MCS & 0x40); /* wait until bus is not busy */ 302 | 303 | return 0; /* no error */ 304 | } 305 | 306 | void UART5_Transmitter(unsigned char data) 307 | { 308 | while((UART5->FR & (1<<5)) != 0); /* wait until Tx buffer not full */ 309 | UART5->DR = data; /* before giving it another byte */ 310 | } 311 | 312 | void printstring(char *str) 313 | { 314 | while(*str) 315 | { 316 | UART5_Transmitter(*(str++)); 317 | } 318 | } 319 | void Delay(unsigned long counter) 320 | { 321 | unsigned long i = 0; 322 | 323 | for(i=0; i< counter*10000; i++); 324 | } 325 | 326 | --------------------------------------------------------------------------------