void sendByte(byte data) {
pinMode(DATA_PIN, OUTPUT); // to output mode
// Send start bit
digitalWrite(DATA_PIN, LOW);
delayMicroseconds(500);
// Send data bits
for (int i = 0; i < 8; i++) {
digitalWrite(DATA_PIN, (data & (1 << i)) ? HIGH : LOW); // send one bit
delayMicroseconds(500); // duration of one bit
}
digitalWrite(DATA_PIN, HIGH); // Idle state
pinMode(DATA_PIN, INPUT); // back to input mode
}
byte receiveByte() {
byte received = 0;
// wait for LOW (start bit)
while (digitalRead(DATA_PIN) == HIGH);
delayMicroseconds(500)
for (int i = 0; i < 8; i++) {
delayMicroseconds(250); // wait for the half of bit
if (digitalRead(DATA_PIN) == HIGH) {
received |= (1 << i); // read the bit
}
delayMicroseconds(250); // to the end of bit
}
return received;
}
void sendByte(byte data) {
pinMode(DATA_PIN, OUTPUT); // to output mode
// Send start bit
digitalWrite(DATA_PIN, LOW);
delayMicroseconds(500);
// Send data bits
for (int i = 0; i < 8; i++) {
digitalWrite(DATA_PIN, (data & (1 << i)) ? HIGH : LOW); // send one bit
delayMicroseconds(500); // duration of one bit
}
digitalWrite(DATA_PIN, HIGH); // Idle state
pinMode(DATA_PIN, INPUT); // back to input mode
}
byte receiveByte() {
byte received = 0;
// wait for LOW (start bit)
while (digitalRead(DATA_PIN) == HIGH);
delayMicroseconds(500)
for (int i = 0; i < 8; i++) {
delayMicroseconds(250); // wait for the half of bit
if (digitalRead(DATA_PIN) == HIGH) {
received |= (1 << i); // read the bit
}
delayMicroseconds(250); // to the end of bit
}
return received;
}
In addition there may be some timing problems. Testing with an ATtiny1614 instead of an ATtiny13, the delayMicrosecondsdelayMicroseconds was found to give too short delays, resulting in the byte received being wrong, the reply being started too early, and the reply having wrong timing.
This problems could be solved by including
#define F_CPU 16000000UL
#include <avr/delay.h>
#define F_CPU 16000000UL
#include <avr/delay.h>