【无标题】STM32低功耗RTC的定时

为了实现STM32L431下低功耗唤醒功能。采用RTC的anlarmA进行定时唤醒。定时的时间可做的很长。

RTC的低功耗模式。

采用RTC加闹钟定时唤醒的方式。

1.计时周期可以很长,用定时器,16位或32位定时器,定时时间太短。

2.低功耗模式可以运行,省电,切可以唤醒处理器。

RTC有个唤醒定时器。按照1HZ的频率,再通过软件实现更长周期计数,麻烦。所以用闹钟,可以定很长的时间1秒到99年。

闹钟的关键参数解释。

typedef struct

{

RTC_TimeTypeDef AlarmTime; /*!< Specifies the RTC Alarm Time members */

哪一个闹钟,RTC_ALARM_A,或B

uint32_t AlarmMask; /*!< Specifies the RTC Alarm Masks.

This parameter can be a value of @ref RTC_AlarmMask_Definitions */

匹配方式:匹配时分秒和日期,日期为星期或者月的第几天,

uint32_t AlarmSubSecondMask; /*!< Specifies the RTC Alarm SubSeconds Masks.

This parameter can be a value of @ref RTC_Alarm_Sub_Seconds_Masks_Definitions */

subsecond不 匹配

uint32_t AlarmDateWeekDaySel; /*!< Specifies the RTC Alarm is on Date or WeekDay.

This parameter can be a value of @ref RTC_AlarmDateWeekDay_Definitions */

日期匹配是按星期几去匹配,还是按照日去匹配。

uint8_t AlarmDateWeekDay; /*!< Specifies the RTC Alarm Date/WeekDay.

If the Alarm Date is selected, this parameter must be set to a value in the 1-31 range.

If the Alarm WeekDay is selected, this parameter can be a value of @ref RTC_WeekDay_Definitions */

如果按照星期匹配这里的值就是星期几,1-7

如果按照日期匹配,这里的值就是1-31

uint32_t Alarm; /*!< Specifies the alarm

This parameter can be a value of @ref RTC_Alarms_Definitions */

} RTC_AlarmTypeDef;


int rtc_get_reg(struct tm *t)
{
    RTC_DateTypeDef sdatestructureget;
    RTC_TimeTypeDef stimestructureget;

    /* Get the RTC current Time */
    HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
    /* Get the RTC current Date */
    HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);

    t->tm_sec = stimestructureget.Seconds;
    t->tm_min = stimestructureget.Minutes;
    t->tm_hour = stimestructureget.Hours;

    t->tm_mday = sdatestructureget.Date;
    t->tm_mon  = sdatestructureget.Month - 1;

    if(s_century)
    {
        t->tm_year = sdatestructureget.Year  + 100;//w
    }
    else
    {
        t->tm_year = sdatestructureget.Year;
    }

    t->tm_yday = 0;
    t->tm_isdst = 0;

    return 0;
}

int rtc_set_reg(struct tm *t)
{
    uint8_t year = 0;
    RTC_TimeTypeDef sTime = {0};
    RTC_DateTypeDef sDate = {0};

    sTime.Hours   = BIN2BCD(t->tm_hour);
    sTime.Minutes = BIN2BCD(t->tm_min);
    sTime.Seconds = BIN2BCD(t->tm_sec);
    sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
    sTime.StoreOperation = RTC_STOREOPERATION_RESET;
    if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
    {
        Error_Handler();
    }

    sDate.Month = BIN2BCD(t->tm_mon + 1);
    sDate.Date  = BIN2BCD(t->tm_mday);

    if (t->tm_year >= 100)
    {
        year = BIN2BCD(t->tm_year - 100);
        s_century = 1;
        HAL_RTCEx_BKUPWrite(&hrtc,0,s_century);//在设置时间时,存储年是否超过1个世纪的标志
    }
    else
    {
        s_century = 0;
        year = BIN2BCD(t->tm_year);
    }

    sDate.Year  = year;

    if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
    {
        Error_Handler();
        return -1;
    }

    return 0;
}

time_t rtc_get_time(void)
{
    struct tm t;
    time_t ret = 0;

    memset(&t, 0, sizeof(t));

    rtc_get_reg(&t);//读取当前寄存器,年为100+N,月-1,小时为0时区

    ret = mktime(&t);//0时区的小时转为当前时�?

    return ret;
}


/******************************************************************************

******************************************************************************/
int rtc_set_time(time_t t)
{
    struct tm *ts = NULL;

    ts  = localtime(&t);//秒数转为年月日时分秒结构
    rtc_set_reg(ts);//写到CPU

    return 0;
}

/*
闹钟的实际是相对于当前时间,在未来经过多少秒后触发如果设置的闹钟时间是过去时间,
则闹钟在个月内不再触发,只到下个月的日和时分秒再次匹�?
*/
int rtc_set_alarm_reg(struct tm *t)
{

    RTC_AlarmTypeDef sAlarm = {0};

    sAlarm.AlarmTime.Hours   = BIN2BCD(t->tm_hour);
    sAlarm.AlarmTime.Minutes = BIN2BCD(t->tm_min);
    sAlarm.AlarmTime.Seconds = BIN2BCD(t->tm_sec);
    sAlarm.AlarmTime.SubSeconds = 0x0;
    sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;//夏令时,加减1小时,或无效
    sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
    sAlarm.AlarmMask = RTC_ALARMMASK_NONE;//按时分秒和日期匹(日期是星期几或每月第几天)
    sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;//subsecond不匹
    sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;//按星期或者按日期匹配
    sAlarm.AlarmDateWeekDay = BIN2BCD(t->tm_mday);//日期就是1-31,星期就1-7
    sAlarm.Alarm = RTC_ALARM_A;
    if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK)
    {
        Error_Handler();
    }

    return 0;
}


int rtc_set_alarm(time_t after_alarm_secnod)
{
    struct tm *ts = NULL;
    time_t   now_time;

    if(after_alarm_secnod==0)//闹钟时间不能和当前时间相
    {
        after_alarm_secnod = 1;
    }

    now_time = rtc_get_time();
    now_time += after_alarm_secnod;

    ts = localtime(&now_time);

    rtc_set_alarm_reg(ts);
    rtc_alarm_irq_enable();
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值