c - 无论我发送什么,我都会在输出中收到 0

标签 c bluetooth

我正在开展一个项目,我必须在微处理器和蓝牙设备之间建立通信。我建立了通信,但是无论我发送什么,打印时都只得到0。感谢您的帮助!

#include <asf.h>
#include <string.h>
#include <stdio.h>
#include "usart.h"

/**
 *  Configure serial console.
 */
static void configure_console(void)
{
    const usart_serial_options_t uart_serial_options = {
        .baudrate = CONF_UART_BAUDRATE,
#ifdef CONF_UART_CHAR_LENGTH
        .charlength = CONF_UART_CHAR_LENGTH,
#endif
        .paritytype = CONF_UART_PARITY,
#ifdef CONF_UART_STOP_BITS
        .stopbits = CONF_UART_STOP_BITS,
#endif
    };

    /* Configure console. */
    stdio_serial_init(CONF_UART, &uart_serial_options);
}
volatile uint32_t ul_ms_ticks = 0;

static void mdelay(uint32_t ul_dly_ticks)
{
    uint32_t ul_cur_ticks;

    ul_cur_ticks = ul_ms_ticks;
    while ((ul_ms_ticks - ul_cur_ticks) < ul_dly_ticks) {
    }
}

void SysTick_Handler(void)
{
    ul_ms_ticks++;
}

#define USART_SERIAL                 USART1
#define USART_SERIAL_ID              ID_USART1  //USART1 for sam4l
#define USART_SERIAL_BAUDRATE        9600
#define USART_SERIAL_CHAR_LENGTH     US_MR_CHRL_8_BIT
#define USART_SERIAL_PARITY          US_MR_PAR_NO
#define USART_SERIAL_STOP_BIT        US_MR_NBSTOP_1

int main(void)
{
        /* Initialize the SAM system */
        sysclk_init();
        board_init();

        /* Initialize the console uart */
        configure_console();

        uint32_t a= 1234;
        int bit_rx = 6;


if (SysTick_Config(sysclk_get_cpu_hz() / 1000)) {
    while (1) {  /* Capture error */
    }
}

 const sam_usart_opt_t usart_console_settings = {
           USART_SERIAL_BAUDRATE,
           USART_SERIAL_CHAR_LENGTH,
           USART_SERIAL_PARITY,
           USART_SERIAL_STOP_BIT,
           US_MR_CHMODE_NORMAL
       };

       #if SAM4L
       sysclk_enable_peripheral_clock(USART_SERIAL);
       #else
       sysclk_enable_peripheral_clock(USART_SERIAL_ID);
       #endif

       usart_enable_tx(USART_SERIAL);
       usart_enable_rx(USART_SERIAL);


    while (1) {

        printf("%d \n", bit_rx );
                    usart_getchar(USART_SERIAL, &a);
                    bit_rx = a;
                    printf("%d \n",bit_rx );

                    if (a != 0) {
                        ioport_set_pin_level(LED_0_PIN, LED_0_ACTIVE);
                        } else {

                        ioport_set_pin_level(LED_0_PIN, !LED_0_ACTIVE);
                    }
                }
    }

最佳答案

也许您不应该在 usart_is_rx_ready() 返回 1 之前调用 usart_getchar()

关于c - 无论我发送什么,我都会在输出中收到 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30062858/

相关文章:

ios - Ellisys蓝牙嗅探Apple Airpods

mysql - 为什么在编译 MySQL 连接测试时会出现此错误?

android - 通过蓝牙将数据从 Arduino 发送到 Android 应用程序

c - C 函数的动态接口(interface)

你能帮我理解这个程序吗?

java - Android BluetoothSocket 输出流写入速度太慢

android - BluetoothDevice getName() 返回本地别名

bluetooth - 无法在 RN4020 蓝牙模块上创建多个私有(private) BLE 服务

c# - C#中的CRC16计算?

c - 如何初始化结构体中的数组字段?