WebbotLib AVR library
WebbotLib It just does it
  C++ documentation  C documentation

writeRegisters

Performs a complete communication session with a device to write to a sequential series of registers.
Returns TRUE if successful or FALSE if there was an error.
Syntax
I2cDevice. writeRegisters(startReg,writeLen,writeData)
Where I2cDevice is the name you have given to the device in Project Designer.
Parameters
TypeNameDescription
'startReg'
The start register number.
'writeLen'
The number of bytes to be written.
'writeData'
The memory address of the data to be written.
Returns
boolean
Note
This is the same as the following pseudo code:
// Connect in write mode
i2cStart( WRITE );
// Write the start register
i2cPut( startreg );
// Write the data values
for(size_t i=0; i<writeLen; i++){
    i2cPut(writeData[writeLen]);
}
// Hang up
i2cStop();
Example
Assuming you have created a 'Generic I2C Device' in Project Designer called myDevice and you want to set the two registers 10 and 11 to the values 1 and 2 then you would use:
// Create an array of the values to be written
uint8_t data[] = { 1, 2 };
if( myDevice.writeRegisters(10, 2, data) ){
    // ok
}else{
    // Communication failed
}
Note how the C operator 'sizeof' can be used to make sure that the number of bytes being read is actually the same as the number of bytes in the 'data' array by changing the 'write' as follows:-
myDevice.writeRegisters(10, sizeof(data), data)
You are encouraged to use this technique as it means that if you decide, later on, to write 4 bytes then you can just change the 'data' definition to:
uint8_t data[] = { 1, 2, 45, 16 };
and nothing else needs to change.

 

 

Valid XHTML 1.0 Transitional