//-------------------------------------------------------------------------------------------------------------------- void TWI_TransmitData(unsigned char data) { TWDR = data; TWCR = (1 << TWINT)|(1 << TWEN); while (!(TWCR & (1<<TWINT))); if(TWI_STATUS != TWI_MT_DATA_ACK){TWI_ERROR();} }
uint8_t TWI_ReadByte_ACK() { TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA); while (!(TWCR & (1<<TWINT))); if (TW_STATUS != TW_MR_DATA_ACK) TWI_ERROR(I2C_NACK_ERROR); return TWDR; }
//-------------------------------------------------------------------------------------------------------------------- void TWI_SendSLAR(unsigned char addr) { TWDR = (addr << 1) + 1; TWCR = (1 << TWINT)|(1 << TWEN); while (!(TWCR & (1<<TWINT))); if(TWI_STATUS != TWI_MR_SLA_ACK){TWI_ERROR();} }
void TWI_WriteByte(uint8_t byte) { TWDR = byte; TWCR = (1<<TWINT) | (1<<TWEN); while (!(TWCR & (1<<TWINT))); if(TW_STATUS != TW_MT_DATA_ACK) TWI_ERROR(I2C_NACK_ERROR); }
//-------------------------------------------------------------------------------------------------------------------- unsigned char TWI_ReceiveData(void) { unsigned char data; TWCR = (1 << TWINT)|(1 << TWEN)|(1 << TWEA); while (!(TWCR & (1<<TWINT))); if(TWI_STATUS != TWI_MR_DATA_ACK){TWI_ERROR();} data = TWDR; return data; }
/* extra use */ char TWI_Write(char address, char *data, char n) { unsigned int SLA_W = (address<<1) & 0XFE; char chk; chk=TWI_Start(); if(chk==1) return chk; //--------------------------- TWDR = SLA_W; TWCR = (1<<TWINT)|(1<<TWEN); while (!(TWCR & (1<<TWINT))); if (TWI_STATUS != TWI_MT_SLA_ACK) { TWI_ERROR(); return 3; } //--------------------------- for(char i=0;i<n;i++) { TWDR = *(data+i); TWCR = (1<<TWINT)|(1<<TWEN); while (!(TWCR & (1<<TWINT))); if (TWI_STATUS != TWI_MT_DATA_ACK) { TWI_ERROR(); return 4; } } if(n>1) { TWI_Stop(); } else write_to_read=1; return 0; }
void TWI_RStart() { TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); //Wait for TWINT Flag set. This indicates that the START condition //has been transmitted while (!(TWCR & (1<<TWINT))); if ((TWSR & 0xF8) != TW_REP_START) { TWI_ERROR(I2C_REP_START_ERROR); //return I2C_START_ERROR; } //return 0; }
void TWI_Start() //Dodac zwracanie statusu wszedzie! { TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); //Wait for TWINT Flag set. This indicates that the START condition //has been transmitted TWI_WaitForFlag(); if ((TWSR & 0xF8) != TW_START) { TWI_ERROR(I2C_START_ERROR); //return I2C_START_ERROR; } //return 0; }
//-------------------------------------------------------------------------------------------------------------------- char TWI_ReStart(void) { TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); while (!(TWCR & (1<<TWINT))); if (TWI_STATUS != TWI_REP_START) { TWI_ERROR(); return 2; } else return 0; }
void TWI_Write_SLA(uint8_t address) { uint8_t status; if((address & 0x01) == 0) status = TW_MT_SLA_ACK; else status = TW_MR_SLA_ACK; TWDR = address; TWCR = (1<<TWINT) | (1<<TWEN); while (!(TWCR & (1<<TWINT))); //Czekaj na zakonczenie if (TW_STATUS != status) { TWI_ERROR(I2C_ACK_ERROR); //NACK error //return I2C_ACK_ERROR; //Send_String(table2,2); } //return 0; }
//-------------------------------------------------------------------------------------------------------------------- void TWI_SendNoAck(void) { TWCR = (1 << TWINT)|(1 << TWEN); while (!(TWCR & (1<<TWINT))); if(TWI_STATUS != TWI_MR_DATA_NACK){TWI_ERROR();} }
char TWI_Read(char address, char *data, char n) { unsigned int SLA_R = (address<<1) | 0X01; char chk; if(write_to_read==1) { chk=TWI_ReStart(); if(chk==2) return chk; write_to_read=0; } else chk=TWI_Start(); if(chk==1) return chk; //--------------------------- TWDR = SLA_R; TWCR = (1<<TWINT)|(1<<TWEN); while (!(TWCR & (1<<TWINT))); if (TWI_STATUS != TWI_MR_SLA_ACK) { TWI_ERROR(); return 5; } //--------------------------- if(n>1) { for(char i=0;i<(n-1);i++) { TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA); while (!(TWCR & (1<<TWINT))); if (TWI_STATUS != TWI_MR_DATA_ACK) { TWI_ERROR(); return 6; } *(data+i) = TWDR; } TWCR = (1<<TWINT)|(1<<TWEN); while (!(TWCR & (1<<TWINT))); if (TWI_STATUS != TWI_MR_DATA_NACK) { TWI_ERROR(); return 7; } *(data+(n-1)) = TWDR; //burst read �� data�̫��@���O�SACK�� } else { TWCR = (1<<TWINT)|(1<<TWEN); while (!(TWCR & (1<<TWINT))); if (TWI_STATUS != TWI_MR_DATA_NACK) { TWI_ERROR(); return 8; } *(data) = TWDR; //data�̫��@���O�SACK�� } TWI_Stop(); return 0; }