使用的MDK-KEIL5 用的标准库.
相关代码
void Usart2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* config USART1 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* Enable Syscfg */
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* USART1 Pins configuration ************************************************/
/* Connect pin to Periph 设置为复用功能 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
/* Configure pins as AF pushpull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART1 IRQ Channel configuration */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* USART1 mode config */
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_Init(USART2,&USART_InitStructure);
//USART_ClearFlag(USART1,USART_FLAG_RXNE);
/* Enable USARTs Receive interrupts */
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
// USART_ITConfig(USART1, USART_IT_ORE, ENABLE); //溢出中断
/* Enable the USARTs */
USART_Cmd(USART2,ENABLE);
}
/* 中断程序 */
void USART2_IRQHandler(void){
if (USART_GetFlagStatus(USART2, USART_FLAG_ORE) != RESET)
{
USART_ReceiveData(USART2);
USART_ClearITPendingBit(USART2,USART_IT_ORE);
}
//接收中断处理程序就做了RXNE和ORE的清除操作,。
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //读_SR
// if(USART_GetFlagStatus(USART1, USART_IT_RXNE) != RESET) //不好用.
{
SilenceTime2 = 0; //静默时间清0
/* Read one byte from the receive data register */
Rx2Buffer[Rx2Counter++] = USART_ReceiveData(USART2); //读_DR
USART_ClearITPendingBit(USART2,USART_IT_RXNE); //必须要清除
}
}
硬件仿真时 使用串口调试工具发送数据.,但是一直进不了中断程序. 不知道是哪里错了.请大神们帮忙看看. |