Hardware Reference
In-Depth Information
I2C communications happen on analog pin 5 for the shared clock (SCL) and analog pin 4 for data (SDL). TWAR,
TWCR, TWDR, and TWSR are the four registers that are used to set up I2C slave mode. TWBR is a fifth register in the
I2C hardware and is unimportant for slave applications. TWBR is used in master mode to control the SCL speed.
SREG is the one register outside the I2C module that will have to be modified for this section. Registers work the
same way as variables in that all the manipulation methods for variables work the same way. The register names have
already been defined by the main libraries used by the Arduino IDE; declarations to use them are not necessary. All
the registers used in this section are 1 byte in size. Some of the registers are for data and others are for control.
The TWCR Register
The TWCR register is the two-wire control register; this is what defines the main working of the I2C communications.
Each bit in the byte of the TWCR register controls a different function within the hardware; the name of the bit
describes its location within the byte.
To put the Arduino into slave mode, you must set the TWI Enable Acknowledge (TWEA) and
TWI Enable (TWEN) bits to 1 in the TWCR. TWEN (bit 2) activates the I2C hardware, and
TWEA (bit 6) tells the hardware to send acknowledgments when appropriate; if the TWEA is
not set, this device will not respond to other devices trying to communicate.
TWI Interrupt (TWINT) (bit 7) and TWI Interrupt Enable (TWIE) (bit 0) are the other two bits
that are important in the TWCR and are used for software control. TWINT is a flag that gets set
to 1 when there is something that needs attention from the software; the software then has to
clear the flag by writing 1 to the TWINT bit when it's finished handling what needed attention.
You can also set up TWINT in conjunction with TWIE as an internal interrupt.
Data being transferred on the I2C is time sensitive, so it is wise to set the communications to be handled using the
internal interrupts on the Arduino. This is accomplished by setting the TWIE and the global interrupt enable in the SREG
to on. SREG needs to be set with a bitwise OR (|) mask so that the other bits are not manipulated, and has to be reset every
time an interrupt happens. When the TWINT flag gets set to 1 by the hardware, the interrupt is triggered. The interrupt
service routine ( ISR(vector) ) is run when an interrupt is triggered; the ISR() works very similarly to a normal function
such as Setup() or Loop() . ISR() can be written directly in the Arduino sketch with no preceding information, but a
vector is required. A vector is a name that describes the interrupt that the ISR responds to for code execution.
a reference of the vector names used in the aVr libraries that the arduino is built upon is located at
www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html . the vector name that is needed
for i2C interrupt on the arduinos with the 328p chips is TWI_vect .
Note
The TWAR Register
The last register that has to be set to get the I2C slave to respond to information moving on the bus is an address. The
address is set in the TWI Address Register (TWAR). The top seven bits (7-1) are the address; bit 0 tells the device that
it is OK to respond to the general call address. The general call address is 0, and when the master sends this address,
every device set to have a response will respond. When the address is set to the TWAR register, it has to shift to the left
by 1, making 126 unique devices that can be on the I2C bus.
The TWDR Register
The TWI Data Register (TWDR) is where all the data bytes will go through. When this register is written ( TWDR = FOO; ),
a data transfer will begin. To read the incoming data, read the data register into a variable ( FOO = TWDR ). I2C uses
 
 
Search WWH ::




Custom Search