// INTERRUPT-DRIVEN serial code // Fully interrupt driven, asynch UART communication // Mega644 version #include #include #include #include //set up the debugging utility ASSERT #define __ASSERT_USE_STDERR #include #include "uart.h" // UART file descriptor // putchar and getchar are in uart.c FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW); //timeout values for each task #define t1 100 #define t2 200 //I like these definitions #define begin { #define end } //the subroutines void task1(void); // Waits for a string and sets a flag void task2(void); // Prints the system time and string from task1 void getstr_int(void); // Starts getting a string from serial line void putstr_int(void); // Starts a send to serial line void initialize(void); // All the usual mcu stuff volatile int time1, time2 ; // task scheduling timeout counters volatile long time; // system time unsigned int v; // number entered by human char newstr ; // task1-->task2 handshake // RXC ISR variables volatile char r_index; //current string index volatile char r_buffer[16]; //input string volatile char r_ready; //flag for receive done volatile char r_char; //current character // TX ISR variables volatile char t_index; //current string index volatile char t_buffer[16]; //output string volatile char t_ready; //flag for transmit done volatile char t_char; //current character //********************************************************** //timer 0 overflow ISR ISR (TIMER0_COMPA_vect) begin // increment system time in mSec time++; //Decrement the times if they are not already zero if (time1>0) --time1; if (time2>0) --time2; end //********************************************************** // UART character-ready ISR // builds a sting and signals when the string is complete // supports backspace ISR (USART0_RX_vect) begin r_char = UDR0 ; //get a char UDR0 = r_char; //then print it //build the input string if (r_char != '\r') // Is the input a ? begin if (r_char == '\b') // Is the input a backspace? begin putchar(' '); // erase the character on the screen putchar('\b'); // backup --r_index ; // wipe a character from the string end else r_buffer[r_index++] = r_char ; // add a character to the string end else // Human pressed begin putchar('\n'); //use putchar to avoid overwrite r_buffer[r_index] = 0x00; //zero terminate r_ready = 1; //signal cmd processor UCSR0B ^= (1<0) begin // if so, send one chararcter putchar(t_buffer[0]); // and turn on transmit (UDR empty) ISR UCSR0B |= (1<