#define F_CPU 1000000UL #include #include #include #include #include #include #include "lcd.h" #define BUF_SIZE 40 #define TRUE 1 #define FALSE 0 volatile unsigned char buf_in[BUF_SIZE], ptr_in; unsigned char ptr_out; SIGNAL (SIG_UART_RECV){ PORTD = ~PORTD; //toggle LED when character received buf_in[ptr_in] = UDR; //put data into buffer ptr_in = ++ptr_in % BUF_SIZE; // update pointer } // sends a character to the serial port int s_putc(char car){ while( !(UCSRA & _BV(UDRE)) ); //wait for transmit buffer to be ready UDR = car; //send character return 0; } // reads a character from the serial port int s_getc(void) { char car; while(ptr_in == ptr_out); //wait for the buffer to contain at least one character car = buf_in[ptr_out]; //read first character from buffer ptr_out = ++ptr_out % BUF_SIZE; //point to next character return car; } void ioinit (void) { DDRD = 0x20; //outputs : LED PORTD = 0x00; //turn LED off at boot. DDRA = 0xFF; // LCD : PA7:2 outputs, PA0:1 unused PORTA = 0x00; //Start with LCD RS reset, for initialization. //set up USART UBRRH = 0x00; UBRRL = 12; //Baud Rate = 9600 UCSRA = 0x02; //mode asynchrone, fast clock UCSRC = 0X86; //asynchronous, no parity, one stop bit, 8 data bits UCSRB = 0x98; //transmitter and receiver enabled, interrupts on receiver sei(); //enable global interrupts (hence receive buffer) } int main (void){ unsigned char car, car2=0; ioinit (); fdevopen(lcd_putc, s_getc, 0); // assign custom I/O functions to stdout & stdin init_lcd(); while(TRUE){ //display the LCD module's character table car = s_getc(); //press a key to show next character if (car == 13) { //press "Enter" to clear the screen and return to top left lcd_clear(); }else lcd_putc(car2++); //display character } }