Majid-Derhambakhsh / i2c-unit

Driver for working with I2C(TWI) unit of AVR microcontroller.
1 stars 1 forks source link
arduino-i2c avr-i2c i2c library majid-derhambakhsh

I2C Unit Driver

Driver for working with I2C(TWI) unit of AVR microcontroller.

Version : 0.7.0

Initialization and de-initialization functions:

void I2C_Init(void); /* Function for Initialize the I2C peripheral. */
void I2C_DeInit(void); /* Function for Deinitialize the I2C peripheral. */
void I2C_SetAddress(uint8_t address); /* Function for self I2C address */

Operation functions:

uint8_t I2C_IsDeviceReady(uint8_t dev_address , uint16_t trials , uint16_t time_out); /* Function for check connected device */
StatusTypeDef I2C_Master_Transmit(uint8_t dev_address , uint8_t *data , uint32_t quantity , uint16_t time_out); /* Function for transmit data to i2c device */
StatusTypeDef I2C_Master_Receive(uint8_t dev_address , uint8_t *data , uint32_t quantity , uint16_t time_out); /* Function for receive data from i2c device */
StatusTypeDef I2C_Mem_Write(uint8_t dev_address , uint32_t mem_address , uint8_t mem_add_size , uint8_t *mem_data , uint32_t quantity , uint16_t time_out); /* This function is for write data to external memory */
StatusTypeDef I2C_Mem_Read(uint8_t dev_address , uint32_t mem_address , uint8_t mem_add_size , uint8_t *mem_data , uint32_t quantity , uint16_t time_out ); /* This function is for read data from external memory */
StatusTypeDef I2C_Mem_Erase(uint8_t dev_address , uint32_t mem_address , uint8_t mem_add_size , uint32_t quantity , uint16_t time_out); /* This function is for erase external memory */

Macros:

#define _F_SCL
#define _PRESCALER

How to use this library

The I2C Unit driver can be used as follows:

1. Add .h and source file in project.

2. Config SCL/SDA GPIO as output in your project.

DDRC = (1 << SCL_PIN)|(1 << SDA_PIN);

3. Config SCL clock in 'i2c_unit_conf.h' header, for example:

#define _F_SCL      100000UL 
#define _PRESCALER  _PRE1 

4. Use operation methods, for example:

Example:


int main(void)
{
    DDRC = (1 << SCL_PIN)|(1 << SDA_PIN);

    I2C_Init()'

    while (1) 
    {

      I2C_Master_Transmit(0xA0 , "MyData" , 6 , 100); /* 0xA0 is device address, "MyData" is data for transmit, 6 is data length, 100 is timeout */

    }
}

Other Examples:

Tests performed:

Developer: Majid Derhambakhsh