0

I'm using stm32f103RBT6 and I want to set RTC alarm event every one hour by using codes below

RTC_Alarm_Time.Alarm = 1;
HAL_RTC_GetTime(&hrtc,&RTC_Time,RTC_FORMAT_BIN);
RTC_Alarm_Time.AlarmTime.Hours=RTC_Time.Hours+1;
if(RTC_Alarm_Time.AlarmTime.Hours>23)
{                                           
    RTC_Alarm_Time.AlarmTime.Hours=0;   
}   
RTC_Alarm_Time.AlarmTime.Minutes=RTC_Time.Minutes;                       
RTC_Alarm_Time.AlarmTime.Seconds=RTC_Time.Seconds;  
HAL_RTC_SetAlarm_IT(&hrtc, &RTC_Alarm_Time, RTC_FORMAT_BIN);                

my problem is after hour 23 alarm comes at hour 1 and it skips hour 0. I think its because when i set alarm hour to 0 RTC date is still previous day. does anyone has any example of codes that i can make this Independent of date or any other way. Thankyou.

Jin Lee
  • 3,194
  • 12
  • 46
  • 86
tariq
  • 13
  • 3

1 Answers1

0

Updated Answer:

There is a bug in your code. Your code sets RTC_Alarm_Time.AlarmTime.Hours=RTC_Time.Hours+1, and then checks for the hour rollover with if(RTC_Time.Hours>23). Note that RTC_Time.Hours was not incremented, rather RTC_Alarm_Time.AlarmTime.Hours was incremented. So when RTC_Time.Hours == 23, RTC_Alarm_Time.AlarmTime.Hours = 24, and RTC_Alarm_Time.AlarmTime.Hours will not be rolled over to 0 because RTC_Time.Hours is not greater than 23. Then the call to HAL_RTC_SetAlarm_IT() will fail because Hours = 24. You would have caught this if you were checking the return value of HAL_RTC_SetAlarm_IT().

You can fix your code by changing the conditional like this.

if(RTC_Alarm_Time.AlarmTime.Hours>23)

Original Answer (would address the suspected problem on a STM32F4):

I think you're right about it skipping hour 0 because the date is the previous day. You should ignore the date/day because you want an hourly alarm. And I think you can ignore the date/day by setting RTC_Alarm_Time.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY. This should mask (i.e., ignore) the date/day so that the alarm will occur based upon only the hour, minute, and second.

kkrambo
  • 6,643
  • 1
  • 17
  • 30
  • I tried it but gives an error that AlarmMask is not a member of RTC_AlarmTypeDef.I'm using "stm32f1xx_hal.h" library that includes stm32f1xx_hal_rtc.c lib.does this library has other commands to ignore date?or can i just set rtc alarm by a counter? – tariq Mar 03 '21 at 11:29
  • Ah, sorry the STM32F1 does not support the `AlarmMask`. I was looking at STM32F4 documentation. But it looks like the STM32F1 RTC hardware does not know the date/day and the date is always ignored for the alarm. (The STM32F1 RTC is a simple counter and the HAL implementation limits alarm detection to 1 day ). The conversion from date/day to count happens in the STM32F1 HAL, in `HAL_RTC_SetAlarm()`. – kkrambo Mar 03 '21 at 15:02
  • @tariq I have updated the answer with the actual solution. The problem is not what we suspected. – kkrambo Mar 03 '21 at 15:16
  • @ kkrambo sorry i made a mistake type code in my question here. my code was if(RTC_Alarm_Time.AlarmTime.Hours>23) in my main program and i still have this problem.The problem does not come from anywhere else?in my main code i want to wakeup from stop mode every one hour can i just set alarm by a counter?or any other way?Thankyou. – tariq Mar 03 '21 at 16:15
  • @tariq I don't see anything else wrong. Check the return value of `HAL_RTC_SetAlarm_IT()`. Then step through the code in the debugger to see where is misbehaves. Step into `HAL_RTC_SetAlarm_IT()` and watch how it handles the case where the alarm hour is less than the current hour. – kkrambo Mar 03 '21 at 17:30