diff --git a/uno_def.c b/uno_def.c index ce6076d..f8568b5 100644 --- a/uno_def.c +++ b/uno_def.c @@ -113,4 +113,94 @@ void usart_win_cursor(bool on) usart_setstr("\e[?25h"); else usart_setstr("\e[?25l"); +} + +void i2c_init(unsigned long clock, bool pullup) +{ + TWBR = ((F_CPU/clock) - 16)/2; + + INPUT(A5); + INPUT(A4); + + if(pullup) + { + ON(A5); + ON(A4); + } + else + { + OFF(A5); + OFF(A4); + } +} + +void i2c_twi_start() +{ + TWCR = (1<>8, NACK); + i2c_twi_tx((uint8_t)data, NACK); +} + +uint8_t i2c_twi_rx(I2C_ACK ack) +{ + TWCR = (1<>8, NACK); + i2c_twi_tx((uint8_t)data, NACK); + + i2c_twi_stop(); } \ No newline at end of file diff --git a/uno_def.h b/uno_def.h index c5ef44c..dfc18e5 100644 --- a/uno_def.h +++ b/uno_def.h @@ -10,6 +10,7 @@ #define UNO_DEF_H_ #include "glob_def.h" +#include "stdint.h" #include // LED @@ -173,4 +174,29 @@ void usart_setint(int i, BASE base); void usart_win_cursor(bool onoff); +typedef enum +{ + READ = 1, + WRITE = 0, +} I2C_RW; + +typedef enum +{ + ACK = 0, + NACK = 1, +} I2C_ACK; + +void i2c_init(unsigned long clock, bool pullup); + +void i2c_twi_start(); +void i2c_twi_tx(uint8_t data, I2C_ACK ack); +void i2c_twi_tx16(uint16_t data, I2C_ACK ack); +uint8_t i2c_twi_rx(I2C_ACK ack); +void i2c_twi_stop(); + +void i2c_tx(uint8_t address, uint8_t data, I2C_ACK ack); +uint8_t i2c_rx(uint8_t address, I2C_ACK ack); + +void i2c_tx16(uint8_t address, uint16_t data, I2C_ACK ack); + #endif /* UNO_DEF_H_ */ \ No newline at end of file