新手入门
积分 16
金钱 16
注册时间 2017-3-14
在线时间 3 小时
10 金钱
本帖最后由 huihu 于 2018-1-25 14:50 编辑
这几天在调stm32l151c8a发现一个奇怪的现象,发现初始化RTC后,板子上电自启动程序运行到一段就卡死,只要接上调试器程序运行就正常。求大神帮忙解惑。另:有一份代码使用HAL库写的能够跑通板子应该没有问题,我现在用标准库写的。
[mw_shl_code=c,true]这个是板子初始化代码
void BoradInit()
{
GPIO_InitTypeDef Gpio;
RCC_Configuration();
NVIC_Configuration();
SysTick_Configuration();
Gpio.GPIO_Mode = GPIO_Mode_OUT;
Gpio.GPIO_OType = GPIO_OType_PP;
Gpio.GPIO_Pin = PWR_PULLUP_CTRL_Pin;
Gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;
Gpio.GPIO_Speed = GPIO_Speed_400KHz;
GPIO_Init(PWR_PULLUP_CTRL_GPIO_Port, &Gpio);
GPIO_WriteBit(PWR_PULLUP_CTRL_GPIO_Port, Gpio.GPIO_Pin, Bit_SET);
Gpio.GPIO_Pin = PWR_CTRL_Pin;
GPIO_Init(PWR_CTRL_GPIO_Port, &Gpio);
GPIO_WriteBit(PWR_CTRL_GPIO_Port, Gpio.GPIO_Pin, Bit_SET); //disable Vsensor
// Gpio.GPIO_Mode = GPIO_Mode_IN;
// Gpio.GPIO_Pin = USER_KEY_Pin;
// GPIO_Init(USER_KEY_GPIO_Port, &Gpio);
DebugUartInit();
LedInit();
RTC_Config();
//LSIRTC_Config();
RTC_AlarmConfig();
}[/mw_shl_code][mw_shl_code=c,true]RTC初始化代码
static void RTC_Config(void)
{
#if 1
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR , ENABLE);
/* Allow access to RTC */
PWR_RTCAccessCmd(ENABLE);
/* LSE used as RTC source clock */
/* The RTC Clock may varies due to LSE frequency dispersion. */
/* Enable the LSE OSC */
RCC_LSEConfig(RCC_LSE_ON);
//RCC_LSEConfig(RCC_LSE_Bypass);
/* Wait till LSE is ready */
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{
}
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
/* Enable the RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC APB registers synchronisation */
if (ERROR == RTC_WaitForSynchro())
{
printf("Wait for RTC APB registers synchronisation Failed\r\n");
}
#endif
/* Calendar Configuration */
RTC_InitStructure.RTC_AsynchPrediv = 127;//0x7F;
RTC_InitStructure.RTC_SynchPrediv = 255; //0x120; /* (37KHz / 128) - 1 = 0x120*/
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
if(RTC_Init(&RTC_InitStructure) == ERROR)
{
printf("Rtc_Init failed\r\n");
}
RTC_CalibOutputCmd(DISABLE);
#if 0
/* EXTI configuration *******************************************************/
EXTI_ClearITPendingBit(EXTI_Line20);
EXTI_InitStructure.EXTI_Line = EXTI_Line20;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable the RTC Wakeup Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Configure the RTC WakeUp Clock source: CK_SPRE (1Hz) */
RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_16bits);
RTC_SetWakeUpCounter(0x0);
/* Enable the RTC Wakeup Interrupt */
RTC_ITConfig(RTC_IT_WUT, ENABLE);
/* Enable Wakeup Counter */
RTC_WakeUpCmd(ENABLE);
#endif
}[/mw_shl_code]
我来回答