RP6 ROBOT SYSTEM - 4. Programming the RP6
4.6.11. I²C Bus Functions
At the end of this chapter we will focus on the I²C-Bus functions, which can be used
for communication with other Microcontrollers and expansion modules.
There are two versions of I²C-Bus functions – one for slave and another one for mas-
ter mode.
Attention: You can not use both versions simultaneously!
You may include one of both versions only and you need to make sure that they are
listed in the makefile. Apropriate entries have been added to the Makefiles of the ex-
ample programs already –
however they have been outcommented in most of the ex-
amples
. Once again: use only one of these entries! Otherwise the compiler will issue
an error message (this is because the TWI Interrupt Vector would be defined twice if
you would include both versions).
4.6.11.1. I²C Slave
On the robot base unit, the slave mode is most important, because it is a very com-
mon task to add another Microcontroller to the Robot in order to control it. There is an
Example program, which allows you to access nearly all functions of the robot base
unit through the I²C Bus (RP6Base_I2CSlave).
Basically both, master mode and slave mode use interrupts. It is not easy to imple-
ment I2C Slave mode in pure Software (at least not with reasonable effort). The mas-
ter mode could be easily implemented in Software, but in order to keep the structure
of both similar, we also used interrupts this mode. Another advantage is, that the
master mode transmissions can be performed in background to save some time.
void I2CTWI_initSlave(uint8_t address)
This function will initialize the Microcontroller's TWI module as I²C Slave and allows
you to define the address as parameter. In the address you can simultaneously define,
whether the controller should react on so-called “General Calls” or not. When the bus
is addressed with a 0, this is called “General Call”. For example you may use this func-
tionality to easily switch all controllers on the bus to a power saving mode simultan-
eously.
Examples:
I2CTWI_initSlave( adr | TWI_GENERAL_CALL_ENABLE ); // Enable general call
I2CTWI_initSlave(adr); // Disable general call
I²C Registers
Usual I²C peripherals can be controlled through a few readable/writeable registers.
Therefore the slave routines are designed to provide a number of “Registers” (in this
application an array of 8 Bit variables), which may be written or read by the master
device. In order to read data from a register or to write data into a register, the mas-
ter device has to send the slave address and subsequently the register address.
There are two uint8_t arrays. One for readable and one for writeable Registers.
- 104 -