stm32低功耗

/**
时间:2017年7月20日
cpu: STM32F042F6
目的:低功耗解决方案
方案:利用RTC闹钟事件将cpu从停止模式中唤醒,处理完事情后
      重新设置时间,(闹钟时间不变)。进入低功耗。
说明:对于新手,先将寄存器摸清楚是非常有必要的。
*/


/** 
* 重新设置时间
*/
void rtc_timeregulate(void)
{
	RTC_TimeTypeDef RTC_TimeStructure;
	RTC_TimeStructure.RTC_H12     = RTC_H12_AM;
    RTC_TimeStructure.RTC_Hours = 10;
	RTC_TimeStructure.RTC_Minutes = 0;
	RTC_TimeStructure.RTC_Seconds = 0;
    RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure);
}

/**
* RTC中断函数 
*/
void RTC_IRQHandler(void)
{
  if(RTC_GetITStatus(RTC_IT_ALRA) != RESET)
  {
    PWR_BackupAccessCmd(ENABLE);
    RTC_ClearITPendingBit(RTC_IT_ALRA);
    EXTI_ClearITPendingBit(EXTI_Line17);
    rtc_irqcallback();
    rtc_timeregulate();
	
	/* 进入低功耗模式 */
	PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFE);
  } 
}

/**
* RTC中断回调函数
*/
void rtc_irqcallback(void)
{
	run_all(); /* 系统运行 */
}

/**
* RTC初始化时要设置系统时间和闹钟 
*/
void RTC_TimeRegulate(void)
{ 
  RTC_TimeTypeDef RTC_TimeStructure;
  RTC_AlarmTypeDef  RTC_AlarmStructure;

  RTC_TimeStructure.RTC_H12     = RTC_H12_AM;
  RTC_TimeStructure.RTC_Hours = 10;
  RTC_TimeStructure.RTC_Minutes = 0;
  RTC_TimeStructure.RTC_Seconds = 0;
  RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure);

  /* Disable the Alarm A */
  RTC_AlarmCmd(RTC_Alarm_A, DISABLE);

  RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_H12_AM;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = 10;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = 0;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = 5;

  RTC_AlarmStructure.RTC_AlarmMask = ( RTC_AlarmMask_DateWeekDay|RTC_AlarmMask_Hours|RTC_AlarmMask_Minutes);
  RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure);
	
  /* Enable the RTC Alarm A Interrupt */
  RTC_ITConfig(RTC_IT_ALRA, ENABLE);
   
  /* Enable the alarm  A */
  RTC_AlarmCmd(RTC_Alarm_A, ENABLE);
}

/**
* RTC配置函数
*/
void RTC_CheckAndConfig(void)
{
    RTC_InitTypeDef RTC_InitStructure;
    EXTI_InitTypeDef  EXTI_InitStructure;

    RTC_Configuration();

    /* Configure the RTC data register and RTC prescaler */
    RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv;
    RTC_InitStructure.RTC_SynchPrediv = SynchPrediv;
    RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;

    /* Check on RTC init */
    if (RTC_Init(&RTC_InitStructure) == ERROR)
        {
            //printf("\n\r        /!\\***** RTC Prescaler Config failed ********/!\\ \n\r");
        }

    /* Configure the time register */
    RTC_TimeRegulate();

    /* RTC Alarm A Interrupt Configuration */
    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);

    RTC_NVIC_Config();
}

/**
* RTC的时钟配置,内部时钟
*/
void RTC_Configuration(void)
{

    /* Enable the PWR clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

    /* Allow access to RTC */
    PWR_BackupAccessCmd(ENABLE);

    /* The RTC Clock may varies due to LSI frequency dispersion. */
    /* 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);

    SynchPrediv = 0x18F;
    AsynchPrediv = 0x63;

    /* Enable the RTC Clock */
    RCC_RTCCLKCmd(ENABLE);

    /* Wait for RTC APB registers synchronisation */
    RTC_WaitForSynchro();
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值