I2C/TWI library for AVR-GCC.
More...
|
| void | twi_init (void) |
| | Initialize TWI unit, enable internal pull-ups, and set SCL frequency.
|
| void | twi_start (void) |
| | Start communication on I2C/TWI bus.
|
| uint8_t | twi_write (uint8_t data) |
| | Write one byte to the I2C/TWI bus.
|
| uint8_t | twi_read (uint8_t ack) |
| | Read one byte from the I2C/TWI bus and acknowledge it by ACK or NACK.
|
| void | twi_stop (void) |
| | Generates Stop condition on I2C/TWI bus.
|
| uint8_t | twi_test_address (uint8_t addr) |
| | Test presence of one I2C device on the bus.
|
| void | twi_readfrom_mem_into (uint8_t addr, uint8_t memaddr, volatile uint8_t *buf, uint8_t nbytes) |
| | Read into buf from the peripheral, starting from the memory address.
|
|
#define | TWI_WRITE 0 |
| | Mode for writing to I2C/TWI device.
|
|
#define | TWI_READ 1 |
| | Mode for reading from I2C/TWI device.
|
|
#define | TWI_ACK 0 |
| | ACK value for writing to I2C/TWI bus.
|
|
#define | TWI_NACK 1 |
| | NACK value for writing to I2C/TWI bus.
|
| #define | DDR(_x) |
| | Address of Data Direction Register of port _x.
|
| #define | PIN(_x) |
| | Address of input register of port _x.
|
|
|
#define | F_CPU 16000000 |
| | CPU frequency in Hz required TWI_BIT_RATE_REG.
|
|
#define | F_SCL 100000 |
| | I2C/TWI bit rate. Must be greater than 31000.
|
|
#define | TWI_BIT_RATE_REG ((F_CPU/F_SCL - 16) / 2) |
| | TWI bit rate register value.
|
|
|
#define | TWI_PORT PORTC |
| | Port of TWI unit.
|
|
#define | TWI_SDA_PIN 4 |
| | SDA pin of TWI unit.
|
|
#define | TWI_SCL_PIN 5 |
| | SCL pin of TWI unit.
|
I2C/TWI library for AVR-GCC.
This library defines functions for the TWI (I2C) communication between AVR and Slave device(s). Functions use internal TWI module of AVR.
- Note
- Only Master transmitting and Master receiving modes are implemented. Based on Microchip Atmel ATmega16 and ATmega328P manuals.
- Copyright
- (c) 2018-2025 Tomas Fryza, MIT license
◆ DDR
Value:
Address of Data Direction Register of port _x.
◆ PIN
Value:
Address of input register of port _x.
◆ twi_init()
Initialize TWI unit, enable internal pull-ups, and set SCL frequency.
- Implementation notes:
- AVR internal pull-up resistors at pins TWI_SDA_PIN and TWI_SCL_PIN are enabled
- TWI bit rate register value is calculated as follows fscl = fcpu/(16 + 2*TWBR)
- Returns
- none
◆ twi_read()
| uint8_t twi_read |
( |
uint8_t | ack | ) |
|
Read one byte from the I2C/TWI bus and acknowledge it by ACK or NACK.
- Parameters
-
| ack | - ACK/NACK value to be transmitted |
- Returns
- Received data byte
◆ twi_readfrom_mem_into()
| void twi_readfrom_mem_into |
( |
uint8_t | addr, |
|
|
uint8_t | memaddr, |
|
|
volatile uint8_t * | buf, |
|
|
uint8_t | nbytes ) |
Read into buf from the peripheral, starting from the memory address.
- Parameters
-
| addr | Slave address |
| memaddr | Starting address |
| buf | Buffer to be read into |
| nbytes | Number of bytes |
- Returns
- none
◆ twi_start()
Start communication on I2C/TWI bus.
- Returns
- none
◆ twi_stop()
Generates Stop condition on I2C/TWI bus.
- Returns
- none
◆ twi_test_address()
| uint8_t twi_test_address |
( |
uint8_t | addr | ) |
|
Test presence of one I2C device on the bus.
- Parameters
-
- Returns
- ACK/NACK received value
- Return values
-
| 0 | - ACK has been received |
| 1 | - NACK has been received |
◆ twi_write()
| uint8_t twi_write |
( |
uint8_t | data | ) |
|
Write one byte to the I2C/TWI bus.
- Parameters
-
| data | Byte to be transmitted |
- Returns
- ACK/NACK received value
- Return values
-
| 0 | - ACK has been received |
| 1 | - NACK has been received |
- Note
- Function returns 0 if 0x18, 0x28, or 0x40 status code is detected
- 0x18: SLA+W has been transmitted and ACK has been received
- 0x28: Data byte has been transmitted and ACK has been received
- 0x40: SLA+R has been transmitted and ACK has been received