STM32串口清中断后下次再接收数据会不会再次进入中断 我调试的是不能 必须复位才能接收新的数据 请问大家有知道怎么回事吗?
代码如下:
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
ErrorStatus HSEStartUpStatus;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : main
* Description : Main program
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
/* System Clocks Configuration */
RCC_Configuration();
/* NVIC configuration */
NVIC_Configuration();
/* Configure the GPIO ports */
GPIO_Configuration();
/* USART1 configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- Two Stop Bit
- Odd parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
- USART Clock disabled
- USART CPOL: Clock is active low
- USART CPHA: Data is captured on the second edge
- USART LastBit: The clock pulse of the last data bit is not output to
the SCLK pin
*/
GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);
/*AFIO->MAPR|=(uint32_t)0x04;*/ // clear USART1 remap
USART_InitStructure.USART_BaudRate = 9600;
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_ClockInitStructure.USART_Clock = USART_Clock_Disable; //禁用USART时钟 时钟低电平活动
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; //时钟极性,数据低电平有效
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; //外部时钟相位,数据在第二个时钟捕捉
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; //禁用最后一位,最后数据位的时钟脉冲不输出到SCLK引脚
/* Configure the USART1 */
USART_Init(USART1, &USART_InitStructure);
USART_ClockInit(USART1,&USART_ClockInitStructure);
/* Enable the USART Transmoit interrupt: this interrupt is generated when the
USART1 transmit data register is empty */
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
/* Enable the USART Receive interrupt: this interrupt is generated when the
USART1 receive data register is not empty */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Enable USART1 */
USART_Cmd(USART1, ENABLE);
while(1)
{
}
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose)将RCC寄存器从新设置为默认值 */
RCC_DeInit();
/* Enable HSE 打开外部高速时钟晶振*/
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready 等待外部高速时钟晶振工作*/
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* HCLK = SYSCLK 设置AHB时钟 设置高速总线时钟=系统时钟*/
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK 设置高速AHB时钟 设置低速总线2时钟=高速总线时钟*/
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 设置低速AHB时钟 设置低速总线1时钟=高速时钟的二分频*/
RCC_PCLK1Config(RCC_HCLK_Div2);
/* Flash 2 wait state 令Flash处于等待状态,2是针对高频时钟的*/
FLASH_SetLatency(FLASH_Latency_2);
/* Enable Prefetch Buffer 使能flash预读取缓冲区*/
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* PLLCLK = 8MHz * 9 = 72 MHz 利用锁相环将外部8MHz晶振9倍频到72MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL 使能锁相环 */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready 等待锁相环输出稳定*/
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source 将锁相环输出设置为系统时钟*/
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source 判断PLL是否是系统时钟*/
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
/* Enable GPIOA and USART1 clocks 使能外围接口总线时钟,开串口时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1| RCC_APB2Periph_AFIO, ENABLE);
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART1 Tx (PB6) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure USART1 Rx (PB7) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures the nested vectored interrupt controller.
* 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_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* Enable the USART1 Interrupt */
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);
}
中断处理函数是:void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
{
/* Read one byte from the receive data register */
RxBuffer[RxCounter++] = (USART_ReceiveData(USART1) & 0x7F);
/* Clear the USART1 Receive interrupt */
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
if(RxCounter == NbrOfDataToRead)
{
/* Disable the USART Receive interrupt */
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
TxCounter=0;
p = RxBuffer;
}
}
if(USART_GetITStatus(USART1, USART_IT_TXE) == SET)
{
/* Write one byte to the transmit data register */
USART_SendData(USART1, p[TxCounter++]);
while(USART_GetFlagStatus(USART1, USART_IT_TXE)==RESET)
/* Clear the USART1 transmit interrupt */
USART_ClearITPendingBit(USART1, USART_IT_TC);
if(p==TxBuffer&&TxCounter == NbrOfDataToTransfer)
{
/* Disable the USART1 Transmit interrupt */
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}
if(p==RxBuffer&&TxCounter==RxCounter)
{
/* Disable the USART1 Transmit interrupt */
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}
}
if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)
{
USART_ClearFlag(USART1,USART_FLAG_ORE); //清溢出位
USART_ReceiveData(USART1); //读DR
}
}
已经清楚中断标志位了,为什么发送新的数据时不能覆盖掉以前的数据?
此帖出自
小平头技术问答
关键时刻,必须淡定和坚持
一周热门 更多>