Advertisement
Guest User

Untitled

a guest
Jun 20th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. int main(void)
  2. {
  3. DDRA =0xFF;
  4.  
  5. char data, olddata;
  6. int count = 0;
  7.  
  8. //USARTInit(6); //9600 baud w/o 1<<u2x
  9. USARTInit(0); //115.2k baud
  10.  
  11. PORTA = 0x00;
  12. bool lights = false;
  13. while(1)
  14. {
  15. //Read data
  16. data=USARTReadChar();
  17.  
  18. if (data != olddata)
  19. {
  20.  
  21. count++;
  22. PORTA = count;
  23. //PORTA = 0x00;
  24. //PORTA = data;
  25. olddata = data;
  26. USARTWriteChar(data);
  27. }
  28.  
  29. //USARTWriteChar(data);
  30. }
  31. }//end of main
  32.  
  33. void USARTInit(uint16_t ubrr_value)
  34. {
  35. //Set Baud rate
  36. UBRRL = ubrr_value;
  37. UBRRH = (ubrr_value>>8);
  38. /*Set Frame Format
  39. >> Asynchronous mode
  40. >> No Parity
  41. >> 1 StopBit
  42. >> char size 8
  43. */
  44. UCSRA = (1<<U2X);//double usart transmission speed
  45. UCSRC=(1<<URSEL)|(3<<UCSZ0);
  46. //Enable The receiver and transmitter
  47. UCSRB=(1<<RXEN)|(1<<TXEN);
  48. }
  49.  
  50.  
  51. //This function is used to read the available data
  52. //from USART. This function will wait until data is
  53. //available.
  54. char USARTReadChar(void)
  55. {
  56. //Wait until a data is available
  57. while(!(UCSRA & (1<<RXC)))
  58. {
  59. //Do nothing
  60. }
  61. //Now USART has got data from host
  62. //and is available is buffer
  63. return UDR;
  64. }
  65.  
  66. //This function writes the given "data" to
  67. //the USART which then transmit it via TX line
  68. void USARTWriteChar(char data)
  69. {
  70. //Wait until the transmitter is ready
  71. while(!(UCSRA & (1<<UDRE)))
  72. {
  73. //Do nothing
  74. }
  75. //Now write the data to USART buffer
  76. UDR=data;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement