STM32F303RE 串口接收中断处理:当从串口发数据时,MCU只能接收到一半数据,请教各位大神,这是怎么一回事?如图1 //中断处理程序 void USART2_IRQHandler(void) { CLI(); //disable all interrupts /* checks whether the specified USART interrupt has occurred or not */ if (USART_GetITStatus(USART2, USART_IT_RXNE)) { /* clear the RXNE pending bit */ USART_RequestCmd(USART2, USART_Request_RXFRQ, ENABLE); //USART_SendData(USART2, USART_ReceiveData(USART2)); HW_USART_SendOneChar(USART2, HW_USART_ReceOneChar(USART2)); } SEI(); //enable all interrupts } 当中断里直接用库函数USART_SendData(USART2, USART_ReceiveData(USART2))时接收正常 //串口程序 //115200 -8-1-0-0 #include "HW_USART.h" static void RCC_Configuration(void); static void NVIC_Configuration(void); static void GPIO_Configuration(GPIO_TypeDef* port); void HW_USART_Init(USART_TypeDef* usartnum, FunctionalState state) { USART_InitTypeDef USART_InitStructure; RCC_Configuration(); NVIC_Configuration(); GPIO_Configuration(Light_Run_Port); USART_InitStructure.USART_BaudRate = BaudRate; USART_InitStructure.USART_WordLength = WordLength; USART_InitStructure.USART_StopBits = StopBits; USART_InitStructure.USART_Parity = ParityChecking; USART_InitStructure.USART_HardwareFlowControl = FlowControl; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_DeInit(usartnum); USART_Init(usartnum, &USART_InitStructure); USART_ITConfig(usartnum, USART_IT_RXNE, state); //enable RXNE interrupt USART_ClearFlag (USART2, (USART_FLAG_TXE | USART_FLAG_TC)); //clear TXE TC flag USART_Cmd(usartnum, state); } void RCC_Configuration(void) { /* GPIOA Periph clock enable */ RCC_AHBPeriphClockCmd(GPIOA, ENABLE); /* USART2 Periph clock enable */ RCC_APB1PeriphClockCmd(USART2, ENABLE); } void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStruct; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); } void GPIO_Configuration(GPIO_TypeDef* port) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = (USART_TXpin | USART_RXpin); //RX TX GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //Alternate function mode GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //High speed GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //Output push-pull GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //Pull-up GPIO_Init(port, &GPIO_InitStructure); /* select the pins to be used as Alternate function */ GPIO_PinAFConfig(port, GPIO_PinSource2, GPIO_AF_7); GPIO_PinAFConfig(port, GPIO_PinSource3, GPIO_AF_7); } void HW_USART_SendOneChar(USART_TypeDef* usartnum, uint8_t data) { USART_SendData(usartnum, data); /* wait until transmission is complete */ while (!(USART_GetFlagStatus(usartnum, USART_FLAG_TC))) { ; } } void HW_USART_SendChars(USART_TypeDef* usartnum, uint16_t len, void *buff) { uint16_t i; uint8_t *ptr = (uint8_t *)buff; for (i = 0; i < len; i++) { HW_USART_SendOneChar(usartnum, ptr[i]); } } void HW_USART_SendString(USART_TypeDef* usartnum, void *buff) { uint8_t *ptr = (uint8_t *)buff; while (*ptr != '\0') { HW_USART_SendOneChar(usartnum, *ptr++); } } uint16_t HW_USART_ReceOneChar(USART_TypeDef* usartnum) { /* Wait until data to be received */ while (!(USART_GetFlagStatus(usartnum, USART_FLAG_RXNE))) { ; } return USART_ReceiveData(usartnum); } |
【STM32F303开发】开发学习笔记、教程集合贴
【合集】STM32F303开发教程
【STM32F303开发】+ 使用片内的CCMRAM缩短代码执行时间
【STM32F303开发】(五)TIM定时器PWM的输出与捕获
【STM32F303开发】+ 内部Flash模拟EEPROM
【STM32F303开发】+如何解析GPS数据
【STM32F303开发】+使用fromelf反汇编keil生成的AXF文件
使用STM32F303VBT6的SPI驱动TM1629A
【STM32F303开发】+ 使用SWO输出调试信息到Debug Viewer窗口
【STM32F303开发】基础:工程模板+LED+USART+常用文档+按键中断
{
USART_SendData(usartnum, data);
/* wait until transmission is complete */
while (!(USART_GetFlagStatus(usartnum, USART_FLAG_TC))) {
;
}
}
void HW_USART_SendChars(USART_TypeDef* usartnum, uint16_t len, void *buff)
{
uint16_t i;
uint8_t *ptr = (uint8_t *)buff;
for (i = 0; i < len; i++) {
HW_USART_SendOneChar(usartnum, ptr[i]);
}
}
void HW_USART_SendString(USART_TypeDef* usartnum, void *buff)
{
uint8_t *ptr = (uint8_t *)buff;
while (*ptr != '\0') {
HW_USART_SendOneChar(usartnum, *ptr++);
}
}
uint16_t HW_USART_ReceOneChar(USART_TypeDef* usartnum)
{
/* Wait until data to be received */
while (!(USART_GetFlagStatus(usartnum, USART_FLAG_RXNE))) {
;
}
return USART_ReceiveData(usartnum);
}
以上这几种发送在HAL库里都有对应的函数,可以试下,使用中断方式,应该没问题的
你好,
在中断里用USART_SendData(USART2, USART_ReceiveData(USART2));是正常的;
但是用自己的就有问题,串口发一个字节要发两次,MCU才能收到;串口发一大串数据的话就只能收到一半。
while (!(USART_GetFlagStatus(usartnum, USART_FLAG_TC))) {
;
} 这个是等待发送完成的,应该要加的吧。
1、是没有理解中断调用的机制,USART2_IRQHandler(void)是一个句柄,个人认为不应该把串口发送放在句柄里执行,应该找到回调函数放到回调函数里进行收发(发送完成或接收完成回调函数)。
2、为什么HAL库里有串口发送函数,封装好的,你不用,你还要自己写串口发送呢?
/* wait until transmission is complete */
while (!(USART_GetFlagStatus(usartnum, USART_FLAG_TC))) {
;
}
换成下面的判断函数试试
还是一样 呵呵
效果一样!真奇怪
邮箱 我把代码发给你 你帮忙试一下
呵呵 不是配置的原因 应该是程序的问题吧