Advertisement
Guest User

Untitled

a guest
Jun 20th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. int main (void)
  2. {
  3. board_init();
  4.  
  5. init();
  6. sei();//enable interrupt
  7.  
  8. while (1)
  9. {
  10.  
  11. }
  12. }
  13.  
  14. void init()
  15. {
  16. USARTInit(0,true);
  17. }
  18.  
  19. //UART receiver interrupt routine
  20. ISR (USART_RXC_vect)
  21. {
  22. char status, ch;
  23.  
  24. status = UCSRA;
  25. ch = UDR;
  26.  
  27. //Check if error
  28. if((status & (_BV(FE) | _BV(DOR))) != 0)
  29. return;
  30.  
  31. //send back to PC
  32. uart_send_char(ch);
  33. }
  34.  
  35. void USARTInit(uint16_t ubrr_value, bool first)
  36. {
  37. //Set Baud rate
  38. UBRRL = ubrr_value;
  39. UBRRH = (ubrr_value>>8);
  40. /*Set Frame Format
  41. >> Asynchronous mode
  42. >> No Parity
  43. >> 1 StopBit
  44. >> char size 8
  45. */
  46. if (first)
  47. {
  48. UCSRA = (1<<U2X);//double usart transmission speed
  49. }
  50. UCSRC=(1<<URSEL)|(3<<UCSZ0);
  51. //Enable The receiver and transmitter
  52. UCSRB=(1<<RXEN)|(1<<TXEN);
  53. }
  54.  
  55. void uart_send_char(unsigned char ch)
  56. {
  57. UDR = ch;
  58. //Wait for empty transfer buffer
  59. while(!(UCSRA & _BV(UDRE)));
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement