Skip to content

Instantly share code, notes, and snippets.

@stravant
Created September 20, 2012 16:00
Show Gist options
  • Save stravant/3756780 to your computer and use it in GitHub Desktop.
Save stravant/3756780 to your computer and use it in GitHub Desktop.
Ardiuno Comm
//maps pin => interrput number
const int INTMAP[] = {-1, -1, 0, 1, -1, -1};
const int COM_SendPin = 2;
const int COM_RecPin = 3;
const int COM_Timing = 6;
const int COM_HalfTiming = 3;
void COM_send(uint8_t b) {
digitalWrite(COM_SendPin, 1);
delayMicroseconds(COM_Timing);
uint8_t checkSum = 0;
for (int digit = 0; digit < 8; ++digit) {
uint8_t value = (b >> digit) & 0x1;
checkSum ^= value;
digitalWrite(COM_SendPin, value);
delayMicroseconds(COM_Timing);
}
digitalWrite(COM_SendPin, checkSum);
delayMicroseconds(COM_Timing);
digitalWrite(COM_SendPin, 1);
delayMicroseconds(COM_Timing);
digitalWrite(COM_SendPin, 0);
delayMicroseconds(COM_HalfTiming);
}
// volatile bool COM_Recieving = false;
uint8_t COM_recieve() {
while (!digitalRead(COM_RecPin));
//wait until the start bit is done
delayMicroseconds(COM_Timing);
//read bits
uint8_t number = 0;
uint8_t checkSum = 0;
for (int digit = 0; digit < 8; ++digit) {
delayMicroseconds(COM_HalfTiming);
uint8_t value = (digitalRead(COM_RecPin) ? 0x1 : 0x0);
checkSum ^= value;
number = (number << 1) | value;
delayMicroseconds(COM_HalfTiming);
}
delayMicroseconds(COM_HalfTiming);
uint8_t checkSumCheck = (digitalRead(COM_RecPin) ? 0x1 : 0x0);
delayMicroseconds(COM_HalfTiming);
if (checkSumCheck == checkSum) {
Serial.print("Sucess: ");
Serial.println(number, BIN);
} else {
Serial.print("Failed: ");
Serial.println(number, BIN);
}
delayMicroseconds(COM_Timing + COM_HalfTiming);
}
void setup() {
Serial.begin(9600);
pinMode(COM_RecPin, INPUT);
pinMode(COM_SendPin, OUTPUT);
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment