现象:使用查询模式能够实现正常收发,使用串口中断时,没有中断时,在主程序循环中正常运行,如果收到字符,不是跳到USART1_IRQHandler,而是跳到了 HardFaultException中断,可能是哪里的原因,如果说硬件有问题,可查询模式很正常。请高手给指点一下,问题在哪,估计很低级,因为在下是新手。
相关的程序如下:(也是从21ic上下的一个成功的,几乎没有修改)
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(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
// Enable the USART1 gloabal Interrupt
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/* USART1 configuration ------------------------------------------------------*/
/* USART1 configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
void USART1_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable USART1 clocks */
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 19200;
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;
/* Configure the USART1 */
USART_Init(USART1, &USART_InitStructure);
/* Enable the USART Receive interrupt: this interrupt is generated when the
USART1 receive data register is not empty */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Enable USART1 */
USART_Cmd(USART1, ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TXE);
}
/*-------------------------------------------------------------------*/
int main(void)
{
u8 i;
#ifdef DEBUG
debug();
#endif
/* Configure the system clocks */
RCC_Configuration();
/* NVIC Configuration */
NVIC_Configuration();
/* Configure the GPIOs */
GPIO_Configuration();
/* Configure the USART1 */
USART1_Configuration();
/* Configure the SPI2 */
//SPI_Configuration();
while(1)
{
//串口和GPIO测试
/*
if(USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET)
{
i = USART_ReceiveData(USART1);
USART_SendData(USART1, i+1);
//Spk_on(ENABLE);
Delay(10000);
//Spk_on(DISABLE);
}
i++;
}
}
/*******************************************************************************
* Function Name : HardFaultException
* Description : This function handles Hard Fault exception.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void HardFaultException(void)
{
/* Go to infinite loop when Hard Fault exception occurs */
while (1)
{
}
}
/*******************************************************************************
* Function Name : USART1_IRQHandler
* Description : This function handles USART1 global interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART1_IRQHandler(void)
{
u8 i;
/*
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
{
i = USART_ReceiveData(USART1);
USART_SendData(USART1, i+1);
}
*/
if(USART_GetITStatus(USART1,USART_IT_RXNE)!= RESET)
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
//接收数据在以下地点加代码
i = USART_ReceiveData(USART1);
USART_SendData(USART1, i+1);
}
}
除了串口的中断外,其它中断能用吗?看现象像是中断向量没找到,有可能是楼主的程序把中断向量表映射错了。在flash里运行的程序,中断向量也要映射到flash。函数NVIC_Configuration有这么一段:
#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
楼主可以搜索一下你的工程里是否定义了VECT_TAB_RAM ,如果定义了,这段程序就会把中断向量映射到RAM, 而RAM里并没有实际的中断向量,发生中断之后就会fault了。
注意,不同型号的MCU中断向量地址不同,详细的请参考相应的数据手册。
别人的现成的程序是根据他们的mcu指定的。
切记!
我的能中断,但是程序一开始就进入中断,但是不知道是哪个中断,因为程序运行不了串口中断函数以外的代码!我没有仿真器,郁闷中!