spacepaste

  1.  
  2. #include <avr/io.h>
  3. #include <avr/interrupt.h>
  4. #define F_CPU 16000000
  5. #define USART_BAUDRATE 9600
  6. //#define BAUD 9600
  7. #define BAUD 76800
  8. //#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
  9. #define BAUD_PRESCALE ((uint16_t) ((F_CPU / ((USART_BAUDRATE) * 16.0)) + .5) - 1)
  10. #include <util/setbaud.h>
  11. int main (void)
  12. {
  13. char ReceivedByte;
  14. UCSR0B = (1 << RXEN0) | (1 << TXEN0); // Turn on the transmission and reception circuitry
  15. //UCSR0C = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes
  16. UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // Use 8-bit character sizes
  17. UBRR0H = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
  18. UBRR0L = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
  19. for (;;) // Loop forever
  20. {
  21. while ((UCSR0A & (1 << RXC0)) == 0) {}; // Do nothing until data have been received and is ready to be read from UDR
  22. ReceivedByte = UDR0; // Fetch the received byte value into the variable "ByteReceived"
  23. while ((UCSR0A & (1 << UDRE0)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it
  24. UDR0 = ReceivedByte; // Echo back the received byte back to the computer
  25. }
  26. }
  27.