Created
October 25, 2014 14:22
-
-
Save erinaceous/daf77c95b07afad7a868 to your computer and use it in GitHub Desktop.
arduino software serial library that doesn't use interrupts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// bits per second, measured in microseconds. | |
#define HALF_BIT_4800 104; | |
#define FULL_BIT_4800 208; | |
unsigned char uart_rx(int pin, int baud) { | |
unsigned char rec = 0; | |
// double pause = 0.0; | |
// double half_pause = 0.0; | |
int i = 0; | |
// this is a bit pointless for now. we're only reading the GPS at 4800bps. | |
// but might want to read it quicker in future... | |
/* switch(baud) { | |
case 4800: | |
default: | |
pause = FULL_BIT_4800; | |
half_pause = HALF_BIT_4800; | |
break; | |
} */ | |
double pause = FULL_BIT_4800; | |
double half_pause = HALF_BIT_4800; | |
start_again: | |
rec = 0; | |
while(DigitalRead(pin) == LOW); // wait for line to be high | |
while(DigitalRead(pin) == HIGH); // wait for line to fall (start bit) | |
DelayMicroseconds(half_pause); | |
if(DigitalRead(pin) != LOW) goto start_again; | |
DelayMicroseconds(pause); | |
if(DigitalRead(pin) == HIGH) rec += 1; | |
DelayMicroseconds(pause); | |
if(DigitalRead(pin) == HIGH) rec += 2; | |
DelayMicroseconds(pause); | |
if(DigitalRead(pin) == HIGH) rec += 4; | |
DelayMicroseconds(pause); | |
if(DigitalRead(pin) == HIGH) rec += 8; | |
DelayMicroseconds(pause); | |
if(DigitalRead(pin) == HIGH) rec += 16; | |
DelayMicroseconds(pause); | |
if(DigitalRead(pin) == HIGH) rec += 32; | |
DelayMicroseconds(pause); | |
if(DigitalRead(pin) == HIGH) rec += 64; | |
DelayMicroseconds(pause); | |
if(DigitalRead(pin) == HIGH) rec += 128; | |
DelayMicroseconds(pause); | |
if(DigitalRead(pin) != HIGH) goto start_again; | |
return rec; | |
} | |
void uart_tx(char data, int baud, int pin) { | |
/* double pause = 0; | |
switch(baud) { | |
case 4800: | |
default: | |
pause = FULL_BIT_4800; | |
break; | |
} */ | |
double pause = FULL_BIT_4800; | |
DigitalWrite(pin, LOW) // transmit start bit | |
DelayMicroseconds(pause); | |
if(data & 1) DigitalWrite(pin, HIGH); | |
else DigitalWrite(pin, LOW); | |
DelayMicroseconds(pause); | |
if(data & 2) DigitalWrite(pin, HIGH); | |
else DigitalWrite(pin, LOW); | |
DelayMicroseconds(pause); | |
if(data & 4) DigitalWrite(pin, HIGH); | |
else DigitalWrite(pin, LOW); | |
DelayMicroseconds(pause); | |
if(data & 8) DigitalWrite(pin, HIGH); | |
else DigitalWrite(pin, LOW); | |
DelayMicroseconds(pause); | |
if(data & 16) DigitalWrite(pin, HIGH); | |
else DigitalWrite(pin, LOW); | |
DelayMicroseconds(pause); | |
if(data & 32) DigitalWrite(pin, HIGH); | |
else DigitalWrite(pin, LOW); | |
DelayMicroseconds(pause); | |
if(data & 64) DigitalWrite(pin, HIGH); | |
else DigitalWrite(pin, LOW); | |
DelayMicroseconds(pause); | |
if(data & 128) DigitalWrite(pin, HIGH); | |
else DigitalWrite(pin, LOW); | |
DelayMicroseconds(pause); | |
DigitalWrite(pin, HIGH); // transmit stop bit | |
DelayMicroseconds(pause); | |
} | |
void uart_str(char *str, int baud, int pin) { | |
int i; | |
for(i=0; i<strlen(str); i++) { | |
uart_tx(str[i], baud, pin); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment