-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmcp7940.h
243 lines (186 loc) · 6.34 KB
/
mcp7940.h
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#ifndef MCP7940_H_
#define MCP7940_H_
#ifndef F_CPU
#define F_CPU 20000000UL
#endif
#ifndef MCP7940_ADDRESS
#define MCP7940_ADDRESS 0x6F
#endif
#ifndef MCP7940_RTCSEC
#define MCP7940_RTCSEC 0x00
#define RTC_ST_bm 0x80
#define RTC_SECTEN_bm 0x70
#define RTC_SECTEN_bp 4
#endif
#ifndef MCP7940_RTCMIN
#define MCP7940_RTCMIN 0x01
#define RTC_MINTEN_bm 0x70
#define RTC_MINTEN_bp 4
#endif
#ifndef MCP7940_RTCHOUR
#define MCP7940_RTCHOUR 0x02
#define RTC_FORMAT_bm 0x40
#define RTC_HRTEN_bm 0x30
#define RTC_HRTEN_bp 4
#endif
#ifndef MCP7940_RTCWKDAY
#define MCP7940_RTCWKDAY 0x03
#define RTC_OSCRUN_bm 0x20
#define RTC_PWRFAIL_bm 0x10
#define RTC_VBATEN_bm 0x08
#define RTC_WKDAY_bp 0
#endif
#ifndef MCP7940_RTCDATE
#define MCP7940_RTCDATE 0x04
#define RTC_DATETEN_bm 0x30
#define RTC_DATETEN_bp 4
#endif
#ifndef MCP7940_RTCMTH
#define MCP7940_RTCMTH 0x05
#define RTC_LPYR_bp 5
#define RTC_LPYR_bm 0x20
#define RTC_MTHTEN_bm 0x10
#define RTC_MTHTEN_bp 4
#endif
#ifndef MCP7940_RTCYEAR
#define MCP7940_RTCYEAR 0x06
#define RTC_YRTEN_bm 0xF0
#define RTC_YRTEN_bp 4
#endif
#ifndef MCP7940_CONTROL
#define MCP7940_CONTROL 0x07
#define RTC_OUT_bm 0x80
#define RTC_SQWEN_bm 0x40
#define RTC_ALM1EN_bm 0x20
#define RTC_ALM0EN_bm 0x10
#define RTC_EXTOSC_bm 0x08
#define RTC_CSTRIM_bm 0x04
#define RTC_SQWFS1_bm 0x02
#define RTC_SQWFS0_bm 0x01
#define RTC_SQWFS_32768HZ (RTC_SQWFS1_bm | RTC_SQWFS0_bm)
#define RTC_SQWFS_8192HZ RTC_SQWFS1_bm
#define RTC_SQWFS_4096HZ RTC_SQWFS0_bm
#define RTC_SQWFS_1HZ 0x00
#endif
#ifndef MCP7940_OSCTRIM
#define MCP7940_OSCTRIM 0x08
#define RTC_SIGN_bm 0x80
#endif
// #############################################
#if !defined(MCP7940_ALM0SEC) && !defined(MCP7940_ALM1SEC)
#define MCP7940_ALM0SEC 0x0A
#define MCP7940_ALM1SEC 0x11
#define ALM_SECTEN_bp 4
#define ALM_SECONE_bp 0
#endif
#if !defined(MCP7940_ALM0MIN) && !defined(MCP7940_ALM1MIN)
#define MCP7940_ALM0MIN 0x0B
#define MCP7940_ALM1MIN 0x12
#define ALM_MINTEN_bp 4
#define ALM_MINONE_bp 0
#endif
#if !defined(MCP7940_ALM0HOUR) && !defined(MCP7940_ALM1HOUR)
#define MCP7940_ALM0HOUR 0x0C
#define MCP7940_ALM1HOUR 0x13
#define ALM_FORMAT_bm 0x40
#define ALM_HRTEN_bp 4
#define ALM_HRONE_bp 0
#endif
#if !defined(MCP7940_ALM0WKDAY) && !defined(MCP7940_ALM1WKDAY)
#define MCP7940_ALM0WKDAY 0x0D
#define MCP7940_ALM1WKDAY 0x14
#define ALM_ALMPOL_bm 0x80
#define ALM_ALMMSK2_bm 0x40
#define ALM_ALMMSK1_bm 0x20
#define ALM_ALMMSK0_bm 0x10
#define ALM_ALMIF_bm 0x08
#define ALM_WKDAY_bp 0
#define ALM_SECOND_MATCH 0x00
#define ALM_MINUTE_MATCH ALM_ALMMSK0_bm
#define ALM_HOUR_MATCH ALM_ALMMSK1_bm
#define ALM_DAY_MATCH (ALM_ALMMSK1_bm | ALM_ALMMSK1_bm)
#define ALM_DATE_MATCH ALM_ALMMSK2_bm
#define ALM_FULL_MATCH (ALM_ALMMSK2_bm | ALM_ALMMSK1_bm | ALM_ALMMSK0_bm)
#endif
#if !defined(MCP7940_ALM0DATE) && !defined(MCP7940_ALM1DATE)
#define MCP7940_ALM0DATE 0x0E
#define MCP7940_ALM1DATE 0x15
#define ALM_DATETEN_bp 4
#define ALM_DATEONE_bp 0
#endif
#if !defined(MCP7940_ALM0MTH) && !defined(MCP7940_ALM1MTH)
#define MCP7940_ALM0MTH 0x0F
#define MCP7940_ALM1MTH 0x16
#define ALM_MTHTEN_bp 4
#define ALM_MTHONE_bp 0
#endif
// #############################################
#if !defined(MCP7940_PWRDNMIN) && !defined(MCP7940_PWRUPMIN)
#define MCP7940_PWRDNMIN 0x18
#define MCP7940_PWRUPMIN 0x1C
#endif
#if !defined(MCP7940_PWRDNHOUR) && !defined(MCP7940_PWRUPHOUR)
#define MCP7940_PWRDNHOUR 0x19
#define MCP7940_PWRUPHOUR 0x1D
#endif
#if !defined(MCP7940_PWRDNDATE) && !defined(MCP7940_PWRUPDATE)
#define MCP7940_PWRDNDATE 0x1A
#define MCP7940_PWRUPDATE 0x1E
#endif
#if !defined(MCP7940_PWRDNMTH) && !defined(MCP7940_PWRUPMTH)
#define MCP7940_PWRDNMTH 0x1B
#define MCP7940_PWRUPMTH 0x1F
#define MCP7940_PWRWEEKDAY_bp 5
#endif
// #############################################
#ifndef MCP7940_MFP_PORT
#define MCP7940_MFP_PORT RTC_SQWEN_bm
#endif
#include <avr/io.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#include "../twi/twi.h"
#include "../types/types.h"
enum RTC_Status_t
{
RTC_Disabled=0,
RTC_Running,
RTC_Fail,
RTC_Power_Fail,
RTC_Valid
};
typedef enum RTC_Status_t RTC_Status;
enum RTC_Mode_t
{
RTC_Disable=0,
RTC_Enable
};
typedef enum RTC_Mode_t RTC_Mode;
enum RTC_Data_t
{
RTC_Current=0,
RTC_Power_Down,
RTC_Power_Up
};
typedef enum RTC_Data_t RTC_Data;
void mcp7940_init(RTC_Mode battery);
void mcp7940_battery(RTC_Mode mode);
void mcp7940_oscillator(RTC_Mode mode);
unsigned char mcp7940_oscillator_value(void);
void mcp7940_oscillator_copy(Copy_Mode mode);
RTC_Status mcp7940_powerfail_status(void);
RTC_Status mcp7940_oscillator_status(void);
const char* mcp7940_weekday_string(unsigned char day);
unsigned char mcp7940_hour(RTC_Data data);
unsigned char mcp7940_minute(RTC_Data data);
unsigned char mcp7940_second(void);
unsigned char mcp7940_weekday(RTC_Data data);
unsigned char mcp7940_day(RTC_Data data);
unsigned char mcp7940_month(RTC_Data data);
unsigned char mcp7940_year(void);
unsigned char mcp7940_leapyear(void);
RTC_Status mcp7940_setweekday(unsigned char weekday);
RTC_Status mcp7940_settime(unsigned char hour, unsigned char minute, unsigned char second);
RTC_Status mcp7940_setdate(unsigned char day, unsigned char month, unsigned char year);
RTC_Status mcp7940_setoscillator(unsigned char trim);
#endif /* MCP7940_H_ */