欢迎来到三一办公! | 帮助中心 三一办公31ppt.com(应用文档模板下载平台)
三一办公
全部分类
  • 办公文档>
  • PPT模板>
  • 建筑/施工/环境>
  • 毕业设计>
  • 工程图纸>
  • 教育教学>
  • 素材源码>
  • 生活休闲>
  • 临时分类>
  • ImageVerifierCode 换一换
    首页 三一办公 > 资源分类 > DOC文档下载  

    STM32独立看门狗和低功耗模式RTC定时唤醒来喂狗.doc

    • 资源ID:4854386       资源大小:66.50KB        全文页数:15页
    • 资源格式: DOC        下载积分:10金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要10金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    STM32独立看门狗和低功耗模式RTC定时唤醒来喂狗.doc

    STM32独立看门狗和低功耗模式_RTC定时唤醒来喂狗在STM32开发中经常会用到独立看门狗(IWDG)和低功耗模式,看门狗是为了检测和解决由软件错误引起的故障,低功耗模式是为了在CPU不需要继续运行时进入到休眠模式用以节省电能。其中独立看门狗的时钟由独立的RC振荡器(STM32F10x一般为40kHz)提供,即使在主时钟出现故障时,也仍然有效,因此可以在停止和待机模式下工作。而且独立看门狗一旦启动,除了系统复位,它不能再被停止。但这样引发的一个问题是当MCU进入到低功耗模式后由于CPU停止运行无法喂狗,会导致系统频繁复位。那如何解决这个问题呢,难道独立看门狗和低功耗模式没法同时使用?一个很好的方式是在休眠模式下通过RTC定时唤醒来喂狗,喂完够在进入继续进入到休眠模式。比如看门狗复位的时间间隔为10s。那么在进入休眠模式前设置RTC闹钟中断时间为5s。这样每隔5s唤醒一次喂一次狗。便可以很好的解决这个问题。while(1) / 执行任务 Task1(); Task2(); / . / 喂狗 dev_iwdg_feed(); / 进入待机模式开关 if(m_bEnterStandByMode) / 使能外部中断,GPIOB3,用以MCU从待机模式唤醒 dev_exTI_enable(TRUE); ENTERSTOPMODE: / 设置RTC闹钟,5秒钟产生一次RTC闹钟中断*/ dev_rtc_setAlarm(5); / 进入停止模式(低功耗),直至外部中断触发时被唤醒 PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI); / 是否是RTC闹钟中断唤醒 if(dev_rtc_isAlarm() / 喂狗 dev_iwdg_feed(); / 喂完狗继续进入停止模式 goto ENTERSTOPMODE; / 禁止外部中断 dev_exTI_enable(FALSE); / 从停止模式唤醒后恢复系统时钟 dev_clk_restore(); 以下是完整的参考代码:/* / STM32F10x StopMode RTC Feed Dog / compiler: Keil UV3 / 2013-01-04 , By friehood /* #include stm32f10x_lib.h #include platform_config.h staTIc Boolean g_bRTCAlarm = FALSE; /* * FuncTIon Name : RCC_Configuration * Description : Configures the different system clocks. * Input : None * Output : None * Return : None */ void RCC_Configuration(void) /* RCC system reset(for debug purpose) */ RCC_DeInit(); /* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON); /* Wait till HSE is ready */ if(RCC_WaitForHSEStartUp() = SUCCESS) /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /FLASH时序控制 /推荐值:SYSCLK = 024MHz Latency=0 / SYSCLK = 2448MHz Latency=1 / SYSCLK = 4872MHz Latency=2 /FLASH_SetLatency(FLASH_Latency_1); /警告:修改为1会对DMA值有影响(如ADC采集值会错位) FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* PLLCLK = 12MHz * 3 = 36 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_3); /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) = RESET) /* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) /* Enable PWR and BKP clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Enable AFIO clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); /* Function Name : NVIC_Configuration* Description : Configures the nested vectored interrupt controller.* 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 /* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* Function Name : SysTick_Configuration* Description : Configures the SysTick to generate an interrupt each 1 millisecond.* Input : None* Output : None* Return : None*/ void SysTick_Configuration(void) /* Select AHB clock(HCLK) as SysTick clock source */ SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); /* Set SysTick Priority to 3 */ NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0); /* SysTick interrupt each 1ms with HCLK equal to 72MHz */ SysTick_SetReload(72000); /* Enable the SysTick Interrupt */ SysTick_ITConfig(ENABLE); /* Function Name : Delay* Description : Inserts a delay time.* Input : nTime: specifies the delay time length, in milliseconds.* Output : None* Return : None*/ void Delay(u32 nTime) /* Enable the SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Enable); TimingDelay = nTime; while(TimingDelay != 0); /* Disable the SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Disable); /* Clear the SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Clear); /* Function Name : RTC_Configuration* Description : Configures RTC clock source and prescaler.* Input : None* Output : Nonevoid RTC_Configuration(void)EXTI_InitTypeDef EXTI_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;/* RTC clock source configuration -*/* Allow access to BKP Domain */PWR_BackupAccessCmd(ENABLE);/* Reset Backup Domain */BKP_DeInit();/* Enable the LSI OSC */RCC_LSICmd(ENABLE);/* Wait till LSI is ready */while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) = RESET)/* Select the RTC Clock Source */RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);/* Enable the RTC Clock */RCC_RTCCLKCmd(ENABLE);/* RTC configuration -*/* Wait for RTC APB registers synchronisation */RTC_WaitForSynchro();/* Set RTC prescaler: set RTC period to 1sec */RTC_SetPrescaler(40000);/* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask();/* Enable the RTC Alarm interrupt */RTC_ITConfig(RTC_IT_ALR, ENABLE);/* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask();/* Configure EXTI Line17(RTC Alarm) to generate an interrupt on rising edge */EXTI_ClearITPendingBit(EXTI_Line17);EXTI_InitStructure.EXTI_Line = EXTI_Line17;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 Interrupt */NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQChannel;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(NVIC_InitStructure);/* Function Name : RTCAlarm_IRQHandler* Description : This function handles RTC Alarm interrupt request.* Input : None* Output : None* Return : None*/void RTCAlarm_IRQHandler(void)if(RTC_GetITStatus(RTC_IT_ALR) != RESET)/* Set the RTC alarm flag */g_bRTCAlarm = TRUE;/* Clear EXTI line17 pending bit */EXTI_ClearITPendingBit(EXTI_Line17);/* Check if the Wake-Up flag is set */if(PWR_GetFlagStatus(PWR_FLAG_WU) != RESET)/* Clear Wake Up flag */PWR_ClearFlag(PWR_FLAG_WU);/* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask();/* Clear RTC Alarm interrupt pending bit */RTC_ClearITPendingBit(RTC_IT_ALR);/* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask();/* Function Name : dev_rtc_setAlarm* Description : 设置RTC闹钟。* Input : 闹钟时间* Output : None* Return : None*/void dev_rtc_setAlarm(u32 AlarmValue)/* Clear the RTC SEC flag */RTC_ClearFlag(RTC_FLAG_SEC);/* Wait clear RTC flag sccess */while(RTC_GetFlagStatus(RTC_FLAG_SEC) = RESET);/* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask();/* Sets the RTC alarm value */RTC_SetAlarm(RTC_GetCounter() + AlarmValue);/* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask();/* Function Name : dev_rtc_isAlarm* Description : RTC闹钟是否触发* Input : None* Output : None* Return : TRUE:已触发,FALSE,未触发*/Boolean dev_rtc_isAlarm(void)if(g_bRTCAlarm)/* Clear the RTC alarm flag */g_bRTCAlarm = FALSE;return TRUE;return FALSE;void dev_iwdg_init(void)/* Enable write access to IWDG_PR and IWDG_RLR registers */IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);/* IWDG counter clock: 40KHz(LSI) / 256 = 0.15625 KHz */IWDG_SetPrescaler(IWDG_Prescaler_256);/* Set counter reload value to 1562 */IWDG_SetReload(1562); / 10s/* Reload IWDG counter */IWDG_ReloadCounter();/* Enable IWDG (the LSI oscillator will be enabled by hardware) */IWDG_Enable();void dev_iwdg_feed(void)IWDG_ReloadCounter();/* Function Name : dev_clk_restore* Description : Restore system clock after wake-up from STOP: enable HSE, PLL* and select PLL as system clock source.* Input : None* Output : None* Return : None*/void dev_clk_restore(void)/* Enable HSE */RCC_HSEConfig(RCC_HSE_ON);/* Wait till HSE is ready */HSEStartUpStatus = RCC_WaitForHSEStartUp();if(HSEStartUpStatus = SUCCESS)/* Enable PLL */RCC_PLLCmd(ENABLE);/* Wait till PLL is ready */while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) = RESET)/* Select PLL as system clock source */RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);/* Wait till PLL is used as system clock source */while(RCC_GetSYSCLKSource() != 0x08)/* Function Name : EXTI_Configuration* Description : Configures EXTI Line3.* Input : None* Output : None* Return : None*/void EXIT_Configuration(void)EXTI_InitTypeDef EXTI_InitStructure;GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource3);EXTI_ClearITPendingBit(EXTI_Line3);EXTI_InitStructure.EXTI_Line = EXTI_Line3;EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;EXTI_InitStructure.EXTI_LineCmd = ENABLE;EXTI_Init(EXTI_InitStructure);void dev_exti_enable(Boolean bEnable)NVIC_InitTypeDef NVIC_InitStructure;/* Clear the Key Button EXTI line pending bit */EXTI_ClearITPendingBit(EXTI_Line3);NVIC_ClearIRQChannelPendingBit(EXTI3_IRQChannel);NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQChannel;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;NVIC_InitStructure.NVIC_IRQChannelCmd = bEnable ? ENABLE : DISABLE;NVIC_Init(NVIC_InitStructure);/* Function Name : main* Description : Main program.* Input : None* Output : None* Return : None*/int main(void)/* System Clocks Configuration */RCC_Configuration();/* NVIC configuration */NVIC_Configuration();/* Configure RTC clock source and prescaler */RTC_Configuration();/* Configure the SysTick to generate an interrupt each 1 millisecond */SysTick_Configuration();/* Configures EXTI Line3 */EXIT_Configuration();/* IWDG initialize*/dev_iwdg_init();while(1)/ 执行任务Task1();Task2();/ 。/ 喂狗dev_iwdg_feed();/ 进入待机模式开关if(m_bEnterStandByMode)/ 使能外部中断,GPIOB3,用以MCU从待机模式唤醒dev_exti_enable(TRUE);ENTERSTOPMODE:/ 设置RTC闹钟,5秒钟产生一次RTC闹钟中断*/dev_rtc_setAlarm(5);/ 进入停止模式(低功耗),直至外部中断触发时被唤醒PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);/ 是否是RTC闹钟中断唤醒if(dev_rtc_isAlarm()/ 喂狗dev_iwdg_feed();/ 喂完狗继续进入停止模式goto ENTERSTOPMODE;/ 禁止外部中断dev_exti_enable(FALSE);/ 从停止模式唤醒后恢复系统时钟dev_clk_restore();

    注意事项

    本文(STM32独立看门狗和低功耗模式RTC定时唤醒来喂狗.doc)为本站会员(sccc)主动上传,三一办公仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一办公(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-2

    经营许可证:宁B2-20210002

    宁公网安备 64010402000987号

    三一办公
    收起
    展开