-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrtc.c
191 lines (154 loc) · 3.14 KB
/
rtc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "pinout.h"
#include "sys.h"
#include "rtc.h"
#include "delay.h"
#include "settings.h"
#include "i2c.h"
RTC_type rtc;
static code RTC_type rtcMin = {0, 0, 0, 1, 1, 1, 0, RTC_NOEDIT,0};
static code RTC_type rtcMax = {59, 59, 23, 7, 31, 12, 99, RTC_NOEDIT,99};
static code uint16_t rtcMonthNumberDay[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
static void rtcWeekDay(void)
{
uint8_t a, y, m;
a = (rtc.month > 2 ? 0 : 1);
y = 12 + rtc.year - a;
m = rtc.month + 12 * a - 2;
rtc.wday = (rtc.date + y + (y / 4) + ((31 * m) / 12) - 1) % 7;
if (rtc.wday == 0)
rtc.wday = 7;
return;
}
uint8_t rtcDaysInMonth(void)
{
uint8_t ret = rtc.month;
if (ret == 2) {
ret = rtc.year & 0x03;
ret = (ret ? 29 : 28);
}
else {
if (ret > 7)
ret++;
ret |= 30;
}
return ret;
}
void rtcInit(void)
{
uint8_t wday;
I2CswStart(RTC_I2C_ADDR);
I2CswWriteByte(0x0A);
I2CswWriteByte(0x81);
I2CswStop();
I2CswStart(RTC_I2C_ADDR);
I2CswWriteByte(0x0D);
I2CswWriteByte(0x81);
I2CswWriteByte(0x04);
I2CswWriteByte(0x00);
I2CswWriteByte(eep.timecoef);
I2CswStop();
rtcReadTime();
wday = rtc.wday;
rtcWeekDay();
if(wday != rtc.wday) {
rtc.year = 19;
rtcSaveDate();
}
return;
}
void rtcSaveTime(void)
{
uint8_t i;
I2CswStart(RTC_I2C_ADDR);
I2CswWriteByte(RTC_SEC);
for (i = RTC_SEC; i <= RTC_HOUR; i++)
I2CswWriteByte(rtcDecToBinDec(*((uint8_t*)&rtc + i)));
I2CswStop();
return;
}
void rtcSaveDate(void)
{
uint8_t i;
rtcWeekDay();
I2CswStart(RTC_I2C_ADDR);
I2CswWriteByte(RTC_WDAY);
for (i = RTC_WDAY; i <= RTC_YEAR; i++)
I2CswWriteByte(rtcDecToBinDec(*((uint8_t*)&rtc + i)));
I2CswStop();
return;
}
void rtcSavePPM(void)
{
I2CswStart(RTC_I2C_ADDR);
I2CswWriteByte(RTC_AGING);
I2CswWriteByte(eep.timecoef);
I2CswStop();
return;
}
void rtcReadTime(void)
{
uint8_t i, temp;
I2CswStart(RTC_I2C_ADDR);
I2CswWriteByte(RTC_SEC);
I2CswStop();
I2CswStart(RTC_I2C_ADDR | I2C_READ);
for (i = RTC_SEC; i <= RTC_YEAR; i++) {
temp = I2CswReadByte((i == RTC_YEAR) ? I2C_NOACK : I2C_ACK);
*((uint8_t*)&rtc + i) = rtcBinDecToDec(temp);
}
I2CswStop();
I2CswStart(RTC_I2C_ADDR);
I2CswWriteByte(RTC_TEMP);
I2CswStop();
I2CswStart(RTC_I2C_ADDR | I2C_READ);
rtc.temp = I2CswReadByte(I2C_NOACK);
I2CswStop();
return;
}
void rtcChangeTime(int8_t diff)
{
int8_t *time = (int8_t*)&rtc + rtc.etm;
int8_t timeMax = *((int8_t*)&rtcMax + rtc.etm);
int8_t timeMin = *((int8_t*)&rtcMin + rtc.etm);
if (rtc.etm == RTC_DATE)
timeMax = rtcDaysInMonth();
*time += diff;
if (*time > timeMax)
*time = timeMin;
if (*time < timeMin)
*time = timeMax;
return;
}
void rtcNextEditParam(void)
{
switch (rtc.etm) {
case RTC_HOUR:
case RTC_MIN:
rtc.etm--;
break;
case RTC_SEC:
rtc.etm = RTC_DATE;
break;
case RTC_DATE:
case RTC_MONTH:
case RTC_YEAR:
rtc.etm--;
break;
default:
rtc.etm = RTC_HOUR;
break;
}
return;
}
uint16_t rtcYearDay(void)
{
return rtcMonthNumberDay[rtc.month-1] + rtc.date + (((rtc.year % 4 == 0) && (rtc.month>2))? 1 : 0);
}
uint8_t rtcBinDecToDec(uint8_t num)
{
return (num >> 4) * 10 + (num & 0x0F);
}
uint8_t rtcDecToBinDec(uint8_t num)
{
return ((num / 10) << 4) + (num % 10);
}