working with delays
This commit is contained in:
@@ -14,10 +14,6 @@ void i2c_init(void) {
|
||||
TWBR = ((F_CPU / TWI_FREQ) - 16) / (2 * TWI_PRESCALE_VALUE); // 22.9.1 : (Bit Rate Register) set SCL frequency (formula from datasheet, 22.5.2)
|
||||
}
|
||||
|
||||
void i2c_start_repeat(void) { // 22.7.1 : "Repeated START enables the Master to switch between Slaves, Master Transmitter mode and Master Receiver mode without losing control of the bus"
|
||||
i2c_start();
|
||||
}
|
||||
|
||||
void i2c_start(void) {
|
||||
i2c_status = START;
|
||||
TWCR = TWI_START_CONDITION; // 22.9.2 : (Control Register) send Start condition (22.7.1) ! writting 1 to TWINT clears it (set it to 0)
|
||||
@@ -28,6 +24,9 @@ void i2c_start(void) {
|
||||
return i2c_stop();
|
||||
}
|
||||
}
|
||||
void i2c_start_repeat(void) { // 22.7.1 : "Repeated START enables the Master to switch between Slaves, Master Transmitter mode and Master Receiver mode without losing control of the bus"
|
||||
i2c_start();
|
||||
}
|
||||
|
||||
void i2c_send_addr_w(uint8_t slave_address) {
|
||||
if (i2c_status == STOP) {
|
||||
@@ -93,8 +92,8 @@ uint8_t i2c_read_ack(void) {
|
||||
TWCR = TWI_ACKNOWLEDGE; // 22.7.1 : Master Transmitter Mode
|
||||
while (!(TEST(TWCR, TWINT))); // 22.7.1 : Wait for TWINT Flag set. This indicates that the SLA+R/W has been transmitted, and ACK/NACK has been received
|
||||
|
||||
uint8_t status = TWSR & 0b11111000; // p225 example code : Check status for Receiver mode. Mask prescaler bits. If status different from MT_DATA_ACK go to ERROR
|
||||
if (status != TW_MT_DATA_ACK) {
|
||||
uint8_t status = TWSR & 0b11111000; // Table 22-3 : check status for Receiver mode, mask prescaler bits
|
||||
if (status != TW_MR_DATA_ACK) {
|
||||
i2c_stop();
|
||||
return 0;
|
||||
}
|
||||
@@ -110,8 +109,8 @@ uint8_t i2c_read_nack(void) {
|
||||
TWCR = TWI_NACKNOWLEDGE; // 22.7.1 : Master Transmitter Mode
|
||||
while (!(TEST(TWCR, TWINT))); // 22.7.1 : Wait for TWINT Flag set. This indicates that the SLA+R/W has been transmitted, and ACK/NACK has been received
|
||||
|
||||
uint8_t status = TWSR & 0b11111000; // p225 example code : Check status for Receiver mode. Mask prescaler bits. If status different from MT_DATA_ACK go to ERROR
|
||||
if (status != TW_MT_DATA_NACK) {
|
||||
uint8_t status = TWSR & 0b11111000; // Table 22-3 : check status for Receiver mode, mask prescaler bits
|
||||
if (status != TW_MR_DATA_NACK) {
|
||||
i2c_stop();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user