用串口模擬仿真,通過串口接收到一幀數據后,一直沒有IDLE中斷。折騰好長時間也沒發現問題在哪。
u8 rxbuffer[100]={0x00};
u8 txbuffer[100]={0x00};
u8 flag=0;
static int rx_count=0;
void usart_gpio_init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO , ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
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);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void usart_init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
USART_InitTypeDef USART_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_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //使能接收中斷
USART_ITConfig(USART1,USART_IT_IDLE ,ENABLE); //使能空閑總線中斷
USART_ClearFlag(USART1,USART_FLAG_TC); //清除發送完成標志位
}
void usart_nvic_init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void usart_config(void)
{
usart_gpio_init();
usart_init();
usart_nvic_init();
}
//串口接收中斷
void USART1_IRQHandler(void) //串口1中斷服務程序
{
if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE )!=RESET)//接收到一字節數據
{
rxbuffer[rx_count++]=USART_ReceiveData(USART1);
USART_ClearFlag(USART1,USART_IT_RXNE);
if(USART_GetFlagStatus(USART1, USART_FLAG_IDLE)!= RESET) //接收到一幀數據
{
USART1->SR;
USART1->DR;
flag=1;
USART_ClearFlag(USART1, USART_FLAG_IDLE);
usart1_send_onebyte(0x26);
}
//usart1_send_onebyte(0x27);
}
}
|