打算测试一下stm32的串口控制用法,仿真发现stm32的程序无法进入接收中断,时而会进入void HardFault_Handler(void),难道是中断向量表不对?...
网上找的程序,也搜了一下网上的解答,还是没解决:
ide平台:iar 5.3;
芯片:stm32f103rct6
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f10x_xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f10x.c file
*/
RCC_cfg();
GPIO_cfg();
NVIC_cfg();
USART_cfg();
/* Output a message on Hyperterminal using USART_senddata() function */
unsigned char Txmes[100]="hello,welcome to flyarm.cn";
for(int i = 0; Txmes!='\0'; ++i)
{
USART_SendData(USART1,Txmes);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
}
/* Output a message on Hyperterminal using printf function */
while( 1 )
{
}
}
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
return ch;
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
//RCC时钟配置
void RCC_cfg()
{
ErrorStatus HSEStartUpStatus;//定义错误状态变量
RCC_DeInit();//将RCC寄存器重新设置为默认值
RCC_HSEConfig(RCC_HSE_ON);//打开外部高速时钟晶振
HSEStartUpStatus = RCC_WaitForHSEStartUp();//等待外部高速时钟晶振工作
if(HSEStartUpStatus == SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置AHB时钟(HCLK)为系统时钟
RCC_PCLK2Config(RCC_HCLK_Div1);//设置高速AHB时钟(APB2)为HCLK时钟
RCC_PCLK1Config(RCC_HCLK_Div2);//设置低速AHB时钟(APB1)为HCLK的2分频
FLASH_SetLatency(FLASH_Latency_2);//设置FLASH代码延时
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//使能预取指缓存
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_6);//设置PLL时钟,为HSE的9倍频 8MHz * 9 = 72MHz
RCC_PLLCmd(ENABLE);//使能PLL
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);//等待PLL准备就绪
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//设置PLL为系统时钟源
while(RCC_GetSYSCLKSource() != 0x08);//判断PLL是否是系统时钟
}
//打开GPIO时钟,复用功能,串口1的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1, ENABLE);
}
//IO口配置
void GPIO_cfg()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//PA9作为US1的TX端,打开复用,负责发送数据
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10作为US1的RX端,负责接收数据
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;//LED显示串口正在发送/接收数据
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
//串口初始化
void USART_cfg()
{
USART_InitTypeDef USART_InitStructure;
USART_StructInit(&USART_InitStructure); //将结构体设置为缺省状态
USART_Init(USART1, &USART_InitStructure); //设置串口1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//打开串口1的收中断响应函数
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);//先关闭串口1的发中断响应函数
USART_Cmd(USART1, ENABLE);//打开串口1
USART_ClearITPendingBit(USART1, USART_IT_TC);//清除中断TC位
}
//配置中断
void NVIC_cfg()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //选择中断分组2
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //选择串口1中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占式中断优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应式中断优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断
NVIC_Init(&NVIC_InitStructure);
}
/**
* @}
*/
/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
中断程序如下:
void HardFault_Handler(void)
{
/* Go to infinite loop when Hard Fault exception occurs */
while (1)
{
printf("\n\hard error\n\r"); //测试用
}
}
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
/* received data */
USART_GetInputString();
}
/* If overrun condition occurs, clear the ORE flag
and recover communication */
if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET)
{
(void)USART_ReceiveData(USART1);
}
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
/* Write one byte to the transmit data register */
USART_SendBufferData();
}
/* 等待串口接收数据完成或缓存区溢出 */
if (USART_Rx_Done == 1)
{
printf("\n\r欢迎使用skater-STM32 You input String is:\n\r");
printf("[%s]\n\r",USART_Rx_Buffer);
USART_Rx_Buffer_Clear();
}
GPIO_ResetBits(GPIOD, GPIO_Pin_2); //设置led灯亮
for (int jack=0;jack<10;++jack)
{
printf("\n\rwelcome to Skater-STM;led on\n\r");
}
}
用户377235 2014-5-15 15:36
用户447644 2013-8-25 12:34
用户610474 2013-3-23 16:04
用户417323 2012-7-25 20:29
用户377235 2012-4-11 22:02
能否告知你怎么解决的,细节!
用户390025 2011-10-21 19:35
用户390025 2011-10-21 19:26