#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/uart.h"
// Define the UART0 TX and RX pins
#define UART0_TX_PIN 0 // GPIO0 is UART0 TX
#define UART0_RX_PIN 1 // GPIO1 is UART0 RX
#define UART1_TX_PIN 4 // GPIO4 is UART1 TX
#define UART1_RX_PIN 5 // GPIO5 is UART1 RX
#define BAUD_RATE 115200 // Standard baud rate for serial communication
int main() {
// Initialize stdio for debugging over USB
stdio_init_all();
// Print a message to the USB console
printf("Iniciando comunicação UART...\n");
// Initialize UART0 and UART1 with the desired baud rate
uart_init(uart0, BAUD_RATE);
uart_init(uart1, BAUD_RATE);
// Configure GPIO pins for UART communication
gpio_set_function(UART0_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART0_RX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART1_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART1_RX_PIN, GPIO_FUNC_UART);
// Buffers to store the name entered and the received name
char name[80];
char name_received[80];
while (true) {
// Prompt the user to enter their name
printf("\nDigite o seu nome: ");
// Read the name from the terminal
if (scanf("%79s", name) == 1) { // Limiting input to avoid buffer overflow
// Send the name via UART1
uart_puts(uart1, name);
} else {
printf("Erro ao ler o nome.\n"); // Handle input errors
}
// Wait a moment to ensure the name is sent properly
sleep_ms(200);
// Wait for data to be received via UART1
int i = 0;
while (uart_is_readable(uart1)) { // Check if data is available to read
char c = uart_getc(uart1); // Receive one character at a time
if (c == '\n') break; // Stop reading on newline
name_received[i++] = c; // Store the received character in buffer
}
name_received[i] = '\0'; // Null-terminate the string
// Print the received name if we got any data
if (i > 0) {
printf("\nRecebido pela UART1: %s\n", name_received);
printf("------------------------------\n");
}
sleep_ms(500);
}
return 0;
}