oled 1.0
OLED Graphics Control Library
|
Source file with implementation of hardware TWI/I2C functions and macros. More...
#include "twi.h"
Functions | |
unsigned char | twi_init (void) |
Initialize the TWI (I2C) hardware interface in master mode. | |
void | twi_disable (void) |
Disable the TWI (I2C) interface. | |
unsigned char | twi_status (void) |
Retrieves the current TWI status byte. | |
TWI_Error | twi_start (void) |
Initiates a TWI (I2C) (repeated) start condition on the bus. | |
void | twi_stop (void) |
Stop the TWI (I2C) transmission by generating a stop condition. | |
TWI_Error | twi_address (unsigned char address, TWI_Operation operation) |
Send a TWI (I2C) slave address combined with the read/write operation bit. | |
TWI_Error | twi_set (unsigned char data) |
Transmit a data byte over the TWI (I2C) bus. | |
TWI_Error | twi_get (unsigned char *data, TWI_Acknowledge acknowledge) |
Receive a data byte from the TWI (I2C) bus. |
Source file with implementation of hardware TWI/I2C functions and macros.
This file contains the definitions of function implementations and macros for hardware-based TWI (I2C) communication on AVR microcontrollers.
|
inline |
Send a TWI (I2C) slave address combined with the read/write operation bit.
address | 7-bit slave address to be transmitted. |
operation | Specifies the operation type: 0 for write, 1 for read. |
This inline function constructs the address byte by shifting the 7-bit slave address left by one and combining it with the least significant bit representing the operation (read/write). It then calls twi_set to transmit this address byte over the TWI bus and returns the transmission status.
void twi_disable | ( | void | ) |
Disable the TWI (I2C) interface.
This function disables the hardware TWI module. This effectively releases control of the TWI lines and sets the pins to reset state. Use this function to safely deactivate the TWI bus when it is no longer needed.
TWI_Error twi_get | ( | unsigned char * | data, |
TWI_Acknowledge | acknowledge ) |
Receive a data byte from the TWI (I2C) bus.
data | Pointer to a variable where the received data byte will be stored. |
acknowledge | Specifies whether the master should send an ACK or NACK after receiving the byte:
|
This function configures the TWI hardware to receive a byte from the bus and decides whether to send an ACK or NACK based on the acknowledge parameter.
The function checks the TWI status register (TWSR) for arbitration loss or to verify that the appropriate ACK/NACK status was received. If the received status matches the expected acknowledge mode, it returns success; otherwise, it indicates an acknowledgment error.
unsigned char twi_init | ( | void | ) |
Initialize the TWI (I2C) hardware interface in master mode.
This function configures the TWI hardware registers to initialize the bus in master mode.
In master mode, it sets the bitrate and prescaler according to predefined macros.
The function returns the initial TWI status for further status checking after initialization.
TWI_Error twi_set | ( | unsigned char | data | ) |
Transmit a data byte over the TWI (I2C) bus.
data | The data byte to be transmitted to the bus. |
This function writes the provided data byte into the TWI data register and triggers the transmission by setting the TWINT and TWEN bits in the control register.
After transmission, the function reads the TWI status register (TWSR) to determine the outcome of the transmission and returns the corresponding status code.
TWI_Error twi_start | ( | void | ) |
Initiates a TWI (I2C) (repeated) start condition on the bus.
This function triggers the TWI hardware to send a START condition on the bus, signaling the beginning of a new communication frame. It sets the necessary control bits in the TWCR register to initiate the start and enables the TWI interface.
After completion, the function checks the TWI status register (TWSR) to verify if the start or repeated start condition was acknowledged by the bus. If successful, it returns TWI_None; otherwise, it returns TWI_Start indicating failure to send the (repeated) start condition.
|
inline |
Retrieves the current TWI status byte.
Reads and returns the TWI status byte from the TWSR register, including the write collision flag.
void twi_stop | ( | void | ) |
Stop the TWI (I2C) transmission by generating a stop condition.
This function initiates the TWI stop condition on the bus, signaling the end of the current communication frame. It sets the appropriate bits in the TWCR register to transmit the stop signal and keeps the TWI hardware enabled.