// USART demo program // Original author: Dr. Christopher Taylor // modified by mlh #include #include #include #include "globaldef.h" static const unsigned int UART_BAUD_RATE = 300; static const unsigned char CR = 0x0d; static const unsigned char LF = 0x0a; void uart_init(const uint32_t clock, const uint16_t baud_rate); unsigned char uart_getc(); void uart_putc(const unsigned char character); void uart_puts( const char* msg ); int main() { cli(); // disable interrupts for now - effectively disables Atmon too // Do not enable global interrupts until USART interrupts are disabled, // in order to effectively disable Atmon. DDRB = 0xFF; // enable PortB PORTB = 0x00; // lower all lines uart_init(F_CPU, UART_BAUD_RATE); sei(); // Reenable global interrupts now that we've disabled local USART interrupts unsigned char letter=0; uart_putc('>'); while(true) // do forever { letter = uart_getc(); // receive a char uart_putc(letter); // echo it back if(letter==CR) // and if it was a CR, echo some more stuff back { uart_putc(LF); uart_putc('>'); } if(letter=='s') // and if it was 's', echo even more stuff back { uart_putc(LF); uart_putc(CR); char secretMessage[] = "J!tff!efbe!qfpqmf"; uart_puts( secretMessage ); uart_putc(LF); uart_putc(CR); } } return 0; } void uart_init(const uint32_t clock, const uint16_t baud_rate) { // Set baud rate uint16_t uart_baud = (clock/baud_rate)/16 - 1; UBRRH = (uint8_t)(uart_baud>>8); // high 8 bits UBRRL = (uint8_t)(uart_baud); // truncate high 8 bits of 16-bit value // Enable receiver and transmitter // Disable RXCIE, TXCIE, UDRIE interrupts UCSRB = (1<