i2c Port Help!!

hi to all...it's possibel to any help me with read 2 bytes of data in the i2c port...in the old librarys i read the data but in the new hal library no work...

i use this in arduplane 2.68 code with librarys ; my sensor address is  0X75...and put in the i2c line 2 byte of data when is request ...

#include <I2C.h>

#define addr 0x75

void setup()
{
   I2c.begin();
 delay(10);
  Serial.begin(115200);
 
}

void loop()

{

 

int  x = 0;

uint8_t buff[2]; 

I2c.read(addr,2,buff);

x = buff[1] << 8;

x |= buff[0];

Serial.println(x,DEC);

}

i tried with news hal  librarys ,,,the code compile ok...but not read the i2c port...i read the i2c driver and the old library have more functions and the new not...its posible to error in the driver inside the AP_halavr???

#include <AP_Common.h>

#include <AP_Math.h>

#include <AP_Param.h>

#include <AP_Progmem.h>

#include <AP_HAL_AVR.h>

#include <AP_HAL.h>

const AP_HAL::HAL& hal = AP_HAL_BOARD_DRIVER;

 

#define addr  0x75

void setup()

{  

hal.i2c->begin();

  hal.scheduler->delay(10);

}

void loop()

{   

int x =0;    

uint8_t buff[2];   

hal.i2c->read(addr, 2, buff);        

x= buff[1] << 8 | buff[0];

 hal.console->printf_P(PSTR("value= %d \r\n"),x,DEC);  

hal.scheduler->delay(10);        

}

AP_HAL_MAIN();

 

 

thank you very much for your attention to all and sorry my bad english...

best regards.

You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –

Replies

  • Hi Luis, our group recently modified the ArduPlane code to read battery information from a BQ34Z100 gas gauge sensor for LiPos, and we had to do the same transition from 2.6x to 2.7x.  Here's a copy/paste of our verified working code in 2.73 (in our case, the code lives in sensors.pde):

     

    // This replaces the read_battery functionality in order to integrate with the BQ34Z00.
    static void query_bq34z100(void)
    {
        hal.i2c->begin();
        uint8_t buff[2];
        // Voltage
        hal.i2c->readRegisters(BQ34Z100, 0x08, 2, buff);
        battery_voltage1 = ((int16_t)buff[1] 8) + buff[0]; // glue together the two bytes we got back in the right order to yield the correct value
        // Current
        hal.i2c->readRegisters(BQ34Z100, 0x0a, 2, buff);
        current_amps1 = (((int16_t)buff[1] 8) + buff[0]) / 10; // glue together the two bytes received from the i2c bus to yield the correct value
        // State of charge remaining, as a %
        hal.i2c->readRegisters(BQ34Z100, 0x02, 2, buff);
        state_of_charge = (uint8_t)((int16_t)buff[1] 8) + buff[0]; // glue together the two bytes we got back in the right order to yield the correct value
    }

     

    Hope this gets you on the right track -- no real tricks here, but it took a bit of fiddling around + looking at other examples in the current revision of the codebase to get it working.  Try searching the codebase for instances of hal.i2c-> to find other existing examples, too.
This reply was deleted.