Create I2C/TWI library and enable it
This commit is contained in:
60
i2c.c
Normal file
60
i2c.c
Normal file
@@ -0,0 +1,60 @@
|
||||
#include <avr/io.h>
|
||||
#include "i2c.h"
|
||||
|
||||
void i2c_init(uint32_t bitrate)
|
||||
{
|
||||
uint8_t prescaler_options[] = {1, 4, 16, 64};
|
||||
uint8_t prescaler_value = prescaler_options[TWSR & 0x03]; // 0x03 (hex) = 0b00000011 (bin)
|
||||
TWBR = (uint8_t)((F_CPU / bitrate - 16) / (2 * prescaler_value));
|
||||
}
|
||||
|
||||
void i2c_start(void)
|
||||
{
|
||||
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTA);
|
||||
while(!(TWCR & (1<<TWINT)));
|
||||
}
|
||||
|
||||
void i2c_stop(void)
|
||||
{
|
||||
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
|
||||
while(TWCR & (1<<TWSTO));
|
||||
}
|
||||
|
||||
uint8_t i2c_read(uint8_t ack)
|
||||
{
|
||||
TWCR = (1<<TWINT) | (ack<<TWEA) | (1<<TWEN);
|
||||
while(!(TWCR & (1<<TWINT)));
|
||||
return TWDR;
|
||||
}
|
||||
|
||||
void i2c_write(uint8_t byte)
|
||||
{
|
||||
TWDR = byte;
|
||||
TWCR = (1<<TWINT) | (1<<TWEN);
|
||||
while(!(TWCR & (1<<TWINT)));
|
||||
}
|
||||
|
||||
void i2c_readbuf(uint8_t sla, uint8_t adr, uint8_t len, uint8_t* buf)
|
||||
{
|
||||
i2c_start();
|
||||
i2c_write(sla);
|
||||
i2c_write(adr);
|
||||
i2c_start();
|
||||
i2c_write(sla + I2C_READ);
|
||||
while(len--) *buf++ = i2c_read(len ? I2C_ACK : I2C_NACK);
|
||||
i2c_stop();
|
||||
}
|
||||
|
||||
void i2c_writebuf(uint8_t sla, uint8_t adr, uint8_t len, uint8_t* buf)
|
||||
{
|
||||
i2c_start();
|
||||
i2c_write(sla);
|
||||
i2c_write(adr);
|
||||
|
||||
while (len--)
|
||||
{
|
||||
i2c_write(*buf++);
|
||||
}
|
||||
|
||||
i2c_stop();
|
||||
}
|
||||
16
i2c.h
Normal file
16
i2c.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef __I2C_H__
|
||||
#define __I2C_H__
|
||||
|
||||
#define I2C_ACK 1
|
||||
#define I2C_NACK 0
|
||||
#define I2C_READ 1
|
||||
|
||||
void i2c_init(uint32_t bitrate);
|
||||
void i2c_start(void);
|
||||
void i2c_stop(void);
|
||||
uint8_t i2c_read(uint8_t ack);
|
||||
void i2c_write(uint8_t byte);
|
||||
void i2c_readbuf(uint8_t sla, uint8_t adr, uint8_t len, uint8_t* buf);
|
||||
void i2c_writebuf(uint8_t sla, uint8_t adr, uint8_t len, uint8_t* buf);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user