3195 views|2 replies
- Last login
- 2024-11-21
- Online Time
- 3622 hours
- Prestige
- 1919 points
- Points
- 1407 points
|
STM32L151C8T6 cannot enter serial port interrupt
[Copy link]
我写了一个STM32L151C8T6串口中断的程序,如下:
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //使能USART1
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //GPIOA时钟
delay_init(32);
//USART1_TX PA9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART1_RX PA10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_DeInit(USART1); //复位串口1
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
USART_InitStructure.USART_BaudRate = 38400;
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(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
/* Enable the usart1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
//串口1接收中断
void USART1_IRQHandler(void) //串口1中断服务程序
{
// if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET) //是否接收中断发生,使用库函数
if ((USART1->CR1 & (1 << 5)) && (USART1->SR & (1 << 5))) //是否接收中断发生,使用寄存器
{
Receive_Buffer[Receive_Cnt++] = USART_ReceiveData(USART1); //读取串口接收数据
if(Receive_Cnt >= 600) Receive_Cnt = 0; //判断缓冲区是否已满
ReceiveTime = 2;
}
}
发现进不了中断,各位大神看看,哪里有问题?谢谢!
|
|