RTC_NXP_Arduino
1.2.1
RTC driver device operation sample code for Arduino
Toggle main menu visibility
Loading...
Searching...
No Matches
src
PCF85063A.cpp
1
#include "RTC_NXP.h"
2
3
PCF85063_base::PCF85063_base
()
4
{
5
}
6
7
PCF85063_base::~PCF85063_base
()
8
{
9
}
10
11
void
PCF85063_base::begin
(
void
)
12
{
13
}
14
15
void
PCF85063_base::set
(
struct
tm* now_tmp )
16
{
17
time_t now_time;
18
struct
tm* cnv_tmp;
19
20
uint8_t bf[ 7 ];
21
22
bf[ 0 ] =
dec2bcd
( now_tmp->tm_sec );
23
bf[ 1 ] =
dec2bcd
( now_tmp->tm_min );
24
bf[ 2 ] =
dec2bcd
( now_tmp->tm_hour );
25
bf[ 3 ] =
dec2bcd
( now_tmp->tm_mday );
26
bf[ 5 ] =
dec2bcd
( now_tmp->tm_mon + 1 );
27
bf[ 6 ] =
dec2bcd
( now_tmp->tm_year - 100 );
28
29
now_time = mktime( now_tmp );
30
cnv_tmp = localtime( &now_time );
31
bf[ 4 ] =
dec2bcd
( cnv_tmp->tm_wday);
32
33
_bit_op8
( Control_1, ~0x20, 0x20 );
34
_reg_w
( Seconds, bf,
sizeof
( bf ) );
35
_bit_op8
( Control_1, ~0x20, 0x00 );
36
}
37
38
bool
PCF85063_base::oscillator_stop
(
void
)
39
{
40
return
_reg_r
( Seconds ) & 0x80;
41
}
42
43
void
PCF85063_base::alarm
(
alarm_setting
digit,
int
val )
44
{
45
int
v = (val == 0x80) ? 0x80 :
dec2bcd
( val );
46
_reg_w
( Second_alarm + digit, v );
47
_bit_op8
( Control_2, (uint8_t)(~0x80), 0x80 );
48
}
49
50
void
PCF85063_base::alarm_clear
(
void
)
51
{
52
// will be implemented later
53
}
54
55
void
PCF85063_base::alarm_disable
(
void
)
56
{
57
_bit_op8
( Control_2, (uint8_t)(~0x80), 0x00 );
58
}
59
60
uint8_t
PCF85063_base::int_clear
(
void
)
61
{
62
uint8_t v =
_reg_r
( Control_2 );
63
_reg_w
( Control_2, v & ~0x48 );
64
65
return
v;
66
}
67
68
float
PCF85063_base::timer
(
float
period,
bool
pulse )
69
{
70
float
sf[] = { 1 / 4096.0, 1 / 64.0, 1.0, 60 };
71
int
tcf;
72
73
period = ((255 * 60.0) < period) ? 255 * 60.0 : period;
74
75
for
( tcf = 0; tcf < 4; tcf++ ) {
76
if
( period <= sf[ tcf ] * 255 )
77
break
;
78
}
79
80
uint8_t v = (uint8_t)(period / sf[ tcf ]);
81
82
#if 0
83
Serial.print(
" period:"
);
84
Serial.print( period, 10 );
85
Serial.print(
" tcf:"
);
86
Serial.print( tcf );
87
Serial.print(
" v:"
);
88
Serial.print( v );
89
Serial.print(
" v*tcf:"
);
90
Serial.print( v * sf[tcf], 10 );
91
Serial.println(
""
);
92
#endif
93
94
#define TE 0x04
// timer enable
95
#define TIE 0x02
// timer interrupt enable
96
#define TI_TP 0x01
// timer interrupt mode 0=flag/1=pulse
97
98
99
uint8_t modes = TE | TIE | (pulse ? TI_TP : 0);
100
101
_reg_w
( Timer_value, v );
102
_reg_w
( Timer_mode, tcf << 3 | modes );
103
104
return
v * sf[tcf];
105
}
106
107
float
PCF85063_base::set_clock_offset
(
float
ratio )
108
{
109
const
float
ppm = ratio * 1.0e6f;
110
const
float
step_normal = 4.34f;
// MODE=0: correction once every two hours
111
const
float
step_coarse = 4.069f;
// MODE=1: correction once every 4 minutes
112
113
int
v_normal = (int)(ppm / step_normal + (ppm < 0 ? -0.5f : 0.5f));
114
int
v_coarse = (int)(ppm / step_coarse + (ppm < 0 ? -0.5f : 0.5f));
115
116
v_normal = (v_normal > 63) ? 63 : (v_normal < -64) ? -64 : v_normal;
117
v_coarse = (v_coarse > 63) ? 63 : (v_coarse < -64) ? -64 : v_coarse;
118
119
float
err_normal = ppm - v_normal * step_normal;
120
float
err_coarse = ppm - v_coarse * step_coarse;
121
122
err_normal = (err_normal < 0) ? -err_normal : err_normal;
123
err_coarse = (err_coarse < 0) ? -err_coarse : err_coarse;
124
125
bool
coarse = err_coarse < err_normal;
126
int
v = coarse ? v_coarse : v_normal;
127
128
_reg_w
( Offset, (uint8_t)((coarse ? 0x80 : 0x00) | (v & 0x7F)) );
129
130
return
v * (coarse ? step_coarse : step_normal) * 1.0e-6f;
131
}
132
133
time_t
PCF85063_base::rtc_time
(
void
)
134
{
135
struct
tm now_tm;
136
137
uint8_t bf[ 7 ];
138
139
_reg_r
( Seconds, bf,
sizeof
( bf ) );
140
141
now_tm.tm_sec =
bcd2dec
( bf[ 0 ] );
142
now_tm.tm_min =
bcd2dec
( bf[ 1 ] );
143
now_tm.tm_hour =
bcd2dec
( bf[ 2 ] );
144
now_tm.tm_mday =
bcd2dec
( bf[ 3 ] );
145
now_tm.tm_mon =
bcd2dec
( bf[ 5 ] ) - 1;
146
now_tm.tm_year =
bcd2dec
( bf[ 6 ] ) + 100;
147
now_tm.tm_isdst = 0;
148
149
return
mktime(&now_tm);
150
}
151
152
153
PCF85063A::PCF85063A
( uint8_t i2c_address ) : I2C_device( i2c_address )
154
{
155
}
156
157
PCF85063A::PCF85063A
( TwoWire& wire, uint8_t i2c_address ) : I2C_device( wire, i2c_address )
158
{
159
}
160
161
PCF85063A::~PCF85063A
()
162
{
163
}
164
165
void
PCF85063A::_reg_w( uint8_t reg, uint8_t *vp,
int
len )
166
{
167
reg_w
( reg, vp, len );
168
}
169
170
void
PCF85063A::_reg_r( uint8_t reg, uint8_t *vp,
int
len )
171
{
172
reg_r
( reg, vp, len );
173
}
174
175
void
PCF85063A::_reg_w( uint8_t reg, uint8_t val )
176
{
177
reg_w
( reg, val );
178
}
179
180
uint8_t PCF85063A::_reg_r( uint8_t reg )
181
{
182
return
reg_r
( reg );
183
}
184
185
void
PCF85063A::_bit_op8( uint8_t reg, uint8_t mask, uint8_t val )
186
{
187
bit_op8
( reg, mask, val );
188
}
PCF85063_base::set
void set(struct tm *now_tm)
Definition
PCF85063A.cpp:15
PCF85063_base::_bit_op8
virtual void _bit_op8(uint8_t reg, uint8_t mask, uint8_t val)=0
PCF85063_base::oscillator_stop
bool oscillator_stop(void)
Definition
PCF85063A.cpp:38
PCF85063_base::int_clear
uint8_t int_clear(void)
Definition
PCF85063A.cpp:60
PCF85063_base::alarm_disable
void alarm_disable(void)
Definition
PCF85063A.cpp:55
PCF85063_base::alarm
void alarm(alarm_setting digit, int val)
Definition
PCF85063A.cpp:43
PCF85063_base::PCF85063_base
PCF85063_base()
Definition
PCF85063A.cpp:3
PCF85063_base::_reg_w
virtual void _reg_w(uint8_t reg, uint8_t *vp, int len)=0
PCF85063_base::begin
void begin(void)
Definition
PCF85063A.cpp:11
PCF85063_base::_reg_r
virtual void _reg_r(uint8_t reg, uint8_t *vp, int len)=0
PCF85063_base::~PCF85063_base
virtual ~PCF85063_base()
Definition
PCF85063A.cpp:7
PCF85063_base::alarm_clear
void alarm_clear(void)
Definition
PCF85063A.cpp:50
PCF85063_base::timer
float timer(float period, bool pulse=false)
Definition
PCF85063A.cpp:68
PCF85063_base::rtc_time
time_t rtc_time(void)
Definition
PCF85063A.cpp:133
PCF85063_base::set_clock_offset
float set_clock_offset(float ratio)
Definition
PCF85063A.cpp:107
PCF85063A::reg_r
void reg_r(uint8_t reg_adr, uint8_t *data, int size)
PCF85063A::PCF85063A
PCF85063A(uint8_t i2c_address=(0xA2 > > 1))
Definition
PCF85063A.cpp:153
PCF85063A::reg_w
void reg_w(uint8_t reg_adr, uint8_t *data, int size)
PCF85063A::bit_op8
void bit_op8(uint8_t reg, uint8_t mask, uint8_t value)
PCF85063A::~PCF85063A
virtual ~PCF85063A()
Definition
PCF85063A.cpp:161
RTC_NXP::alarm_setting
alarm_setting
Definition
RTC_NXP.h:35
RTC_NXP::dec2bcd
static uint8_t dec2bcd(uint8_t v)
Definition
RTC_NXP.cpp:24
RTC_NXP::bcd2dec
static uint8_t bcd2dec(uint8_t v)
Definition
RTC_NXP.cpp:19
Generated by
1.17.0