实现方法:
1、定时休眠唤醒code
HAL_Delay(2000);//进行采样的时间
HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 2, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);//调用库函数进入STOP模式
SystemClockConfig_STOP();//停机模式后唤醒,恢复时钟源
count1++;
while(count1==1)//循环的次数
{
Rx_Init();
count1=0;
flag3=0;
__HAL_RTC_ALARM_DISABLE_IT(&RTCHandle, RTC_IT_ALRA);
__HAL_RTC_ALARM_CLEAR_FLAG(&RTCHandle, RTC_IT_ALRA);
HAL_TIM_Base_Stop(&TimHandle); //定时器停止
HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);//调用库函数进入STOP模式
}
计算方法:例如休眠4s
/* RTC Wakeup Interrupt Generation:
Wakeup Time Base = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI))
Wakeup Time = Wakeup Time Base * WakeUpCounter
= (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI)) * WakeUpCounter
==> WakeUpCounter = Wakeup Time / Wakeup Time BaseTo configure the wake up timer to 4s the WakeUpCounter is set to 0x1FFF:
RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16
Wakeup Time Base = 16 /(~39.000KHz) = ~0,410 ms
Wakeup Time = ~4s = 0,410ms * WakeUpCounter
==> WakeUpCounter = ~4s/0,410ms = 9750 = 0x2616 */
HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 0x2616, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
局限性:用内部时钟LSI,大小固定导致Wakeup Time Base 被限制。 Wakeup Time(最大)=0.41ms*65535(0xffff)=26.869s,可能达不到休眠时间要求。
另一种方法:改HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 2, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
这里RTC_WAKEUPCLOCK_CK_SPRE_16BITS是设置CR寄存器中的低三位,唤醒时钟选择,这里选择1hz,2表示工作2S后进入唤醒中断,这个数随便改,改成多少就是休眠多少时间。
|