User manual

RP6 ROBOT SYSTEM - 4. Programming the RP6
4.6.11.2. I²C Master
In Master mode, the ATMEGA32's TWI module can be used to control other
devices/Microcontrollers/sensors through the I²C Bus.
void I2CTWI_initMaster(FREQ)
This function initializes the TWI module as master. Of course the Master mode does
not require an address but we have to specify the data transmission frequency for
the TWI module. You have to define the frequency in kHz by using the parameter
FREQ. A usual value is 100kHz, which can be set by a parameter value of 100. You
may speed up transmission by using values up to 400. Do not exceed the upper limit
400 for the TWI module!
According to Atmel's specifications (see data sheet) the TWI module
of the MEGA32 may only be operated at rates of up to a maximal
220kBit/s in master mode! A transmission frequency of 400kHz
would require a clock frequency over 14.4MHz, but for power saving
reasons, we chose a 8MHz clock frequency. But this is only a small
timing issue and it may still work properly for you. In slave mode,
this does not cause any problems at all and 400kBit/s can be used.
If you really need that fast communication, you can either try to set
the 400kBit/s mode and see if it works properly with your specific
slave devices or use the RP6 CONTROL M32 expansion module which
is clocked at 16MHz. Usually it should also work with 8MHz as this is
only a small timing issue. But we can not guarantee this!
Data transmission
There are a number of functions for transferring data with the I²C Bus. Basically these
functions are all quite similar, but they allow different number of bytes for transmis-
sion.
void I2CTWI_transmitByte(uint8_t adr, uint8_t data)
transfers one byte to the specified address.
void I2CTWI_transmit2Bytes(uint8_t adr, uint8_t data1, uint8_t data2)
transfers two bytes to the specified address. You will need this function frequently as
a number of I²C-devices requests data formats like:
Slave address – Register address – data
void I2CTWI_transmit3Bytes(uint8_t adr, uint8_t data1, uint8_t data2,
uint8_t data3)
is used quite frequently as well, especially the previously described slave program
communicates with data formats like:
Slave Address – command register – command – parameter1
- 107 -