本帖最后由 电子write_cai 于 2013-8-5 15:12 编辑
在串口应用中,我设置了发送中断使能,并指定了串口中断向量. 可是我并没有发送数据,怎么就进入中断去了.
如代码:
1:串口的初始化 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
RCC_APB1PeriphClockCmd (RCC_APB1Periph_USART2, ENABLE);
/* Configure USART1 Rx (PD6) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Configure USART1 Tx (PD5) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &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_Parity_Odd
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE); //ʹÄÜ·¢ËÍÖжÏ
//USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
2. 设置串口中断向量void SetupNVIC(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
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
3.中断向量函数void USART2_IRQHandler(void)
{
unsigned int Receive_data;
printf("\r\nUSART Interrupt is occur\r\n");
if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET) //ÅжϷ¢Éú½ÓÊÕÖжÏ
{
printf("\r\nWaiting receive data\r\n");
Receive_data = (USART_ReceiveData(USART2) & 0x7f);
USART_ClearITPendingBit(USART2,USART_IT_RXNE); //Çå³ýÖжϱêÖ¾
while(USART_GetITStatus(USART2,USART_IT_RXNE) == RESET)
{
//µÈ´ýÊý¾Ý½ÓÊÕ½áÊø;
}
USART_SendData(USART2,Receive_data);
}
//USART_ClearITPendingBit(USART2,USART_IT_TXE); //Çå³ýÖжϱêÖ¾
}
|