-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealtimeclock.c
193 lines (168 loc) · 4.01 KB
/
realtimeclock.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
192
#include <xc.h>
#include "realtimeclock.h"
int __hex_to_int(unsigned char hex) {
int t = 0;
for(int i = 0; i < hex; ++i) {
++t;
if(t && t % 10 == 0)
i+=6;
}
return t;
}
unsigned char __int_to_hex(int integer) {
int t = 0;
for(int i = 0; i < integer; ++i) {
++t;
if(i && i % 10 == 0) {
t+=6;
}
}
return t;
}
void RealTimeClock_write_byte(unsigned char time_tx) {
for(int i = 0; i < 8; ++i) {
i_o = 0;
sclk = 0;
if (time_tx&0x01) {
i_o = 1;
}
time_tx = time_tx >> 1;
sclk = 1;
}
sclk = 0;
}
unsigned char RealTimeClock_read_byte(void) {
unsigned char time_rx = 0;
TRISB4 = 1;
for(int i = 0; i < 8; ++i) {
sclk = 1;
//time_rx = time_rx >> 1;
//time_rx |= (((unsigned char)i_o) << i);
if(i_o) time_rx += 1 << i;
sclk = 0;
}
sclk = 0;
TRISB4 = 0;
return time_rx;
}
void RealTimeClock_set_burst_time(int* time) {
rst = 1;
RealTimeClock_write_byte(0xbe);
for(int i = 0; i < 8; ++i) {
RealTimeClock_write_byte(__int_to_hex(time[i]));
}
rst = 0;
}
int* RealTimeClock_get_burst_time(void) {
rst = 1;
RealTimeClock_write_byte(0xbf);
for(int i = 0; i < 7; ++i) {
__RealTimeClock_buff[i] = __hex_to_int(RealTimeClock_read_byte());
}
rst = 0;
return __RealTimeClock_buff;
}
void RealTimeClock_init(void) {
rst = 0;
ADCON1 = 0x06;
//TRISB = 0;
TRISB0 = 0;
TRISB4 = 0;
TRISB5 = 0;
sclk = 0;
rst = 1;
RealTimeClock_write_byte(0x8e);
RealTimeClock_write_byte(0);
rst = 0;
}
int RealTimeClock_get_seconds(void) {
rst = 1;
RealTimeClock_write_byte(0x81);
unsigned char c = RealTimeClock_read_byte();
rst = 0;
return __hex_to_int(c);
}
int RealTimeClock_get_minutes(void) {
rst = 1;
RealTimeClock_write_byte(0x83);
unsigned char c = RealTimeClock_read_byte();
rst = 0;
return __hex_to_int(c);
}
int RealTimeClock_get_hours(void) {
rst = 1;
RealTimeClock_write_byte(0x85);
unsigned char c = RealTimeClock_read_byte();
rst = 0;
return __hex_to_int(c);
}
int RealTimeClock_get_day_of_month(void) {
rst = 1;
RealTimeClock_write_byte(0x87);
unsigned char c = RealTimeClock_read_byte();
rst = 0;
return __hex_to_int(c);
}
int RealTimeClock_get_month(void) {
rst = 1;
RealTimeClock_write_byte(0x89);
unsigned char c = RealTimeClock_read_byte();
rst = 0;
return __hex_to_int(c);
}
int RealTimeClock_get_day_of_week(void) {
rst = 1;
RealTimeClock_write_byte(0x8b);
unsigned char c = RealTimeClock_read_byte();
rst = 0;
return __hex_to_int(c);
}
int RealTimeClock_get_year(void) {
rst = 1;
RealTimeClock_write_byte(0x8d);
unsigned char c = RealTimeClock_read_byte();
rst = 0;
return __hex_to_int(c);
}
void RealTimeClock_set_seconds(int seconds) {
rst = 1;
RealTimeClock_write_byte(0x80);
RealTimeClock_write_byte(__int_to_hex(seconds));
rst = 0;
}
void RealTimeClock_set_minutes(int minutes) {
rst = 1;
RealTimeClock_write_byte(0x82);
RealTimeClock_write_byte(__int_to_hex(minutes));
rst = 0;
}
void RealTimeClock_set_hours(int hours) {
rst = 1;
RealTimeClock_write_byte(0x84);
RealTimeClock_write_byte(__int_to_hex(hours));
rst = 0;
}
void RealTimeClock_set_day_of_month(int month) {
rst = 1;
RealTimeClock_write_byte(0x86);
RealTimeClock_write_byte(__int_to_hex(month));
rst = 0;
}
void RealTimeClock_set_month(int month) {
rst = 1;
RealTimeClock_write_byte(0x88);
RealTimeClock_write_byte(__int_to_hex(month));
rst = 0;
}
void RealTimeClock_set_day_of_week(int day) {
rst = 1;
RealTimeClock_write_byte(0x8a);
RealTimeClock_write_byte(__int_to_hex(day));
rst = 0;
}
void RealTimeClock_set_year(int year) {
rst = 1;
RealTimeClock_write_byte(0x8c);
RealTimeClock_write_byte(__int_to_hex(year));
rst = 0;
}