-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds1307.c
201 lines (169 loc) · 4.06 KB
/
ds1307.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
193
194
195
196
197
198
199
200
201
/*! \file ds1307.c
\brief DS1307 RTC Functions
DS1307 RTC Functions
*/
#include "pic.h"
#include "i2c.h"
#define DS1307_WRITE_ADDRESS 0b11010000
#define DS1307_READ_ADDRESS 0b11010001
#define DS1307_TIMEOUT 500000
#pragma udata DS1307
unsigned char DS1307_BUFFER[0x3f];
#pragma udata
unsigned char DS1307_ToBCD(char in)
{
unsigned char out_ones;
unsigned char out_tens;
unsigned char value;
// First get rid of hundres place
value = in / 100;
in = in - (value * 100);
out_tens = in / 10;
in = in - (value * 10);
out_ones = in;
out_tens = out_tens << 4;
return (out_ones + out_tens);
}
unsigned char DS1307_FromBCD(char in)
{
unsigned char out;
unsigned char ones;
unsigned char tens;
ones = in & 0x0f;
tens = (in >> 4)*10;
return ones + tens;
}
char DS1307_Disable(void)
{
unsigned char s[2] = { 0,0};
BIT_SET(&s[1],7);
if(I2C_Master7_WriteBytes(DS1307_WRITE_ADDRESS,&s,2, DS1307_TIMEOUT,100)==0)
return 1; // Success
else
return 0; // Failed;
}
char DS1307_Enable(void)
{
unsigned char s[2] = {0,0};
if(I2C_Master7_WriteBytes(DS1307_WRITE_ADDRESS,&s,2, DS1307_TIMEOUT,100)==0)
return 1; // Success
else
return 0; // Failed;
}
char DS1307_SetHour(char hour)
{
unsigned char b[2];
b[0] = 0x02;
b[1] = DS1307_ToBCD(hour);
BIT_SET(&b[1],6);
if(I2C_Master7_WriteBytes(DS1307_WRITE_ADDRESS,&b[0],2, DS1307_TIMEOUT,100)==0)
return 1; // Success
else
return 0; // Failed;
}
char DS1307_SetSeconds(char seconds)
{
unsigned char b[2];
b[0] = 0x00;
b[1] = DS1307_ToBCD(seconds);
if(I2C_Master7_WriteBytes(DS1307_WRITE_ADDRESS,&b[0],2, DS1307_TIMEOUT,100)==0)
return 1; // Success
else
return 0; // Failed;
}
char DS1307_SetMinutes(char minutes)
{
unsigned char b[2];
b[0] = 0x01;
b[1] = DS1307_ToBCD(minutes);
if(I2C_Master7_WriteBytes(DS1307_WRITE_ADDRESS,&b[0],2, DS1307_TIMEOUT,100)==0)
return 1; // Success
else
return 0; // Failed;
}
char DS1307_SetDay(char day)
{
unsigned char b[2];
b[0] = 0x03;
b[1] = DS1307_ToBCD(day);
if(I2C_Master7_WriteBytes(DS1307_WRITE_ADDRESS,&b[0],2, DS1307_TIMEOUT,100)==0)
return 1; // Success
else
return 0; // Failed;
}
char DS1307_SetDate(char date)
{
unsigned char b[2];
b[0] = 0x04;
b[1] = DS1307_ToBCD(date);
if(I2C_Master7_WriteBytes(DS1307_WRITE_ADDRESS,&b[0],2, DS1307_TIMEOUT,100)==0)
return 1; // Success
else
return 0; // Failed;
}
char DS1307_SetMonth(char month)
{
unsigned char b[2];
b[0] = 0x05;
b[1] = DS1307_ToBCD(month);
if(I2C_Master7_WriteBytes(DS1307_WRITE_ADDRESS,&b[0],2, DS1307_TIMEOUT,100)==0)
return 1; // Success
else
return 0; // Failed;
}
char DS1307_SetYear(char year)
{
unsigned char b[2];
b[0] = 0x06;
b[1] = DS1307_ToBCD(year);
if(I2C_Master7_WriteBytes(DS1307_WRITE_ADDRESS,&b[0],2, DS1307_TIMEOUT,100)==0)
return 1; // Success
else
return 0; // Failed;
}
char DS1307_WriteRAM(char addr,unsigned char c)
{
unsigned char b[2];
if(addr < 0x3f) {
b[0] = 0x08 + addr;
b[1] = c;
if(I2C_Master7_WriteBytes(DS1307_WRITE_ADDRESS,&b[0],2, DS1307_TIMEOUT,100)==0)
return 1; // Success
else
return 0; // Failed;
}
return 0;
}
char DS1307_Read(void)
{
unsigned char b[2] = {0x7,0x0};
// First we write to the control register to set our location
// Writing anywhere else would adjust the time or change a value
if(I2C_Master7_WriteBytes(DS1307_WRITE_ADDRESS,&b[0],2, DS1307_TIMEOUT,100)==0)
return 1; // Success
else
return 0; // Failed;
if(I2C_Master7_ReadBytes(DS1307_READ_ADDRESS,&DS1307_BUFFER[0],0x3f, DS1307_TIMEOUT,100)==0)
return 1; // success
else
return 0; // failed
}
char DS1307_GetSeconds(void)
{
return(DS1307_FromBCD(DS1307_BUFFER[0x3f - 7]));
}
char DS1307_GetMinutes(void)
{
return(DS1307_FromBCD(DS1307_BUFFER[0x3f - 6]));
}
char DS1307_GetHours(void)
{
return(DS1307_FromBCD(DS1307_BUFFER[0x3f - 5]));
}
char DS1307_GetN(char c)
{
c=c - 0x7;
if(c < 0)
c = c + 0x3f;
return(DS1307_BUFFER[c]);
}