Little help using i2c API

Joaohcca
Posts: 3
Joined: Tue Dec 13, 2016 9:24 pm

Little help using i2c API

Postby Joaohcca » Tue Jan 24, 2017 1:10 am

Hi all, i've some problems to implement correctly a comunication using i2c protocol between my nodemcu v3(ESP12E) and my mpu-6050(gy-521) (accelerometer+gyro+temp sensor).
I've set the SDA on GPIO5 and SCL on GIPO0 on i2c_master.h (Board pins D1 and D3).
From what I understood studying the protocol, in order to write to a register I should :
1- put the 8-bit address for the slave (in my case my mpu-6050) .
2 - put the 7-bit address fot the slave register and set bit 8 high for write operation.
3 - put the 8-bit data to the i2c bus to write on the slave.
4- Send the stop signal.
In between steps I should check the ack from slave in order to proceed.
In the code bellow I'm trying to write data

Code: Select all

void mpu_sleep(void)
{
  uint8 add=104; //0x68 MPU-6050 address
  uint8 reg_add=107; //register I'm trying to write
  uint8 data=8; //data I'm trying to write
  uint8 ack;         //ack signal from slave
        i2c_master_gpio_init();
      i2c_master_start();
        i2c_master_writeByte(add);
      ack = i2c_master_getAck();
   if(ack){
        os_printf("located mpu6050 \n \r");
        i2c_master_writeByte(reg_add+128);//address and 8-bit set high for write operation
        ack=i2c_master_getAck();
        if(ack) {
        os_printf("pointing to register reg_add and bit 8 set to high (+128) \n \r");
        i2c_master_writeByte(data);  //data
        ack=i2c_master_getAck();
        if(ack){
        os_printf("sucessfully wrote the data to reg_addr \n \r");
        i2c_master_stop();
        os_printf("acc disable sucessfully \n \r");
        }
        //debug messages
        else{
         i2c_master_stop();
        os_printf("failed to write data to address \n \r");}
        }
        else {
        i2c_master_stop();
        os_printf("failed to point to address \n \r");}
   }
   else{
    i2c_master_stop();
    os_printf("failed to locate the device \n \r");
   }
}

what could I be missing?
Did I get something wrong from studying the protocol?
I can build my application but it won't execute as I expected using nonos SDK v2.0.0
Thank you for your time to read the post, any suggestions are welcome.
PS: I've test the sensor module with my arduino using one example from the arduino library and it work as expected, which indicates is not a faulty module

pratik

Re: Little help using i2c API

Postby pratik » Mon Feb 06, 2017 11:16 am

i2c_master_start(); is the line at fault. Your program is correct, but the I2C library implements dummy clock cycles when you start the master using this function, and what happens is some devices get confused because of dummy clocks. This should not happen, but does seem to happen with a lot of devices.
You can get into the I2C driver code and check it out whether it suits your accelerometer or not. The I2C driver code is very, very simple. Check it out once, and modify as needed. :)
You can find the C file in driver directory of SDK.

Who is online

Users browsing this forum: No registered users and 60 guests