raspberrypi / pico-examples

BSD 3-Clause "New" or "Revised" License
2.83k stars 820 forks source link

UART RX Garbage data recived #478

Closed engrzamanimran closed 5 months ago

engrzamanimran commented 7 months ago

I am working on a recieving example but I recive the garbage data. ``#include

include "pico/stdlib.h"

include "hardware/uart.h"

include "hardware/irq.h"

include "hardware/rtc.h"

include "pico/util/datetime.h"

define UART_ID uart0

define BAUD_RATE 115200

define UART_TX_PIN 0

define UART_RX_PIN 1

define DATA_BITS 8

define STOP_BITS 1

define PARITY UART_PARITY_NONE

define UART_BUFFER_SIZE 32

char buf[UART_BUFFER_SIZE]; uint32_t rx_buf_index = 0;

void on_uart_rx();

int main() { stdio_init_all(); char datetime_buf[256]; char *datetime_str = &datetime_buf[0]; datetime_t t = { .year = 2024, .month = 03, .day = 01, .dotw = 5, .hour = 15, .min = 45, .sec = 00 }; rtc_init(); rtc_set_datetime(&t); sleep_us(64); const uint LED_PIN = 25; gpio_init(LED_PIN); gpio_set_dir(LED_PIN, GPIO_OUT); gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART); gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART); uart_init(UART_ID, 2400); int __unused actual = uart_set_baudrate(UART_ID, BAUD_RATE); uart_set_hw_flow(UART_ID,false,false); uart_set_format(UART_ID,DATA_BITS,STOP_BITS,PARITY); uart_set_fifo_enabled(UART_ID,false); // int UART_IRQ = UART_ID == uart0 ? UART0_IRQ : UART1_IRQ; `

// irq_set_exclusive_handler(UART_IRQ, on_uart_rx);
// irq_set_enabled(UART_IRQ,true);

// uart_set_irq_enables(UART_ID, true, false);

while (true) {
    // rtc_get_datetime(&t);
    // datetime_to_str(datetime_str,sizeof(datetime_buf), &t);
    // printf("\r%s    ", datetime_str);
    // sleep_ms(100);
    gpio_put(LED_PIN, 1);
    sleep_ms(250);
    gpio_put(LED_PIN, 0);
    sleep_ms(250);

    while(uart_is_readable(UART_ID)) {
    uint8_t ch = uart_getc(UART_ID);
    printf("%s",ch);
}

    // uart_putc_raw(UART_ID, 'A');
    // uart_putc(UART_ID,'B');
    // uart_puts(UART_ID, "Hello, UART!\n");
}

}

// RX interrupt handler // void on_uart_rx() {

// }`

Output: The garbage data image

mcjurij commented 7 months ago

Either baud settings wrong or baud rate too high.

Cheers, Jurij

On Sat, Mar 2, 2024 at 11:44 AM Zaman Imran @.***> wrote:

I am working on a recieving example but I recive the garbage data. ``#include

include "pico/stdlib.h"

include "hardware/uart.h"

include "hardware/irq.h"

include "hardware/rtc.h"

include "pico/util/datetime.h"

define UART_ID uart0

define BAUD_RATE 115200

define UART_TX_PIN 0

define UART_RX_PIN 1

define DATA_BITS 8

define STOP_BITS 1

define PARITY UART_PARITY_NONE

define UART_BUFFER_SIZE 32

char buf[UART_BUFFER_SIZE]; uint32_t rx_buf_index = 0;

void on_uart_rx();

int main() { stdio_init_all(); char datetime_buf[256]; char *datetime_str = &datetime_buf[0]; datetime_t t = { .year = 2024, .month = 03, .day = 01, .dotw = 5, .hour = 15, .min = 45, .sec = 00 }; rtc_init(); rtc_set_datetime(&t); sleep_us(64); const uint LED_PIN = 25; gpio_init(LED_PIN); gpio_set_dir(LED_PIN, GPIO_OUT); gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART); gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART); uart_init(UART_ID, 2400); int __unused actual = uart_set_baudrate(UART_ID, BAUD_RATE); uart_set_hw_flow(UART_ID,false,false); uart_set_format(UART_ID,DATA_BITS,STOP_BITS,PARITY); uart_set_fifo_enabled(UART_ID,false); // int UART_IRQ = UART_ID == uart0 ? UART0_IRQ : UART1_IRQ; `

// irq_set_exclusive_handler(UART_IRQ, on_uart_rx); // irq_set_enabled(UART_IRQ,true);

// uart_set_irq_enables(UART_ID, true, false);

while (true) { // rtc_get_datetime(&t); // datetime_to_str(datetime_str,sizeof(datetime_buf), &t); // printf("\r%s ", datetime_str); // sleep_ms(100); gpio_put(LED_PIN, 1); sleep_ms(250); gpio_put(LED_PIN, 0); sleep_ms(250);

while(uart_is_readable(UART_ID)) {
uint8_t ch = uart_getc(UART_ID);
printf("%s",ch);

}

// uart_putc_raw(UART_ID, 'A');
// uart_putc(UART_ID,'B');
// uart_puts(UART_ID, "Hello, UART!\n");

}

}

// RX interrupt handler // void on_uart_rx() {

// }`

Output: The garbage data image.png (view on web) https://github.com/raspberrypi/pico-examples/assets/134497987/76120daa-07e1-4fdb-8922-dc157694238b

— Reply to this email directly, view it on GitHub https://github.com/raspberrypi/pico-examples/issues/478, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7XKSAQPKK5PFGHLFSITHTYWGUSXAVCNFSM6AAAAABEDAATZ2VHI2DSMVQWIX3LMV43ASLTON2WKOZSGE3DINZVGM4TIMA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

mcjurij commented 7 months ago

See also: https://www.reddit.com/r/raspberrypipico/comments/uc9v1r/comment/i69n41m/

Cheers, Jurij

engrzamanimran commented 7 months ago

Either baud settings wrong or baud rate too high. Cheers, Jurij On Sat, Mar 2, 2024 at 11:44 AM Zaman Imran @.> wrote: I am working on a recieving example but I recive the garbage data. ``#include #include "pico/stdlib.h" #include "hardware/uart.h" #include "hardware/irq.h" #include "hardware/rtc.h" #include "pico/util/datetime.h" #define UART_ID uart0 #define BAUD_RATE 115200 #define UART_TX_PIN 0 #define UART_RX_PIN 1 #define DATA_BITS 8 #define STOP_BITS 1 #define PARITY UART_PARITY_NONE #define UART_BUFFER_SIZE 32 char buf[UART_BUFFER_SIZE]; uint32_t rx_buf_index = 0; void on_uart_rx(); int main() { stdio_init_all(); char datetime_buf[256]; char datetime_str = &datetime_buf[0]; datetime_t t = { .year = 2024, .month = 03, .day = 01, .dotw = 5, .hour = 15, .min = 45, .sec = 00 }; rtc_init(); rtc_set_datetime(&t); sleep_us(64); const uint LED_PIN = 25; gpio_init(LED_PIN); gpio_set_dir(LED_PIN, GPIO_OUT); gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART); gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART); uart_init(UART_ID, 2400); int __unused actual = uart_set_baudrate(UART_ID, BAUD_RATE); uart_set_hw_flow(UART_ID,false,false); uart_set_format(UART_ID,DATA_BITS,STOP_BITS,PARITY); uart_set_fifo_enabled(UART_ID,false); // int UART_IRQ = UART_ID == uart0 ? UART0_IRQ : UART1_IRQ; // irq_set_exclusive_handler(UART_IRQ, on_uart_rx); // irq_set_enabled(UART_IRQ,true); // uart_set_irq_enables(UART_ID, true, false); while (true) { // rtc_get_datetime(&t); // datetime_to_str(datetime_str,sizeof(datetime_buf), &t); // printf("\r%s ", datetime_str); // sleep_ms(100); gpio_put(LED_PIN, 1); sleep_ms(250); gpio_put(LED_PIN, 0); sleep_ms(250); while(uart_is_readable(UART_ID)) { uint8_t ch = uart_getc(UART_ID); printf("%s",ch); } // uart_putc_raw(UART_ID, 'A'); // uart_putc(UART_ID,'B'); // uart_puts(UART_ID, "Hello, UART!\n"); } } // RX interrupt handler // void on_uart_rx() { // } Output: The garbage data image.png (view on web) https://github.com/raspberrypi/pico-examples/assets/134497987/76120daa-07e1-4fdb-8922-dc157694238b — Reply to this email directly, view it on GitHub <#478>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7XKSAQPKK5PFGHLFSITHTYWGUSXAVCNFSM6AAAAABEDAATZ2VHI2DSMVQWIX3LMV43ASLTON2WKOZSGE3DINZVGM4TIMA . You are receiving this because you are subscribed to this thread.Message ID: **@.***>

I have tried the communication with different baud rates and different board but issue remain the same.

peterharperuk commented 7 months ago

Can you try changing that %s to %c?

peterharperuk commented 5 months ago

Closing due lack of response to last comment