8.Master_I2C_PWM/i2c.c
2023-06-26 23:58:53 +03:00

38 lines
707 B
C

#include "i2c.h"
void i2c_init() {
TWBR = I2C_BITRATE;
}
void i2c_start() {
TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
while (!(TWCR & (1 << TWINT)))
;
}
void i2c_stop() {
TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN);
while (TWCR & (1 << TWSTO))
;
}
void i2c_write(uint8_t data) {
TWDR = data;
TWCR = (1 << TWINT) | (1 << TWEN);
while (!(TWCR & (1 << TWINT)))
;
}
uint8_t i2c_read_ack() {
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
while (!(TWCR & (1 << TWINT)))
;
return TWDR;
}
uint8_t i2c_read_nack() {
TWCR = (1 << TWINT) | (1 << TWEN);
while (!(TWCR & (1 << TWINT)))
;
return TWDR;
}