#include "main.h"
typedef struct RTCdata {
unsigned char hours;
unsigned char minutes;
unsigned char seconds;
unsigned char day;
unsigned char month;
int year;
unsigned char daysPerMonth[12];
}RTCdata;
RTCdata rtc;
bool isLeapYear(int yearT)
{
if(((yearT % 4) == 0) && ((yearT % 100) != 0) || ((yearT % 400) == 0))
return true;
return false;
}
void rtcInit(RTCdata * t)
{
t->daysPerMonth[0] = 31;
t->daysPerMonth[1] = 28;
t->daysPerMonth[2] = 31;
t->daysPerMonth[3] = 30;
t->daysPerMonth[4] = 31;
t->daysPerMonth[5] = 30;
t->daysPerMonth[6] = 31;
t->daysPerMonth[7] = 31;
t->daysPerMonth[8] = 30;
t->daysPerMonth[9] = 31;
t->daysPerMonth[10] = 30;
t->daysPerMonth[11] = 31;
t->hours = 0;
t->minutes = 0;
t->seconds = 0;
t->day = 1;
t->month = 1;
t->year = 2000;
}
bool setTime(unsigned char hourT, unsigned char minuteT, unsigned char secondT, RTCdata * t)
{
if(hourT > 23) return false;
if(minuteT > 59) return false;
if(secondT > 59) return false;
t->hours = hourT;
t->minutes = minuteT;
t->seconds = secondT;
return true;
}
bool setDate(unsigned char dayT, unsigned char monthT, int yearT, RTCdata * t)
{
if((dayT < 1) || (dayT > 31)) return false;
if((monthT < 1) || (monthT > 12)) return false;
if(yearT < 0) return false;
t->day = dayT;
t->month = monthT;
t->year = yearT;
return true;
}
unsigned long getTimestamp(int yearT, RTCdata * t)
{
int i;
unsigned long time = 0;
if(yearT == 0)
yearT = 1970;
else
if(yearT < 1900)
yearT = 1900;
else
if(yearT > 1970)
yearT = 1970;
else
if((yearT != 1900) && (yearT != 1970))
yearT = 1970;
time += (t->year - yearT) * 365.2422;
for(i = 0; i < t->month - 1; i++)
time += t->daysPerMonth[i];
time = (time + t->day) * 24UL;
time = (time + t->hours) * 60UL;
time = (time + t->minutes) * 60UL;
time += t->seconds;
if(time > 951847199UL)
time += 86400UL;
return (time - 86400UL);
}
unsigned char setClockWithTimestamp(unsigned long timeT, int yearRef, RTCdata * t)
{
int yearT, monthT, hoursT, minutesT;
unsigned long dayT;
float remaining, dayRemaining;
if(timeT > 951847199UL)
timeT -= 86400UL;
timeT += 86400UL;
dayT = timeT / (60UL * 60UL * 24UL);
remaining = timeT - dayT * (60UL * 60UL * 24UL);
yearT = (dayT / 365.2422);
dayRemaining = dayT - yearT * 365.2422;
if(yearRef == 0)
yearRef = 1970;
else
if(yearRef < 1900)
yearRef = 1900;
else
if(yearRef > 1970)
yearRef = 1970;
else
if((yearRef != 1900) && (yearRef != 1970))
yearRef = 1970;
yearT += yearRef;
if(dayRemaining >= 365.2422)
return 1;
if(yearT < yearRef)
return 2;
monthT = 0;
while(dayRemaining > t->daysPerMonth[monthT])
{
dayRemaining -= t->daysPerMonth[monthT];
if(monthT == 1 && isLeapYear(yearT))
dayRemaining--;
monthT++;
}
monthT++;
if(monthT > 12)
return 3;
if(dayRemaining >= (60UL * 60UL * 24UL))
return 4;
dayT = dayRemaining;
if(dayRemaining - dayT > 0)
dayT++;
hoursT = remaining / (60UL * 60UL);
remaining = remaining - hoursT * (60UL * 60UL);
if(remaining >= (60UL * 60UL))
return 5;
minutesT = remaining / 60UL;
remaining = remaining - minutesT * 60UL;
if(remaining >= 60)
return 6;
t->year = yearT;
t->month = monthT;
t->day = dayT;
t->hours = hoursT;
t->minutes = minutesT;
t->seconds = remaining;
return 0;
}
unsigned char weekDay(unsigned long timestamp)
{
return ((timestamp / 86400UL) + 4 ) % 7;
}
unsigned char getWeekDay(RTCdata * t)
{
return (weekDay(getTimestamp(0, t)));
}
void RTC(RTCdata * t)
{
unsigned char dayT;
t->seconds++;
if(rtc.seconds > 59)
{
t->seconds = 0;
t->minutes++;
if(t->minutes > 59)
{
t->minutes = 0;
t->hours++;
if(t->hours > 23)
{
t->hours = 0;
t->day++;
if(t->month == 2)
{
if(isLeapYear(t->year))
dayT = 29;
else
dayT = 28;
}
else
dayT = t->daysPerMonth[t->month - 1];
if(t->day > dayT)
{
t->day = 1;
t->month++;
if(t->month > 12)
{
t->month = 1;
t->year++;
}
}
}
}
}
}
void BackgroundProcess()
{
RTC(&rtc);
printf("%4d/%02d/%02d - %02d:%02d:%02d: TS = %d\n", rtc.year, rtc.month, rtc.day, rtc.hours, rtc.minutes, rtc.seconds, getTimestamp(0, &rtc));
}
int main(void)
{
Set_PLL(16, 4);
Set_Port();
Set_Uart0(115200);
Set_TimerInterruptService(TIMER_0, &BackgroundProcess, 1000000);
Set_TimerEnable(TIMER_0, true);
rtcInit(&rtc);
rtc.year = 2015;
rtc.month = 3;
rtc.day = 20;
rtc.hours = 19;
rtc.minutes = 35;
rtc.seconds = 23;
while(1)
{
}
return 0;
}