RT,使用串口发送一些数据,其中某些相邻的2个字节会变成1个字节的0x3F,不知道是什么原因,请问有遇到类似问题的大神吗?
比如通过串口发送数据:
C2 00 00 18 2E 00
用串口调试助手收到的数据是:
3F 00 18 2E 00
再比如通过串口发送数据:
80 D3 FF 2F
用串口调试助手收到的数据是:
80 3F 2F
情况并不是必现的,但是大部分情况都会发生,而且总是将两个字节变为 0x3F,很诡异。
初始化串口的代码:
- static void _usart_baudrate_config(uint32_t BaudRate)
- {
- uint32_t BaudRate_Mantissa = 0;
- uint32_t ClockFreq = bclk_get_sysclk_freq(); // 12MHz 时钟
- BaudRate_Mantissa = (uint32_t)(ClockFreq / BaudRate );
- /* Set the fraction of USARTDIV */
- USART1->BRR2 = (uint8_t)((BaudRate_Mantissa >> (uint8_t)8) & (uint8_t)0xF0);
- /* Set the MSB mantissa of USARTDIV */
- USART1->BRR2 |= (uint8_t)(BaudRate_Mantissa & (uint8_t)0x0F);
- /* Set the LSB mantissa of USARTDIV */
- USART1->BRR1 = (uint8_t)(BaudRate_Mantissa >> (uint8_t)4);
- }
- void init_433(void)
- {
- // PC5/USART_TX --> RXD 推挽输出
- GPIO_Init(GPIOC, GPIO_Pin_5, GPIO_Mode_Out_PP_High_Fast);
- // PC6/USART_RX --> TXD 上拉输入
- GPIO_Init(GPIOC, GPIO_Pin_6, GPIO_Mode_In_PU_No_IT);
- // 配置串口时钟
- CLK_PeripheralClockConfig(CLK_Peripheral_USART1, ENABLE);
- SYSCFG_REMAPPinConfig(REMAP_Pin_USART1TxRxPortC, ENABLE);
- // 初始化串口
- USART_DeInit(USART1);
- // GPIO_ExternalPullUpConfig(GPIOC, GPIO_Pin_5 | GPIO_Pin_6, ENABLE);
- USART_Init(USART1, (uint32_t) 9600, USART_WordLength_8b, USART_StopBits_1, \
- USART_Parity_No, USART_Mode_Tx | USART_Mode_Rx);
- _usart_baudrate_config(9600); // 按照 12MHz 时钟重新配置波特率
- // 使能串口
- USART_Cmd(USART1, ENABLE);
- }
复制代码 串口发送数据代码:
- void send_byte_433(uint8_t data)
- {
- USART_SendData8(USART1, data);
- while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
- USART_ClearFlag(USART1, USART_FLAG_TXE);
- // bsp_delay(1000); // 加了延时也没用,还是会出现 0x3F
- }
- void send_data_433(const uint8_t *data, uint32_t len)
- {
- uint32_t i = 0;
- for (i = 0; i < len; i++)
- {
- send_byte_433(data[i]);
- }
- while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
- USART_ClearFlag(USART1, USART_FLAG_TC);
- wait_send_433(len);
- }
复制代码
|
假设发送两个字节 a 和 b,满足以下条件时串口实际发送的数据就是 0x3F:
比如通过串口发送数据:
81 7F 81 80 80 7F 80 80
用串口调试助手收到的数据是:
3F 81 80 80 7F 80 80
80 3F 2F
void send_byte_433(uint8_t data)
{
USART_SendData8(USART1, data);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
void send_data_433(const uint8_t *data, uint32_t len)
{
uint32_t i = 0;
for (i = 0; i < len; i++)
{
send_byte_433(data[i]);
}
wait_send_433(len);
}