AnsweredAssumed Answered

RTC alarm incrementation for event between a period

Question asked by piet t on Feb 17, 2018

Hey everyone,

 

I have an alarm enabled and want my RTC to increment the timer to trigger an Alarm every ten minutes between 8 am and 5 pm. I've set the following code as I don't think there's a method provided by HAL to do it.

 

In the call back I'm just running if statements for incrementation between periods, once the period has elapsed, the hour is set to 8am for the next day. Is there any other way to do this, maybe a function provided by HAL or is this a satisfactory method?

 

void RTC_Alarm_IRQHandler(void)
{
  /* USER CODE BEGIN RTC_Alarm_IRQn 0 */

    //Remove write protection
      hrtc.Instance->WPR = 0xCA;
      hrtc.Instance->WPR = 0x53;


      //Clear alarm enable bit
      hrtc.Instance->CR &= ~(1UL<<8) ;


      //Increment time by ten minutes or reset alarm to 8 am if past 5pm


      if(sAlarm.AlarmTime.Minutes < 0x50 ){
          sAlarm.AlarmTime.Minutes = sAlarm.AlarmTime.Minutes + 0x10;
      }
      else{
          sAlarm.AlarmTime.Minutes = 0x0;
          if(sAlarm.AlarmTime.Hours < 0x17){
          sAlarm.AlarmTime.Hours = sAlarm.AlarmTime.Hours + 0x1;
          }
          else{
              sAlarm.AlarmTime.Hours = 0x8;
          }
      }
      sAlarm.AlarmTime.Seconds = 0x0;
      sAlarm.AlarmTime.SubSeconds = 0x0;
      sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
      sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
      sAlarm.AlarmMask = RTC_ALARMMASK_HOURS|RTC_ALARMMASK_MINUTES;
      sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
      sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
      sAlarm.AlarmDateWeekDay = 0x1;
      sAlarm.Alarm = RTC_ALARM_A;
      if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK)
      {
        _Error_Handler(__FILE__, __LINE__);
      }


      //Enable alarm bit
      hrtc.Instance->CR |= 1UL<<8 ;


  /* USER CODE END RTC_Alarm_IRQn 0 */
      HAL_RTC_AlarmIRQHandler(&hrtc);
  /* USER CODE BEGIN RTC_Alarm_IRQn 1 */

 

  /* USER CODE END RTC_Alarm_IRQn 1 */
}

Thanks in advance for any help.

Outcomes