@@ -22,7 +22,7 @@ void TWI_Init(uint16_t TheFrequency) {
2222}
2323
2424uint8_t TWI_Start () {
25- TWCR = (1 << TWINT ) | (1 << TWEN ) | (1 << TWSTA ); //send START
25+ TWCR = (1 << TWINT ) | (1 << TWSTA ) | (1 << TWEN ); //send START
2626
2727 while (!(TWCR & (1 << TWINT ))); //wait for the TWINT flag
2828
@@ -32,7 +32,7 @@ uint8_t TWI_Start() {
3232}
3333
3434void TWI_Stop () {
35- TWCR = (1 << TWINT ) | (1 << TWEN ) | (1 << TWSTO ); //send STOP
35+ TWCR = (1 << TWINT ) | (1 << TWSTO ) | (1 << TWEN ); //send STOP
3636}
3737
3838uint8_t TWI_WriteSLA (uint8_t TheSLA ) {
@@ -59,7 +59,7 @@ uint8_t TWI_Write(uint8_t TheData) {
5959
6060uint8_t TWI_Read (bool TheACKReply ) {
6161 if (TheACKReply ) { //end with ACK
62- TWCR = (1 << TWINT ) | (1 << TWEN ) | (1 << TWEA ); //initialize transmission with the ACK bit
62+ TWCR = (1 << TWINT ) | (1 << TWEA ) | (1 << TWEN ); //initialize transmission with the ACK bit
6363 while (!(TWCR & (1 << TWINT ))); //wait for the TWINT flag
6464 if (TW_STATUS != TW_MR_DATA_ACK ) return TW_STATUS ; //check if there is an error
6565 }
@@ -92,6 +92,23 @@ uint8_t TWI_Transmit(uint8_t TheSlaveAddress, uint8_t TheData[], uint8_t TheData
9292 return OP_SUCCESSFUL ;
9393}
9494
95+ uint8_t TWI_TransmitByte (uint8_t TheSlaveAddress , uint8_t TheData , bool TheRepeatedStart ) {
96+ uint8_t ErrorCode ;
97+
98+ ErrorCode = TWI_Start (); //send START
99+ if (ErrorCode != OP_SUCCESSFUL ) return ErrorCode ; //return if there is an error, otherwise carry on
100+
101+ ErrorCode = TWI_WriteSLA (TheSlaveAddress << 1 | TW_WRITE ); //send slave address with the Read/Write flag
102+ if (ErrorCode != OP_SUCCESSFUL ) return ErrorCode ; //return if there is an error, otherwise carry on
103+
104+ ErrorCode = TWI_Write (TheData ); //send the data
105+ if (ErrorCode != OP_SUCCESSFUL ) return ErrorCode ; //return if there is an error, otherwise carry on
106+
107+ if (!TheRepeatedStart ) TWI_Stop (); //don't send STOP if TheRepeatedStart is enabled
108+
109+ return OP_SUCCESSFUL ;
110+ }
111+
95112uint8_t TWI_Recieve (uint8_t TheSlaveAddress , uint8_t TheData [], uint8_t TheDataSize ) {
96113 uint8_t ErrorCode ;
97114
@@ -109,4 +126,21 @@ uint8_t TWI_Recieve(uint8_t TheSlaveAddress, uint8_t TheData[], uint8_t TheDataS
109126 TWI_Stop (); //send STOP
110127
111128 return OP_SUCCESSFUL ;
129+ }
130+
131+ uint8_t TWI_RecieveByte (uint8_t TheSlaveAddress ) {
132+ uint8_t ErrorCode ;
133+ uint8_t RecievedByte ;
134+
135+ ErrorCode = TWI_Start (); //send START
136+ if (ErrorCode != OP_SUCCESSFUL ) return 0 ; //return if there is an error, otherwise carry on
137+
138+ ErrorCode = TWI_WriteSLA (TheSlaveAddress << 1 | TW_READ ); //send slave address with the Read/Write flag
139+ if (ErrorCode != OP_SUCCESSFUL ) return 0 ; //return if there is an error, otherwise carry on
140+
141+ RecievedByte = TWI_Read (TW_READ_NACK ); //send a NACK to end the transmission
142+
143+ TWI_Stop (); //send STOP
144+
145+ return RecievedByte ;
112146}
0 commit comments