soft Real Time Clock implementation
soft Real Time Clock may be implemented when MCU don't have hardware RTC.
typedef struct t_smalltm{
u16 year;
u8 mon;
u8 day;
u8 hour;
u8 min;
u8 sec;
} t_smalltm;
t_smalltm rtc;
//**************************************************************
// call this function every 1 sec. from timer interrupt.
// for fast code execution rtc will be placed in internal RAM.
//**************************************************************
void realTimeClock(void)
{u8 rest;
if (++rtc.sec==60) // sec
{
rtc.sec=0;
if (++rtc.min==60) // min
{
rtc.min=0;
if (++rtc.hour==24) // hour
{
rtc.hour=0;
rtc.day++; // day
rest=rtc.year%4;
if ((((rest==0 && rtc.year%100!=0) || rtc.year%400==0)
&& rtc.mon==2 && rtc.day==30)
|| (rest && rtc.mon==2 && rtc.day==29)
|| ((rtc.mon==4 || rtc.mon==6 || rtc.mon==9 || rtc.mon==11)
&& rtc.day==31)
|| (rtc.day==32))
{
rtc.day=1;
if (++rtc.mon==13) // mon
rtc.mon=1, rtc.year++; // HAPPY NEW YEAR ! :)
}
}
}
}
}
//**************************************************************
// read RTC function
void getRTC(t_smalltm* stm)
{
disableInterrupts(); // evite erronated read
// because realTimeClock is called from interrupt
memcpy(stm,&rtc,sizeof(t_smalltm));
enableInterrupts();
}
//**************************************************************
// set RTC function
void setRTC(u16 year, u8 mon, u8 day, u8 hour, u8 min, u8 sec)
{
disableInterrupts();
rtc.year=year;
rtc.mon=mon;
rtc.day=day;
rtc.hour=hour;
rtc.min=min;
rtc.sec=sec;
enableInterrupts();
}