Question
How to get day of week by RTC? if you had no idea so I had share my function.
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.
