1、STM32 的串口中断配置, 也是很简单的.首先是配置 UART 的 GPIO 口 /* Name : UART1_GPIO_Configuration* Deion : Configures the uart1 GPIO ports.* Input : None* Output : None* Return : None*/void UART1_GPIO_Configuration(void)GPIO_InitTypeDef GPIO_InitStructure;/ Configure USART1_Tx as alternate push-pull GPIO_InitStructure.G
2、PIO_Pin = GPIO_Pin_9;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_Init(GPIOA, / Configure USART1_Rx as input floating GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_Init(GPIOA, 然后是配置串口参数/* Na
3、me : UART1_Configuration* Deion : Configures the uart1 * Input : None* Output : None* Return : None*/void USART_Configuration(void)USART_InitTypeDef USART_InitStructure;USART_ClockInitTypeDef USART_ClockInitStructure;Uart1_GPIO_Configuration();USART_ClockInitStructure.USART_Clock = USART_Clock_Disab
4、le;USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;/* Configure the USART1 synchronous paramters */USART_ClockInit(USART1, USART_InitStructure.USART_BaudRate = 9600;USART_InitSt
5、ructure.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;/* Co
6、nfigure USART1 basic and asynchronous paramters */USART_Init(USART1, /* Enable USART1 Receive interrupts */USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);/* Enable USART1 */USART_Cmd(USART1, ENABLE);然后是在中断设置,需要修改 stm32f10x_it.c 中的串口中断函数 并且需要修改 void NVIC_Configuration(void)函数修改 NVIC_Configuration 函数/*
7、 Name : NVIC_Configuration* Deion : Configures NVIC and Vector Table base location.* Input : None* Output : None* Return : None*/void NVIC_Configuration(void)NVIC_InitTypeDef NVIC_InitStructure;#ifdef VECT_TAB_RAM/* Set the Vector Table base location at 0x20000000 */NVIC_SetVectorTable(NVIC_VectTab_
8、RAM, 0x0);#else /* VECT_TAB_FLASH */* Set the Vector Table base location at 0x08000000 */NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);#endif/* Configure the NVIC Preemption Priority Bits */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);/* Enable the USART1 Interrupt */NVIC_InitStructure.NVIC_IRQCha
9、nnel = USART1_IRQChannel;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(/串口中断void USART1_IRQHandler(void)/处理接收到的数据if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)/* Clear the USART1 Receive interrupt */USART_ClearITPendingBit(USART1, U
10、SART_IT_RXNE);/发送中断if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET)USART_SendData(USART1, Send_DataSend_Length+);if (Send_Length=SEND_LENGTH)/发送字节结束USART_ClearITPendingBit(USART1,USART_IT_TXE);USART_ITConfig(USART1, USART_IT_TXE, DISABLE);USART_ITConfig(USART1, USART_IT_TC, ENABLE);/发送完成if (USA
11、RT_GetITStatus(USART1, USART_IT_TC) != RESET)USART_ClearITPendingBit(USART1,USART_IT_TC);USART_ITConfig(USART1, USART_IT_TC, DISABLE);在需要发送的程序里 Send_DataSEND_LENGTH和发送长度设置好 ,void Send_to_PC(void)/设置好 Send_DataSEND_LENGTH数组/打开发送中断USART_ITConfig(USART1, USART_IT_TXE, ENABLE); 至此 串口就可以工作起来了发送中断应该说有两个一个是发送完中断,一个是缓冲区空中断,你说的问题应该是发生在缓冲区空中断中.当发送一帧数据时,如果发生缓冲区空中断或发送完毕中断时,还有数据要继续发送,那就发送下一个数据,如果数据已经都发送完了, 则处理方式上有点区别,如果用缓冲区空中断, 则而将中断关闭,如果用发送完毕中断, 则直接返回就可以了.