Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

UART stop bit is shorter than data bit

Hello Nordic Experts,

I am developing using the nRF52833, using the module BM833 and a custom board with the Nordic SDK.  I am running the UART example with some modifications to transmit 7 bytes every second to a display.

I have encountered an issue where the stop bit is shorter than the data bit. This discrepancy is causing my display to malfunction, and my PicoScope cannot decode the bytes correctly.

In the picture below I am transmitting 0x02 as my first byte. You can see that the stop bit is shorter than the data bit.

The code from my main file:

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "app_uart.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "nrf.h"
#include "nrf_gpio.h"  // Include the GPIO library

#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h"
#endif

#define UART_TX_BUF_SIZE 256    /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 256    /**< UART RX buffer size. */

#define RX_PIN UART_PIN_DISCONNECTED
#define TX_PIN 22
#define DIODE_PIN 25  // Define the pin number for the diode

void uart_error_handle(app_uart_evt_t * p_event)
{
    if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_communication);
    }
    else if (p_event->evt_type == APP_UART_FIFO_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_code);
    }
}

/* When UART is used for communication with the host do not use flow control.*/
#define UART_HWFC APP_UART_FLOW_CONTROL_DISABLED

/**
 * @brief Function for main application entry.
 */
int main(void)
{
    uint32_t err_code;

    const app_uart_comm_params_t comm_params =
    {
        RX_PIN,
        TX_PIN,
        UART_PIN_DISCONNECTED,
        UART_PIN_DISCONNECTED,
        UART_HWFC,
        false,
#if defined (UART_PRESENT)
        NRF_UART_BAUDRATE_9600
#else
        NRF_UARTE_BAUDRATE_9600
#endif
    };

    APP_UART_FIFO_INIT(&comm_params,
                       UART_RX_BUF_SIZE,
                       UART_TX_BUF_SIZE,
                       uart_error_handle,
                       APP_IRQ_PRIORITY_LOWEST,
                       err_code);

    APP_ERROR_CHECK(err_code);

    // Configure the diode pin as output
    nrf_gpio_cfg_output(DIODE_PIN);
    nrf_gpio_pin_clear(DIODE_PIN);  // Ensure the diode is off initially

    printf("\r\nUART example started.\r\n");

    uint8_t data_to_send[] = {0x02, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00};

    uint16_t value = 0;

    while (true)
    {
        // Update the value bytes
        data_to_send[3] = value & 0xFF;       // Least significant byte
        data_to_send[4] = (value >> 8) & 0xFF; // Most significant byte

        // Calculate checksum
        uint16_t checksum = 0;
        for (int i = 0; i < 5; i++)
        {
            checksum += data_to_send[i];
        }
        

        // Invert the bits of the checksum
        checksum = ~checksum;
        checksum -= 1;

        // Update the last two bytes with the inverted checksum, swapping LSB and MSB
        data_to_send[5] = checksum & 0xFF;        // Low byte
        data_to_send[6] = (checksum >> 8) & 0xFF; // High byte

        // Send the data
        for (int i = 0; i < sizeof(data_to_send); i++)
        {
            while (app_uart_put(data_to_send[i]) != NRF_SUCCESS);
            //nrf_delay_us(1200); // Add a small delay between each byte (adjust as needed)
        }
        
        // Toggle the diode pin state
        nrf_gpio_pin_toggle(DIODE_PIN);
        
        // Increment the value
        value++;
        
        nrf_delay_ms(1000); // Wait for 1 second before sending the next set of data
    }
}

Related