Difference between revisions of "Basics of I2C with AVR"
Line 42: | Line 42: | ||
'''• Bit 6 – TWEA: TWI Enable Acknowledge Bit''' | '''• Bit 6 – TWEA: TWI Enable Acknowledge Bit''' | ||
− | It controls the generation of the acknowledge pulse. If the TWEA bit is written | + | |
− | + | It controls the generation of the acknowledge pulse. | |
+ | |||
+ | If the TWEA bit is written toone, the ACK pulse is generated on the TWI bus if the following conditions are met: | ||
+ | |||
1. The device’s own slave address has been received. | 1. The device’s own slave address has been received. | ||
+ | |||
2. A general call has been received, while the TWGCE bit in the TWAR is set. | 2. A general call has been received, while the TWGCE bit in the TWAR is set. | ||
+ | |||
3. A data byte has been received in Master Receiver or Slave Receiver mode. | 3. A data byte has been received in Master Receiver or Slave Receiver mode. | ||
Revision as of 13:31, 4 February 2015
Contents
Basics
The I²C (Inter-Integrated Circuit) protocol, referred to as I-squared-C, I-two-C, or IIC) is two wire serial communication protocol for connecting low speed peripherals to a micrcontroller or computer motherboard.
You can go through I²C /TWI Basics tutorials to understand how I²C works.
Registers
TWBR ( TWI Bit Rate Register )
TWBR | |||||||
---|---|---|---|---|---|---|---|
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
TWBR7 | TWBR6 | TWBR5 | TWBR4 | TWBR3 | TWBR2 | TWBR1 | TWBR0 |
Bits [7:0] – Bit Rate
It selects the division factor for the bit rate generator. The bit rate generator is basically a frequency divider. It generates the SCL clock frequency in the Master modes.
TWCR ( TWI Control Register )
It is used to control all TWI operations.
TWCR | |||||||
---|---|---|---|---|---|---|---|
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
TWBR7 | TWBR6 | TWBR5 | TWBR4 | TWBR3 | TWBR2 | TWBR1 | TWBR0 |
• Bit 7 – TWINT: TWI Interrupt Flag This bit is set by hardware when the TWI has finished its current job and expects application software response. It is not automatically cleared by hardware when executing the interrupt routine. So, you must clear TWINT Flag by writing a logic one to it.
Also note that clearing this flag starts the operation of the TWI. So before clearing this flag, all accesses to the other TWI registers must be complete .
• Bit 6 – TWEA: TWI Enable Acknowledge Bit
It controls the generation of the acknowledge pulse.
If the TWEA bit is written toone, the ACK pulse is generated on the TWI bus if the following conditions are met:
1. The device’s own slave address has been received.
2. A general call has been received, while the TWGCE bit in the TWAR is set.
3. A data byte has been received in Master Receiver or Slave Receiver mode.
You can virtually disconnect device from the Two-wire Serial Bus temporarily by writting zero to TWEA. And to resume address recognition write one to it.
• Bit 5 – TWSTA: TWI START Condition Bit To make the micro-controller master device on the Two wire Serial Bus, you must set this bit.
• Bit 4 – TWSTO: TWI STOP Condition Bit In Master mode, write the one TWSTO bit to generate a STOP condition. This bit is cleared automatically when the STOP condition is executed on the bus.
In slave mode, setting the TWSTO bit can be used to recover from an error condition.
• Bit 3 – TWWC: TWI Write Collision Flag The TWWC bit is set when attempting to write to the TWI Data Register – TWDR when TWINT is low. This flag is cleared by writing the TWDR Register when TWINT is high.
• Bit 2 – TWEN: TWI Enable Bit To start TWI interface write one to this bit.
• Bit 1 – Reserved Bit This bit is a reserved bit and will always read as zero.
• Bit 0 – TWIE: TWI Interrupt Enable When this bit is written to one, and the I-bit in SREG is set, the TWI interrupt request will be activated for as long as the TWINT Flag is high.