Last active
August 29, 2015 14:24
-
-
Save ibanezmatt13/b4e3d7d682e8a9c716a6 to your computer and use it in GitHub Desktop.
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
#include <avr/io.h> | |
#include <avr/interrupt.h> | |
#include <string.h> | |
#define USART_BAUDRATE 9600 | |
//#define BAUD_PRESCALE ((((F_CPU / 16) + (USART_BAUDRATE / 2) / (USART_BAUDRATE))-1) | |
#define BAUD_PRESCALE 104 | |
static volatile char rxbuff[100]; // to fill from USART | |
static volatile char parsebuff[120]; // separate buffer for parsing | |
static volatile unsigned int write_pointer = 0; | |
static unsigned int DEBUG_PIN = 5; | |
static unsigned int TIMEOUT = 3; | |
static unsigned int CRASH_LED = 4; | |
static volatile unsigned int need_data = 0; | |
static volatile unsigned int got_data = 0; | |
static volatile unsigned int timeout_counter = 0; | |
static volatile unsigned int error = 0; | |
ISR(USART_RXC_vect) | |
{ | |
if (need_data){ | |
if (write_pointer <= 99){ | |
rxbuff[write_pointer] = UDR0; | |
if (rxbuff[write_pointer] == '\n'){ | |
got_data = 1; | |
need_data = 0; | |
write_pointer = 0; | |
} | |
write_pointer++; | |
} | |
else { | |
write_pointer = 0; | |
got_data = 1; | |
} | |
} | |
} | |
ISR(TIMER1_COMPA_vect) | |
{ | |
timeout_counter++; | |
PORTB ^= (1 << CRASH_LED); | |
} | |
void tx_string(char* data) | |
{ | |
unsigned int i = 0; | |
while((data[i] != '\n') && (i < 100)) | |
{ | |
while (!( UCSR0A & (1 << UDRE0))){} | |
UDR0 = data[i]; | |
i++; | |
} | |
} | |
void initialise_timer(void) | |
{ | |
TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode | |
TIMSK1 |= (1 << OCIE1A); // Enable CTC interrupt | |
OCR1A = 15624; // Set CTC compare value | |
TCCR1B |= ((1 << CS10) | (1 << CS12)); | |
} | |
void initialise_USART(void) | |
{ | |
UCSR0B = ((1 << RXEN0) | (1 << TXEN0)); // enable TX and RX lines | |
UCSR0C = (1 << USBS0) | (3 << UCSZ00); // set 8 data bits, 2 stop bits | |
// set higher and lower parts of 16 bit register for setting baud rate | |
UBRR0H = (BAUD_PRESCALE >> 8); | |
UBRR0L = BAUD_PRESCALE; | |
UCSR0B |= (1 << RXCIE0); // enable RX_complete interrupt | |
} | |
void parse_data(void) | |
{ | |
tx_string(parsebuff); | |
} | |
int main(void) | |
{ | |
cli(); | |
DDRB |= (1 << DEBUG_PIN) | (1 << CRASH_LED); | |
initialise_timer(); | |
initialise_USART(); | |
sei(); // Enable global interrupts | |
for (;;){ | |
need_data = 1; // I need data! | |
while ((!got_data) && (timeout_counter < TIMEOUT)){} // do nothing (will be more efficient eventually) | |
if (timeout_counter >= TIMEOUT){ | |
tx_string("timeout...\n"); // BEWARE: tx_string needs a \n, I think... needs confirmation | |
PORTB ^= (1 << DEBUG_PIN); | |
error = 1; | |
} else if (error == 0) { | |
need_data = 0; | |
strncpy(parsebuff, rxbuff, sizeof(rxbuff)); | |
parse_data(); | |
need_data = 1; | |
} else { | |
tx_string("Got error...\n"); | |
} | |
timeout_counter = 0; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment