Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. /*
  2. * UART test.c
  3. *
  4. * Created: 30-Jun-16 9:48:05 PM
  5. * Author : Lan
  6. */
  7. #define F_CPU 506880 // internal fuses set to 8mhz on internal RC oscillator
  8. #include <avr/io.h>
  9. #include <avr/interrupt.h>
  10. #include <util/delay.h>
  11.  
  12.  
  13.  
  14. #define BAUD 9600
  15. #define MYUBRR F_CPU/16/BAUD-1
  16.  
  17.  
  18. void USART_Init( unsigned int baud )
  19. {
  20. /* Set baud rate */
  21. UBRRH = (unsigned char)(baud>>8);
  22. UBRRL = (unsigned char)baud;
  23. /* Enable receiver and transmitter */
  24. UCSRB = (1<<RXEN)|(1<<TXEN);
  25. /* Set frame format: 8data, 2stop bit(USBS bit) */
  26. UCSRC = (1<<USBS)|(3<<UCSZ0);
  27. }
  28.  
  29. void USART_Transmit( unsigned char data )
  30. {
  31. /* Wait for empty transmit buffer */
  32. while ( !( UCSRA & (1<<UDRE)) )
  33. ;
  34. /* Put data into buffer, sends the data */
  35. UDR = data;
  36. }
  37.  
  38. unsigned char USART_Receive( void )
  39. {
  40. /* Wait for data to be received */
  41. while ( !(UCSRA & (1<<RXC)) )
  42. ;
  43. /* Get and return received data from buffer */
  44. return UDR;
  45. }
  46.  
  47.  
  48. int main(void)
  49. {
  50. USART_Init(MYUBRR);
  51. char A;
  52. /* Replace with your application code */
  53. while (1)
  54. {
  55. USART_Transmit(0xAA);
  56. A = USART_Receive();
  57. USART_Transmit(A);
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement