RP6 ROBOT SYSTEM - 4. Programming the RP6
void I2CTWI_transmitBytes(uint8_t targetAdr, uint8_t *msg,
uint8_t numberOfBytes)
Basically this function will transfer up to 20 Bytes to the specified address. For trans-
ferring greater data blocks, you may increase the I2CTWI_BUFFER_SIZE constant in
the Header file.
In order to specify a register for data transfer you may simply use the first byte of the
buffer.
Using all these functions is rather simple, e.g.:
I2CTWI_transmit2Bytes(10, 2, 128);
I2CTWI_transmit2Bytes(10, 3, 14);
I2CTWI_transmit3Bytes(64, 12, 98, 120);
The preceding example transmits successively two times two bytes to a slave device
with address 10 and additionally three bytes to a slave with address 64.
The other transmitXBytes-functions are used in a similar way.
Another example:
uint8_t messageBuf[4];
messageBuf[0] = 2; // Here you may optionally specify the addressed register.
messageBuf[1] = 244; // Data...
messageBuf[2] = 231;
messageBuf[3] = 123;
messageBuf[4] = 40;
I2CTWI_transmitBytes(10,&messageBuf[0],5);
Like this, you can transmit several Bytes (5 in this case) via the I²C Bus.
The previously described functions will not block the program flow, unless the I²C In-
terface is busy. A busy I²C Interface will cause the functions to wait until all transfers
are completed. Therefore checking for completion before calling the function will allow
you to perform other jobs while data transfer is in progress. Data transfer with the I²C
Bus is relatively time consuming compared to the micro-controller's speed and you
can save some time by checking this.
The following macro indicates whether the TWI module is busy or not:
I2CTWI_isBusy()
If the module is free, you may transfer new data.
Data reception
The RP6Library provides several options for data reception. First we present a few
blocking functions, which have been designed in analogy to the writing functions. Ad-
ditionally we will discuss functions for receiving data in the background.
First of all the simple blocking function for reading data:
uint8_t I2CTWI_readByte(uint8_t targetAdr);
This function reads one byte from a slave device. This functions can not be used
alone, most likely you will have to transfer the register number with
I2CTWI_transmitByte before.
- 108 -