cancel
Showing results for 
Search instead for 
Did you mean: 

How to get day of week by RTC? if you had no idea so I had share my function.

Tinnagit
Senior II

I had use RTC calendar in STM32 with ext-oscillator.
but I was surprised when I try to set date which it's don't set day of week by itself.
and they also had no function to find day of week too.

OK. I make this function follow Sakamoto's methods 
calendarCalculator.h

/*
 * calendarCalculator.h
 *
 *  Created on: Jul 19, 2023
 *      Author: Tihnov
 */

#ifndef INC_CALENDARCALCULATOR_H_
#define INC_CALENDARCALCULATOR_H_

#include "main.h"

uint8_t Calendar_GetDayWeek (RTC_DateTypeDef thisDate);

#endif /* INC_CALENDARCALCULATOR_H_ */

 calendarCalculator.c

#include "math.h"
#include "string.h"
#include "main.h"
#include "calendarCalculator.h"

extern RTC_HandleTypeDef hrtc;

static const uint8_t list_mth[12] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};

uint8_t Calendar_GetDayWeek (RTC_DateTypeDef thisDate){
	uint8_t ret = 0;
    if(thisDate.Month < 3){
    	thisDate.Year -= 1;
    }
    ret = (uint8_t)((thisDate.Year + (thisDate.Year/4) - (thisDate.Year/100) + (thisDate.Year/400) + list_mth[thisDate.Month-1] + thisDate.Date) % 7);
	return (ret);
}

 I hope this function will make you easier to find day of week to set date correctly.

3 REPLIES 3
STOne-32
ST Employee

Dear @Tinnagit ,

 

you can refer to this PDF on STM32G4  series to directly read from our Calendar shadow registers of the RTC https://www.st.com/resource/en/product_training/STM32G4-WDG_TIMERS-Real-Time_Clock_RTC.pdf .  

which MCU series are used in your applications ?

ciao

STOne-32

No, In regular situation you should not read date and time register from shadow register.
You should sync regular register with shadow register and read date and time after it.

and my function is a solution when you need to know day of week of date.
I know that when you set time to current day you know exactly day of week 
but if you need to set date to some date so what's that day of week and the calendar is not calculate automatically for you. this is my function do for find day of week of date a whole date until 31 DEC 2099.

Tinnagit
Senior II

Oh Sorry you are one from ST.
I think if you had this function that it's easier to set date or alarm.