Advertisement
mikolajmki

mikro_lab05

Nov 18th, 2022
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1. #define F_CPU            8000000UL // ustawienie częstotliwości zegara – proponowane 8MHz
  2. #define BAUDRATE         9600 //określenie prędkości przesyłania
  3. #define BAUD_PRESCALLER  (((F_CPU / (BAUDRATE * 16UL))) - 1) //wyznaczenie zawartości rejestru UBRR ze wzoru
  4.  
  5. #include <avr/io.h>
  6. #include <avr/interrupt.h>
  7. #include <util/delay.h>
  8.  
  9.  
  10. char tekst[] = "Mikolaj Marcinkowski";
  11.  
  12. void USART_Init(void)
  13. {
  14.     /* ustawienie baud */
  15.     UBRRH = (uint8_t)(BAUD_PRESCALLER>>8);
  16.     UBRRL = (uint8_t)(BAUD_PRESCALLER);
  17.    
  18.     //konfiguracja (baud rate = 9600, ramka: 8 bitów danych, brak kontroli parzystości, 1 bit stopu)
  19.     UCSRA=0;
  20.     UCSRC=(1<<URSEL) | (1<<UCSZ0) | (1<<UCSZ1);
  21.     UCSRB=(1<<RXEN) | (1<<TXEN);
  22. }
  23.  
  24. void USART_putchar(unsigned char data)
  25. {
  26.     while (!(UCSRA & (1<<UDRE)));
  27.     UDR = data;
  28. }
  29.  
  30. char tekst_ok[] = "Nacisnieto odpowiedni przycisk.";
  31. char tekst_nieok[] = "Nacisnieto zly przycisk.";
  32. char znak = 'h';
  33.  
  34. unsigned char UART_RxChar()
  35. {
  36.     while ((UCSRA & (1 << RXC)) == 0);/* Wait till data is received */
  37.     return(UDR); /* Return the byte */
  38. }
  39.  
  40. void napis(char *text,int n)
  41. {
  42. uint8_t i;
  43.  
  44.     for (i=0;i<n;i++)
  45.     USART_putchar(text[i]);
  46.     USART_putchar('\r');
  47.     USART_putchar('\n'); // nowa linia
  48. }
  49.  
  50. void switchNum(c) {
  51.     PORTA = 2 ^ (c - 48);
  52.     if (c == 'c') PORTA = 0x00;
  53.     if (c == 's') PORTA = 0xFF;
  54. }
  55.  
  56. int main(void)
  57. {
  58.     uint8_t i;
  59.     unsigned char c;
  60.     DDRA |= 0xFF;
  61.     PORTA |= 0x00;
  62.  
  63.     _delay_ms(100);
  64.     USART_Init();
  65.  
  66.         napis(tekst, sizeof(tekst));
  67.         while (1) {
  68.         c=UART_RxChar();/*
  69.         if (c==znak)
  70.             napis(tekst_ok,sizeof(tekst_ok));
  71.         else
  72.             napis(tekst_nieok,sizeof(tekst_nieok));*/
  73.         switchNum(c);
  74.         }
  75.  
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement