Professional Documents
Culture Documents
RTC_SLAVE_ADDRESS | 0x01
char second;
18.
char minute;
19.
char hour;
20. };
21. struct DateTime
22. {
23.
char second;
24.
char minute;
25.
char hour;
26.
char day;
27.
char date;
28.
char month;
29.
char year;
30. };
31.
32.
33. void i2c_release(void)
34. {
35.
DirSDA = 1;
36.
DirSCL = 1;
37. }
38. void i2c_start(void)
39. {
40.
DirSCL = 0;
41.
SCL = 1;
42.
SCL = 1;
43.
DirSDA = 0;
44.
SDA = 0;
45.
SDA = 0;
46.
SCL = 0;
47.
SCL = 0;
48. }
49. void i2c_stop(void)
50. {
51.
52.
DirSDA = 0;
53.
SDA = 0;
54.
SDA = 0;
55.
DirSCL = 0;
56.
SCL = 1;
57.
SCL = 1;
58.
SDA = 1;
59.
SDA = 1;
60. }
61. unsigned int i2c_read(int1 ack)
62. {
63.
int i, result;
64.
65.
result = 0;
66.
67.
SCL = 0;
68.
SCL = 0;
69.
70.
71.
SCL = 1;
72.
SCL = 1;
73.
result <<= 1;
74.
if(SDA == 1) result++;
75.
SCL = 0;
76.
SCL = 0;
77.
78.
79.
80.
SCL = 1;
81.
SCL = 1;
82.
SCL = 0;
83.
SCL = 0;
84.
return result;
85. }
86. int1 i2c_write(unsigned int value)
87. {
88.
89.
int1 ack;
90.
91.
temp = value;
92.
DirSDA = 0;
93.
SCL = 0;
94.
SCL = 0;
95.
96.
97.
98.
SCL = 1;
99.
SCL = 1;
100.
SCL = 0;
101.
SCL = 0;
102.
temp <<= 1;
103.
104.
105.
SCL = 1;
106.
SCL = 1;
107.
ack = SDA;
108.
SCL = 0;
109.
SCL = 0;
110.
return ack;
111.
112.
113.
114.
void DS1307_init()
115.
116.
char x;
117.
118.
i2c_release();
119.
delay_us(10);
120.
i2c_start();
121.
122.
i2c_write(0x00);
123.
i2c_start();
124.
i2c_write(RTC_READ_COMMAND);
125.
126.
i2c_stop();
127.
128.
i2c_start();
129.
i2c_write(RTC_WRITE_COMMAND);
130.
131.
132.
i2c_stop();
133.
134.
i2c_start();
135.
i2c_write(RTC_WRITE_COMMAND);
136.
137.
138.
i2c_stop();
139.
140.
141.
142.
143.
144.
struct Time t;
145.
146.
i2c_start();
147.
i2c_write(RTC_WRITE_COMMAND);
148.
i2c_write(0x00);
149.
i2c_start();
150.
i2c_write(RTC_READ_COMMAND);
// Go to top of memory
151.
152.
t.minute = i2c_read(1);
153.
t.hour = i2c_read(0);
154.
i2c_stop();
155.
return t;
156.
157.
158.
159.
struct DateTime t;
160.
161.
i2c_start();
162.
i2c_write(RTC_WRITE_COMMAND);
163.
i2c_write(0x00);
164.
i2c_start();
165.
i2c_write(RTC_READ_COMMAND);
166.
167.
t.minute = i2c_read(1);
168.
t.hour = i2c_read(1);
169.
t.day = i2c_read(1);
170.
t.date = i2c_read(1);
171.
t.month = i2c_read(1);
172.
t.year = i2c_read(0);
173.
i2c_stop();
174.
return t;
175.
// Go to top of memory
176.
177.
178.
i2c_start();
179.
i2c_write(RTC_WRITE_COMMAND);
180.
i2c_write(0x00);
181.
i2c_write(t.second);
182.
i2c_write(t.minute);
183.
i2c_write(t.hour);
184.
i2c_stop();
185.
186.
187.
188.
i2c_start();
189.
i2c_write(RTC_WRITE_COMMAND);
190.
i2c_write(0x04);
191.
192.
193.
i2c_write(t.hour);
194.
i2c_stop();
// set Year
195.
196.
197.
198.
i2c_start();
199.
i2c_write(RTC_WRITE_COMMAND);
200.
i2c_write(0x00);
201.
i2c_write(t.second);
202.
i2c_write(t.minute);
203.
i2c_write(t.hour);
204.
i2c_write(t.day);
205.
i2c_write(t.date);
206.
i2c_write(t.month);
207.
i2c_write(t.year);
208.
i2c_stop();
209.
210.
211.
212.
i2c_start();
213.
i2c_write(RTC_WRITE_COMMAND);
214.
i2c_write(addr);
215.
i2c_write(value);
216.
i2c_stop();
217.
218.
219.
220.
221.
222.
i2c_start();
223.
i2c_write(RTC_WRITE_COMMAND);
224.
i2c_write(addr);
225.
i2c_start();
226.
i2c_write(RTC_READ_COMMAND);
227.
value = i2c_read(0);
228.
i2c_stop();
229.
return value;
230.