C 无论发送什么,我都会在输出中不断接收0
我正在做一个项目,我必须在我的微处理器和蓝牙设备之间建立通信。我建立了一个通信,但无论我发送什么,打印时都只能得到0。谢谢你的帮助C 无论发送什么,我都会在输出中不断接收0,c,bluetooth,C,Bluetooth,我正在做一个项目,我必须在我的微处理器和蓝牙设备之间建立通信。我建立了一个通信,但无论我发送什么,打印时都只能得到0。谢谢你的帮助 #include <asf.h> #include <string.h> #include <stdio.h> #include "usart.h" /** * Configure serial console. */ static void configure_console(void) { const usar
#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.h”
/**
*配置串行控制台。
*/
静态void配置控制台(void)
{
常量usart\u串行\u选项\u uart\u串行\u选项={
.baudrate=CONF_UART_baudrate,
#ifdef CONF_UART_CHAR_长度
.charlength=CONF\u UART\u CHAR\u LENGTH,
#恩迪夫
.paritytype=CONF\u UART\u奇偶校验,
#ifdef CONF_UART_STOP_位
.stopbits=CONF\u UART\u STOP\u位,
#恩迪夫
};
/*配置控制台*/
stdio_serial_init(CONF_UART和UART_serial_选项);
}
易失性uint32_t ul_ms_ticks=0;
静态空隙时间(uint32_t ul_d_)
{
uint32_t ul_cur_ticks;
ul_cur_ticks=ul_ms_ticks;
while((ul_ms_ticks-ul_cur_ticks)
也许在usart\u getchar()准备就绪()之前,您不应该调用usart\u getchar()
,
返回1。谢谢大家!
我首先尝试转换接收到的变量:bit_rx=(int)a;
然后我发现我需要这个来让它工作:
static usart_serial_options_t usart_options = {
.baudrate = USART_SERIAL_BAUDRATE,
.charlength = USART_SERIAL_CHAR_LENGTH,
.paritytype = USART_SERIAL_PARITY,
.stopbits = USART_SERIAL_STOP_BIT
};
usart_serial_init(USART_SERIAL, &usart_options);
我禁用了停止位,因为我的蓝牙没有:
#define USART_SERIAL_STOP_BIT false
现在它就像一个符咒。谢谢大家的帮助 我想您是想说当您打印
位rx
时,它的输出0
?此外,我看不到您的代码中有任何地方在第一次打印之前修改了bit\u rx
的值。你的第一次打印正确吗?你的第二个呢?我建议使用调试器并单步执行代码。同样,也提供了大量无用的代码。请参阅第页以整理您的问题并获得更好的答案。另外,usart\u getchar(usart\u SERIAL,&a)代码>然后位\u rx=a代码>对我来说似乎非常错误。正如我在谷歌搜索usart\u getchar()
时所看到的,这个函数返回一个char
,但是你要把它传递给bit\u rx
,它是一个int?