串口接收标志位语句_串口接收到数据后RXNE标志位不置1

问题描述:

最近在做一个关于串口读压力模块压力值的项目,用到的压力模块的基本工作原理为,通过串口向压力模块发送读压力指令"1:R:MRMD:0:0"(字符串),压力模块会返回压力值字符串"1:F:MRMD:101.00:KPA"(非定长字符串),收到压力值字符串后需要对其进行分割,以提取标识位'F'、压力值'101.00',以及压力单位'KPA'。

我做该项目的思路是通过DMA传输的方式将串口接收到的压力字符串从串口数据寄存器传输到内存,通过串口的总线空闲中断(IDLE)判断压力值字符串是否接收完成,若接收完成触发中断程序,在中断程序中对字符串中进行分割并提取具有关键值的子串。

但是通过keil仿真器仿真过程中rxne状态位在接收到字符串后始终不会被置位,导致空闲中断不被响应,最终导致压力字符串没不能被有效处理,刚接触stm32,不知道问题出在哪里,希望前辈能指点一二。

下面是我的程序:

主函数:

void Delay(__IO u32 nCount)     /*自定义的延时函数*/

{

for(;nCount!=0;nCount--);

}

void USART_SendChar(USART_TypeDef* USARTx,uint8_t data)     /*自定义的串口发送数据函数*/

{

USART_SendData(USARTx,data);

while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)==RESET);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,根据您的要求,这里给出一个简单的示例代码,实现了您的需求: ``` #include "stm32f10x.h" #include "stdio.h" // 定义串口接收缓冲区 #define RX_BUF_SIZE 32 volatile char rx_buf[RX_BUF_SIZE]; volatile uint8_t rx_buf_idx = 0; // 定义锁的控制引脚 #define LOCK_PIN GPIO_Pin_0 #define LOCK_PORT GPIOA // 串口接收中断服务函数 void USART1_IRQHandler(void) { if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { char ch = USART_ReceiveData(USART1); if (rx_buf_idx < RX_BUF_SIZE - 1) { rx_buf[rx_buf_idx++] = ch; } } } // 初始化串口 void USART1_Init(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; // 使能串口和GPIO时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); // 配置USART1 Tx (PA.09) 为推挽输出模式 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); // 配置USART1 Rx (PA.10) 为浮空输入模式 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); // 配置串口参数 USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); // 使能串口接收中断 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 配置串口中断优先级 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); // 使能串口 USART_Cmd(USART1, ENABLE); } // 初始化GPIO void GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; // 使能GPIOA时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 配置锁的控制引脚为输出模式 GPIO_InitStructure.GPIO_Pin = LOCK_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(LOCK_PORT, &GPIO_InitStructure); } // 获取温度ADC值 uint16_t get_temp_adc(void) { // TODO: 实现获取温度ADC值的代码 return 0; } int main(void) { // 初始化串口和GPIO USART1_Init(); GPIO_Init(); // 接收lock标志位 while (rx_buf_idx < 1) { // 等待接收数据 } // 根据接收到的标志位控制锁的状态 if (rx_buf[0] == '0') { GPIO_ResetBits(LOCK_PORT, LOCK_PIN); } else if (rx_buf[0] == '1') { GPIO_SetBits(LOCK_PORT, LOCK_PIN); } // 接收get_temp_adc指令 while (rx_buf_idx < 11) { // 等待接收数据 } // 解析指令并获取ADC值 if (strncmp(rx_buf, "get_temp_adc", 12) == 0) { uint16_t adc_value = get_temp_adc(); // 将ADC值发送回上位机 char buf[6]; sprintf(buf, "%d", adc_value); USART_SendData(USART1, (uint8_t *) buf, strlen(buf)); } // 程序结束 while (1); } ``` 需要注意的是,这里使用了串口接收中断来接收数据,因此在主循环中没有使用while语句。另外,由于您没有给出具体的温度控制方式,这里的get_temp_adc函数只是一个空函数,需要您自己根据实际情况来实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值