Hi,
Im trying to have the MPU 6050 IMU sensor interfaced through I2C communication. My problem is when going through the setup process as outlined in the data sheet, the I2C1_MDR_R register is always zero or always 255.
Here's my code
#include <stdint.h> #include <stdbool.h> #include "inc/hw_memmap.h" #include "inc/hw_ints.h" #include "driverlib/debug.h" #include "driverlib/gpio.h" #include "driverlib/interrupt.h" #include "driverlib/pin_map.h" #include "driverlib/rom.h" #include "driverlib/sysctl.h" #include "driverlib/uart.h" #include "utils/uartstdio.h" #include "sensorlib/hw_mpu9150.h" #include "sensorlib/hw_ak8975.h" #include "sensorlib/i2cm_drv.h" #include "sensorlib/ak8975.h" #include "sensorlib/mpu9150.h" #include "sensorlib/comp_dcm.h" #include "sensorlib/hw_mpu6050.h" #include "inc/tm4c123gh6pm.h" int main(void) { uint32_t acc_x_L=0; while(1){ //16.4.1 Configure the I2C Module to Transmit a Single Byte as a Master //The following example shows how to configure the I2C module to transmit a single byte as a master. //This assumes the system clock is 20 MHz. //1. Enable the I2C clock using the RCGCI2C register in the System Control module (see page 348). SYSCTL_RCGCI2C_R=(1<<1);//selected i2c module 1 (will connect to portA pin 6 and 7 with I2C1_SCL and I2C1_SDA respectively //2. Enable the clock to the appropriate GPIO module via the RCGCGPIO register in the System //Control module (see page 340). To find out which GPIO port to enable, refer to Table //23-5 on page 1351. SYSCTL_RCGCGPIO_R=(1<<0);//Clock to port A (pin 6 and 7 will have I2C1_SCL and I2C1_SDA respectively) //3. In the GPIO module, enable the appropriate pins for their alternate function using the //GPIOAFSEL register (see page 671). To determine which GPIOs to configure, see Table ///23-4 on page 1344. GPIO_PORTA_AFSEL_R=0xC0;//pin 6 and 7 are assigned alternative function. Nanmely connecting to I2C1_SCL and I2C1_SDA respectively GPIO_PORTA_DEN_R=0b11000000;//set pin 6 and 7 to be digitcal pins GPIO_PORTA_PUR_R=0b11000000;//SDA and SCL pulled up //4. Enable the I2CSDA pin for open-drain operation. See page 676. GPIO_PORTA_ODR_R=0b10000000;//set SDA pin to open drain (bit 7 (pin 8) is set to high) //5. Configure the PMCn fields in the GPIOPCTL register to assign the I2C signals to the appropriate //pins. See page 688 and Table 23-5 on page 1351. GPIO_PORTA_PCTL_R=(0x33 <<24); //6. Initialize the I2C Master by writing the I2CMCR register with a value of 0x0000.0010. I2C1_MCR_R=0x10; //7. Set the desired SCL clock speed of 100 Kbps by writing the I2CMTPR register with the correct //value. The value written to the I2CMTPR register represents the number of system clock periods //in one SCL clock period. The TPR value is determined by the following equation: //TPR = (System Clock/(2*(SCL_LP + SCL_HP)*SCL_CLK))-1; //TPR = (16MHz/(2*(6+4)*100000))-1; //TPR = 7 //Write the I2CMTPR register with the value of 0x0000.0007. I2C1_MTPR_R=0x9; //8. Specify the slave address of the master and that the next operation is a Transmit by writing the //I2CMSA register with a value of 0x0000.0076. This sets the slave address to 0x3B. I2C1_MSA_R=(0x68 <<1 & 0xFE);// send //9. Place data (byte) to be transmitted or recived in the data register by writing the I2CMDR register with the //desired data. I2C1_MDR_R= 0x3C;//write the register to read from //10. Initiate a single byte transmit of the data from Master to Slave by writing the I2CMCS register //with a value of 0x0000.0007 (STOP, START, RUN). I2C1_MCS_R=0b00000011; //11. Wait until the transmission completes by polling the I2CMCS register's BUSBSY bit until it has //been cleared. while(I2C1_MCS_R & 0b00000001 !=0); I2C1_MCS_R=0b00001011; // Wait while i2c module is not busy //while(!(I2C1_MCS_R & 0b00000001 !=0)) I2C1_MSA_R=(0x68<<1 | 0x01);// recieve //12. Check the ERROR bit in the I2CMCS register to confirm the transmit was acknowledged. while (I2C1_MCS_R & 0x02 != 0); } return 0; }
Mind you I am forced to code everything in registers so I cant code anything using the pre defined libraries.
Your help will be appreciated,