コード例 #1
0
void readRegister( I2C& i2c, int registerAdress, char buff[], size_t nbBytes )
{
    if( nbBytes == 0 ) return;
    
    i2c.start();        
    int ack = i2c.write( MMA8452_SLAVE_ADRESS );
    if( ack == 0 ) return;
    
    ack = i2c.write( registerAdress );
    if( ack == 0 ) return;
    
    i2c.start();
    ack = i2c.write( MMA8452_SLAVE_ADRESS | 1 );
    if( ack == 0 ) return;
        
    for( int i = 0; i < nbBytes; ++i )
    {
        buff[ i ] = i2c.read( i == nbBytes - 1 ? 0 : 1 );   
    }
  
    i2c.stop();
}
コード例 #2
0
void writeRegister( I2C& i2c, char registerAdress, char *data, int nbBytes ) 
{
    if( nbBytes == 0 ) return;
    
    i2c.start();

    int ack = i2c.write( MMA8452_SLAVE_ADRESS );
    if( ack == 0 ) return;
    
    ack = i2c.write( registerAdress );
    if( ack == 0 ) return;

    for( int i = 0; i < nbBytes; i++ ) 
    {
       if( i2c.write(data[i]) != 1 ) 
       {
          return;
       }
    }
    
    i2c.stop();
}