trojanxem

Untitled

Jun 6th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. #include <stdint.h>      //Bibloiteki
  2. #include <avr/io.h>
  3.  
  4. //#include <util/delay.h>
  5. // Define baud rate
  6. #define F_CPU 8000000          // taktowanie
  7. #define USART_BAUDRATE 9600     // predkosc bandwith rate
  8. #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)       // wzor ktory Ci przeslalem na wyliczenie baudprescale
  9.  
  10. void USART_vInit(void)
  11. {
  12.  
  13. // Set baud rate
  14. UBRRH = (uint8_t)(51>>8);  // musisz to wyliczyc na podstaiwe taktowania twego badziewia
  15. UBRRL = (uint8_t)51; // to samo
  16.  
  17.  
  18. // Set frame format to 8 data bits, no parity, 1 stop bit
  19. UCSRC = (0<<USBS)|(3<<UCSZ0);    // ustawiasz format na 8 bitowy, ostatni bit przekazuje sygnal
  20.  
  21. // Enable receiver and transmitter
  22. UCSRB = (1<<RXEN)|(1<<TXEN);     // wlaczasz odbiornik i nadjanij
  23.  
  24. }
  25.  
  26.  
  27.  
  28. uint8_t USART_vReceiveByte()       // odbierz bit
  29.  
  30. {
  31.  
  32. // Wait until a byte has been received
  33.  
  34. while((UCSRA&(1<<RXC)) == 0);        // wykonuj dopoki bit nie dotrze
  35.  
  36. // Return received data
  37.  
  38. return UDR;    // zwroc wartosc UDR
  39.  
  40. }
  41.  
  42.  
  43. int main(void)
  44.  
  45. {
  46. DDRB = 0xFF ; // jest to liczba 255 czyli ostatni najwiekszy mozliwy bit
  47. uint8_t u8Data;   // zamiana u8data na maly int 8 bitowy
  48.  
  49.  
  50. // Initialize USART
  51. USART_vInit();   // incjalizacja usart
  52.  
  53.  
  54.  
  55.  
  56.  
  57. // Repeat indefinitely
  58. for(;;) //forever      
  59.  
  60. {
  61.  
  62. // Echo received characters
  63. u8Data = USART_vReceiveByte();
  64.  
  65.  
  66. switch (u8Data) { // decode what char just came in
  67. case 'a' :
  68. PORTB =0xff;
  69. break;
  70. case 'b' :
  71. PORTB =0x00;
  72. break;
  73.  
  74. }
  75.  
  76. }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment