-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtc3231.c
155 lines (124 loc) · 3.19 KB
/
rtc3231.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
#include "rtc3231.h"
#include "i2c.h"
#define RTC_WADDR 0b11010000
#define RTC_RADDR 0b11010001
static unsigned char bcd (unsigned char data)
{
unsigned char bc;
bc = (data >> 4) * 10 + (data & 0x0f);
return bc;
}
static unsigned char bin(unsigned char dec)
{
char bcd = ((dec / 10) << 4) | (dec % 10);
return bcd;
}
void rtc3231_init(void)
{
if(i2c_start_condition())
{
i2c_send_byte(RTC_WADDR);
i2c_send_byte(0x0E);
i2c_send_byte(0x04); //desable 1Hz SQW pin
i2c_send_byte(0x00); //reset OSF flag, disable 32kHz output
i2c_stop_condition();
}
}
void rtc3231_read_time(struct rtc_time *time)
{
if(i2c_start_condition())
{
i2c_send_byte(RTC_WADDR);
i2c_send_byte(0x00);
i2c_stop_condition();
}
if(i2c_start_condition())
{
i2c_send_byte(RTC_RADDR);
time->sec = bcd(i2c_recv_byte());
time->min = bcd(i2c_recv_byte());
time->hour = bcd(i2c_recv_last_byte());
i2c_stop_condition();
}
}
void rtc3231_read_date(struct rtc_date *date)
{
if(i2c_start_condition())
{
i2c_send_byte(RTC_WADDR);
i2c_send_byte(0x03);
i2c_stop_condition();
}
if(i2c_start_condition())
{
i2c_send_byte(RTC_RADDR);
date->wday = bcd(i2c_recv_byte());
date->day = bcd(i2c_recv_byte());
date->month = bcd(i2c_recv_byte());
date->year = bcd(i2c_recv_last_byte());
i2c_stop_condition();
}
}
void rtc3231_read_datetime(struct rtc_time *time, struct rtc_date *date)
{
if(i2c_start_condition())
{
i2c_send_byte(RTC_WADDR);
i2c_send_byte(0x00);
i2c_stop_condition();
}
if(i2c_start_condition())
{
i2c_send_byte(RTC_RADDR);
time->sec = bcd(i2c_recv_byte());
time->min = bcd(i2c_recv_byte());
time->hour = bcd(i2c_recv_byte());
date->wday = bcd(i2c_recv_byte());
date->day = bcd(i2c_recv_byte());
date->month = bcd(i2c_recv_byte());
date->year = bcd(i2c_recv_last_byte());
i2c_stop_condition();
}
}
void rtc3231_read_temperature(struct temp_t *t)
{
if(i2c_start_condition())
{
i2c_send_byte(RTC_WADDR);
i2c_send_byte(0x11);
i2c_stop_condition();
if(i2c_start_condition())
{
i2c_send_byte(RTC_RADDR);
t->degrees = i2c_recv_byte();
uint8_t sub = i2c_recv_last_byte();
i2c_stop_condition();
t->subdegr = (25 * (sub >> 6)) % 100;
}
}
}
void rtc3231_write_time(struct rtc_time *time)
{
if(i2c_start_condition())
{
i2c_send_byte(RTC_WADDR);
i2c_send_byte(0x00);
i2c_send_byte(bin(time->sec));
i2c_send_byte(bin(time->min));
i2c_send_byte(bin(time->hour));
i2c_stop_condition();
}
}
void rtc3231_write_date(struct rtc_date *date)
{
if(i2c_start_condition())
{
i2c_send_byte(RTC_WADDR);
i2c_send_byte(0x03);
i2c_send_byte(bin(date->wday));
i2c_send_byte(bin(date->day));
i2c_send_byte(bin(date->month));
i2c_send_byte(bin(date->year));
i2c_stop_condition();
}
}