/* * Return true if we received an ACK. */ static int swrite(unsigned _c) { unsigned char c = _c; int i; int ret; for(i = 0; i < 8; i++, c <<= 1) { if(c & 0x80) { SDA_1(); } else { SDA_0(); } SCL_1(); delay_400ns(); SCL_0(); } SDA_1(); SDA_IN(); SCL_1(); delay_400ns(); ret = !SDA_IS_1; SCL_0(); return ret; }
/*---------------------------------------------------------------------------*/ static unsigned sread(int send_ack) { int i; unsigned char c = 0x00; SDA_1(); SDA_IN(); for(i = 0; i < 8; i++) { c <<= 1; SCL_1(); delay_400ns(); if(SDA_IS_1) { c |= 0x1; } SCL_0(); } SDA_OUT(); if(send_ack) { SDA_0(); } SCL_1(); delay_400ns(); SCL_0(); SDA_1(); /* Release SDA */ SDA_IN(); return c; }
uint8_t tw_send_byte(uint8_t data) { register uint8_t i; for (i = 0; i <= 7; i++) { if (data & 0x80) { SDA_1(); } else { SDA_0(); } SCL_1(); _delay_loop_1(TWI_SW_DELAY); SCL_0(); _delay_loop_1(TWI_SW_DELAY); data = data << 1; } RELEASE_SDA(); SCL_1(); _delay_loop_1(TWI_SW_DELAY); if(bit_is_set(TWI_PIN_SDA, bSDA)) { SCL_0(); _delay_loop_1(TWI_SW_DELAY); return 0; //Acknowledge NOT received } SCL_0(); _delay_loop_1(TWI_SW_DELAY); return 1; }
int tw_recv_byte(uint8_t ack) { uint8_t i, data; RELEASE_SDA(); data = 0; for (i = 0; i <= 7; i++) { SCL_1(); _delay_loop_1(TWI_SW_DELAY); data <<= 1; if (bit_is_set (TWI_PIN_SDA, bSDA)) data++; SCL_0(); _delay_loop_1(TWI_SW_DELAY); } if (!ack) { SDA_1(); /*Set NO ACK*/ } else { SDA_0(); /*Set ACK*/ } SCL_1(); _delay_loop_1(TWI_SW_DELAY); SCL_0(); _delay_loop_1(TWI_SW_DELAY); return data; }
void tw_stop(void) { SDA_0(); SCL_1(); _delay_loop_1(TWI_SW_DELAY); SDA_1(); _delay_loop_1(TWI_SW_DELAY); SCL_0(); _delay_loop_1(TWI_SW_DELAY); //we need to free the bus RELEASE_SDA(); SCL_1(); _delay_loop_1(TWI_SW_DELAY); }
uint8_t tw_start(void) { RELEASE_SDA(); SCL_1(); _delay_loop_1(TWI_SW_DELAY); if(bit_is_clear(TWI_PIN_SDA,bSDA)) { return RC_FAIL; } SDA_0(); _delay_loop_1(TWI_SW_DELAY); SCL_0(); _delay_loop_1(TWI_SW_DELAY); return RC_SUCCESS; }
/*---------------------------------------------------------------------------*/ static void sstart(void) { SDA_1(); SCL_0(); delay_400ns(); SCL_1(); delay_400ns(); SDA_0(); delay_400ns(); SCL_0(); delay_400ns(); SCL_1(); delay_400ns(); SDA_1(); delay_400ns(); SCL_0(); }