STM32 USART1进不了中断
RVMDK环境,开发板上只引出了USART1这一个串口。通过查询方式和PC通讯,没有问题,接收到PC发出的字节并发送回PC。现在想通过中断方式通讯,设置断点,发现始终未进入中断服务程序。程序很简单,一些初始化设置而已,网上搜了不少例程,找不出问题原因。请教各位达人。
代码如下:
----------------------------------------------
/*-------------- main.c --------------*/
int main(void)
{
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
USART_Configuration();
while(1)
{
}
}
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
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 GPIO_Configuration(void)
{
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_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void USART_Configuration(void)
{
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_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
USART_Cmd(USART1,ENABLE);
}
/*-------- stm32f10x_it.c ---------*/
void USART1_IRQHandler(void)
{
u16 c;
if (SET == USART_GetFlagStatus(USART1, USART_FLAG_RXNE))
{
c = USART_ReceiveData(USART1);
USART_SendData(USART1, c);
while (RESET == USART_GetFlagStatus(USART1, USART_FLAG_TC));
}
}
---------------------------------------------------
如果不配置NVIC,而在main()的while(1)中添加
---------------------------------------------------
if (SET == USART_GetITStatus(USART1, USART_IT_RXNE))
{
c = USART_ReceiveData(USART1);
{
USART_SendData(USART1, c);
while (RESET == USART_GetFlagStatus(USART1, USART_FLAG_TC));
}
}
-----------------------------------------------------
可以正常工作。说明产生中断的条件已经具备。可就是进不了ISR。不解中。
求教,并感谢看完这么多代码。
[解决办法]
你配置了NVIC,但是你没有配置NVIC的优先级位数(这个非常重要)!
是这条语句 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
加在void NVIC_Configuration(void)这个函数最开始那里
void NVIC_Configuration(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitTypeDef NVIC_InitStructure;
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);
[解决办法]
代码没看出什么问题。你是在SRAM里调试的吧?在flash下调试看看。