Advertisement
Guest User

Untitled

a guest
Nov 25th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.84 KB | None | 0 0
  1. #define FOSC 16000000 // Clock Speed
  2. #define BAUD 9600
  3. #define MYUBRR FOSC/16/BAUD-1
  4. #include <avr/io.h>
  5.  
  6. void USART_Init( unsigned int ubrr)
  7. {
  8.     /*Set baud rate */
  9.     UBRR0H = (unsigned char)(ubrr>>8);
  10.     UBRR0L = (unsigned char)ubrr;
  11.     /* Enable receiver and transmitter */
  12.     UCSR0B = (1<<RXEN0)|(1<<TXEN0);
  13.     /* Set frame format: 8data, 2stop bit */
  14.     UCSR0C = (3<<UCSZ00);
  15. }
  16.  
  17. unsigned char USART_Receive( void )
  18. {
  19.     /* Wait for data to be received */
  20.     while ( !(UCSR0A & (1<<RXC0)) )
  21.     ;
  22.     /* Get and return received data from buffer */
  23.     return UDR0;
  24. }
  25.  
  26. void USART_Transmit( unsigned char data )
  27. {
  28.     /* Wait for empty transmit buffer */
  29.     while ( !( UCSR0A & (1<<UDRE0)) )
  30.     ;
  31.     /* Put data into buffer, sends the data */
  32.     UDR0 = data;
  33. }
  34.  
  35. void main( void )
  36. {
  37.     USART_Init(MYUBRR);
  38.     while (1)
  39.     {
  40.         USART_Transmit(USART_Receive());
  41.     }  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement