oled 1.0
OLED Graphics Control Library
|
Source file with implementation of software TWI/I2C functions and macros. More...
#include "twi_soft.h"
Functions | |
unsigned char | twi_soft_init (void) |
Initialize the software TWI (I2C) bus. | |
void | twi_soft_disable (void) |
Disable the software TWI (I2C) interface. | |
unsigned char | twi_soft_status (void) |
Get the current status of the software TWI bus. | |
TWI_Error | twi_soft_start (void) |
Generate a TWI start or repeated start condition. | |
void | twi_soft_stop (void) |
Stop the TWI (I2C) transmission. | |
TWI_Error | twi_soft_address (unsigned char address, TWI_Operation operation) |
Sends the slave address with the read/write operation on the TWI bus. | |
TWI_Error | twi_soft_set (unsigned char data) |
Transmit a byte of data on the TWI bus. | |
TWI_Error | twi_soft_get (unsigned char *data, TWI_Acknowledge acknowledge) |
Reads a byte of data from the TWI bus and sends an ACK or NACK. |
Variables | |
unsigned char | twi_soft_bus_status |
Source file with implementation of software TWI/I2C functions and macros.
This file contains the definitions of function implementations and macros for software-based TWI (I2C) communication on AVR microcontrollers.
TWI_Error twi_soft_address | ( | unsigned char | address, |
TWI_Operation | operation ) |
Sends the slave address with the read/write operation on the TWI bus.
address | 7-bit address of the slave device. |
operation | Specifies the operation type: TWI_Write or TWI_Read. |
This function prepares and sends the 8-bit address byte on the TWI bus, combining the 7-bit slave address shifted left by 1 with the least significant bit indicating the operation (read or write). The actual transmission and status handling is performed by twi_soft_set().
void twi_soft_disable | ( | void | ) |
Disable the software TWI (I2C) interface.
This function disables the software TWI module by clearing the SDA and SCL bits in the port and data direction registers. This effectively releases control of the TWI lines and sets the pins to a low output state. Use this function to safely deactivate the TWI bus when it is no longer needed.
TWI_Error twi_soft_get | ( | unsigned char * | data, |
TWI_Acknowledge | acknowledge ) |
Reads a byte of data from the TWI bus and sends an ACK or NACK.
data | Pointer to a variable where the received data byte will be stored. |
acknowledge | Specifies whether to send an ACK (TWI_ACK) or NACK (TWI_NACK) after reception. |
This function reads one data byte from the software TWI bus by manually controlling the clock and data lines. After reception, it sends either an acknowledgment (ACK) or not-acknowledgment (NACK) bit to the transmitter depending on the 'acknowledge' parameter, following the TWI protocol.
unsigned char twi_soft_init | ( | void | ) |
Initialize the software TWI (I2C) bus.
This function initializes the software TWI bus by configuring the SDA and SCL lines to their idle high state and checking if the bus lines are free (not held low). It introduces delays to ensure proper timing according to the configured TWI clock pulse duration. If either the SCL or SDA line is stuck low, the function returns a bus error status. Otherwise, it confirms that the bus has been initialized successfully.
< Bus error occurred
< Bus initialization complete
TWI_Error twi_soft_set | ( | unsigned char | data | ) |
Transmit a byte of data on the TWI bus.
data | The data byte to be transmitted. |
This function sends the given 8-bit data byte bit-by-bit over the software TWI bus, handling the clock and data line states manually. It checks for arbitration loss when transmitting 1 bits and waits for clock stretching by the slave. After transmission, it reads the acknowledgment bit from the slave to determine successful reception.
TWI_Error twi_soft_start | ( | void | ) |
Generate a TWI start or repeated start condition.
This function produces a start condition on the TWI (I2C) bus which is used to initiate communication with a slave device. It sets the data line (SDA) low while the clock line (SCL) is high, creating the required falling edge signaling the start condition.
< Transmission running
|
inline |
Get the current status of the software TWI bus.
This inline function returns the value of the internal status variable twi_soft_bus_status which reflects the current operational state or error conditions of the software TWI interface.
void twi_soft_stop | ( | void | ) |
Stop the TWI (I2C) transmission.
This function generates the TWI stop condition by first pulling both SCL and SDA lines low, then releasing SCL high, followed by releasing SDA high, with appropriate delays to ensure bus timing compliance. After sending the stop condition, the transmit status flag is cleared to indicate the end of the transmission.
< Transmission running
unsigned char twi_soft_bus_status |