uart.c 525 B

1234567891011121314151617181920212223242526272829
  1. #include<avr/io.h>
  2. #define F_CPU 16000000UL
  3. #include <util/delay.h>
  4. #include "uart.h"
  5. #define USART_BAUD 115200UL
  6. #define BAUDRATE (F_CPU/(8UL*USART_BAUD))-1
  7. void uart_init()
  8. {
  9. /* Set baud rate */
  10. UBRR0H = (uint8_t) ((BAUDRATE) >> 8);
  11. UBRR0L = (uint8_t) BAUDRATE;
  12. UCSR0B = (1 << RXEN0) | (1 << TXEN0); // | (1 << RXCIE0);
  13. UCSR0C = (3 << UCSZ00);
  14. UCSR0A = 1 << U2X0;
  15. }
  16. void putch(uint8_t c)
  17. {
  18. while (!(UCSR0A & (1 << UDRE0))) ;
  19. UDR0 = c;
  20. }
  21. uint8_t getch(void)
  22. {
  23. while (!(UCSR0A & (1 << RXC0))) ;
  24. return UDR0;
  25. }