Skip to content

Commit e18e2c0

Browse files
authored
added single byte functions
1 parent 29f76f2 commit e18e2c0

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

TWI/TWI.c

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void TWI_Init(uint16_t TheFrequency) {
2222
}
2323

2424
uint8_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

3434
void TWI_Stop() {
35-
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO); //send STOP
35+
TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN); //send STOP
3636
}
3737

3838
uint8_t TWI_WriteSLA(uint8_t TheSLA) {
@@ -59,7 +59,7 @@ uint8_t TWI_Write(uint8_t TheData) {
5959

6060
uint8_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+
95112
uint8_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
}

TWI/TWI.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@
1111

1212
#ifndef TWI_H_
1313
#define TWI_H_
14+
15+
#include <avr/io.h>
16+
#include <util/twi.h>
17+
#include <stdbool.h>
18+
19+
#define __TWI_FREQ_100K 100
20+
#define __TWI_FREQ_250K 250
21+
#define __TWI_FREQ_400K 400
22+
23+
void TWI_Init(uint16_t TheFrequency);
24+
uint8_t TWI_Transmit(uint8_t TheSlaveAddress, uint8_t TheData[], uint8_t TheDataSize, bool TheRepeatedStart);
25+
uint8_t TWI_TransmitByte(uint8_t TheSlaveAddress, uint8_t TheData, bool TheRepeatedStart);
26+
uint8_t TWI_Recieve(uint8_t TheSlaveAddress, uint8_t TheData[], uint8_t TheDataSize);
27+
uint8_t TWI_RecieveByte(uint8_t TheSlaveAddress);
1428

15-
#include <avr/io.h>
16-
#include <util/twi.h>
17-
#include <stdbool.h>
18-
19-
#define __TWI_FREQ_100K 100
20-
#define __TWI_FREQ_250K 250
21-
#define __TWI_FREQ_400K 400
22-
23-
void TWI_Init(uint16_t TheFrequency);
24-
uint8_t TWI_Transmit(uint8_t TheSlaveAddress, uint8_t TheData[], uint8_t TheDataSize, bool TheRepeatedStart);
25-
uint8_t TWI_Recieve(uint8_t TheSlaveAddress, uint8_t TheData[], uint8_t TheDataSize);
26-
27-
#endif /* TWI_H_ */
29+
#endif /* TWI_H_ */

0 commit comments

Comments
 (0)