#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/uart.h"
#define UART0_TX_PIN 0
#define UART0_RX_PIN 1
#define UART1_TX_PIN 4
#define UART1_RX_PIN 5
void uart_clean_rx(uart_inst_t *uart) {
while (uart_is_readable(uart)) uart_getc(uart);
}
int main() {
stdio_init_all();
// ## Configuracoes da UART0
uart_init(uart0, 9600); // Inicializa a UART0 com baud rate de 115200
gpio_set_function(UART0_TX_PIN, GPIO_FUNC_UART); // Configura pino 0 como TX
gpio_set_function(UART0_RX_PIN, GPIO_FUNC_UART); // Configura pino 1 como RX
// ## Configuracoes da UART1
uart_init(uart1, 9600); // Inicializa a UART1 com baud rate de 115200
gpio_set_function(UART1_TX_PIN, GPIO_FUNC_UART); // Configura pino 4 como TX
gpio_set_function(UART1_RX_PIN, GPIO_FUNC_UART); // Configura pino 5 como RX
while (true) {
char uart_tx[100] = {0};
char uart_rx[100] = {0};
// Leitura da string de transmissao
printf("Insira a string: \n");
scanf(" %[^\n]s", uart_tx); // o espaco no inicio skipa o buffer deixado
sleep_ms(100);
// Limpa buffer da uart1
uart_clean_rx(uart1);
// Transmite a string
uart_puts(uart0, uart_tx);
sleep_ms(100);
// Aguarda ate que tenha algo para receber
while (!uart_is_readable(uart1)) {
sleep_ms(100);
}
// Ler os caracteres recebidos na uart1
int index = 0;
while (uart_is_readable(uart1)) {
char c = uart_getc(uart1);
uart_rx[index++] = c;
sleep_ms(100);
}
printf("\nString recebida: %s\n", uart_rx);
sleep_ms(1000);
}
}