luanpcs

Relatório 10

Jul 24th, 2021 (edited)
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define FOSC 16000000U // Clock Speed
  4. #define BAUD 9600
  5. #define MYUBRR FOSC / 16 / BAUD - 1
  6. #define botao (1 << PD4)
  7. char msg_tx[20];
  8. char msg_rx[32];
  9. int pos_msg_rx = 0;
  10. int tamanho_msg_rx = 3;
  11. unsigned int x = 0, valor = 0;
  12. //Prototipos das funcoes
  13. void UART_Init(unsigned int ubrr);
  14. void UART_Transmit(char *dados);
  15. int main(void)
  16. {
  17.   UART_Init(MYUBRR);
  18.   sei();
  19.   PORTD |= botao;
  20.   UART_Transmit("Digite 'ola':\n");
  21.   x = 0;
  22.   while (x == 0)
  23.   {
  24.     if ((msg_rx[0] == 'o') &&
  25.         (msg_rx[1] == 'l') &&
  26.         (msg_rx[2] == 'a'))
  27.     {
  28.       x = 1;
  29.     }
  30.   }
  31.   UART_Transmit("Digite '250':\n");
  32.   x = 0;
  33.   valor = 0;
  34.   while (x == 0)
  35.   {
  36.     valor = (msg_rx[0] - 48) * 100 +
  37.             (msg_rx[1] - 48) * 10 +
  38.             (msg_rx[2] - 48) * 1;
  39.     if (valor == 250)
  40.       x = 1;
  41.   }
  42.   x = 0;
  43.   UART_Transmit("Aperte o botao:\n");
  44.   // Super-loop
  45.   while (1)
  46.   {
  47.     if ((PIND & botao) == 0) // O botao foi pressionado?
  48.     {
  49.       // Se sim, envia mensagem
  50.       UART_Transmit("Hello World!\n");
  51.       x++;
  52.       UART_Transmit("num vezes botao press: ");
  53.       itoa(x, msg_tx, 10);
  54.       UART_Transmit(msg_tx);
  55.       UART_Transmit("\n");
  56.       _delay_ms(500); // Aguarda um tempo para evitar o bounce
  57.     }
  58.   }
  59. }
  60. ISR(USART_RX_vect)
  61. {
  62.   // Escreve o valor recebido pela UART na posição pos_msg_rx do buffer msg_rx
  63.   msg_rx[pos_msg_rx++] = UDR0;
  64.   if (pos_msg_rx == tamanho_msg_rx)
  65.     pos_msg_rx = 0;
  66. }
  67. void UART_Transmit(char *dados)
  68. {
  69.   // Envia todos os caracteres do buffer dados ate chegar um final de linha
  70.   while (*dados != 0)
  71.   {
  72.     while (!(UCSR0A & (1 << UDRE0))); // Aguarda a transmissão acabar
  73.     // Escreve o caractere no registro de tranmissão
  74.     UDR0 = *dados;
  75.     // Passa para o próximo caractere do buffer dados
  76.     dados++;
  77.   }
  78. }
  79. void UART_Init(unsigned int ubrr)
  80. {
  81.   // Configura a baud rate */
  82.   UBRR0H = (unsigned char)(ubrr >> 8);
  83.   UBRR0L = (unsigned char)ubrr;
  84.   // Habilita a recepcao, tranmissao e interrupcao na recepcao */
  85.   UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);
  86.   // Configura o formato da mensagem: 8 bits de dados e 1 bits de stop */
  87.   UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
  88. }
  89.  
Add Comment
Please, Sign In to add comment