RTC_NXP_Arduino 1.2.1
RTC driver device operation sample code for Arduino
Loading...
Searching...
No Matches
PCF85063A.cpp
1#include "RTC_NXP.h"
2
6
10
12{
13}
14
15void 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
39{
40 return _reg_r( Seconds ) & 0x80;
41}
42
43void 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
51{
52 // will be implemented later
53}
54
56{
57 _bit_op8( Control_2, (uint8_t)(~0x80), 0x00 );
58}
59
61{
62 uint8_t v = _reg_r( Control_2 );
63 _reg_w( Control_2, v & ~0x48 );
64
65 return v;
66}
67
68float 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
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
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
153PCF85063A::PCF85063A( uint8_t i2c_address ) : I2C_device( i2c_address )
154{
155}
156
157PCF85063A::PCF85063A( TwoWire& wire, uint8_t i2c_address ) : I2C_device( wire, i2c_address )
158{
159}
160
164
165void PCF85063A::_reg_w( uint8_t reg, uint8_t *vp, int len )
166{
167 reg_w( reg, vp, len );
168}
169
170void PCF85063A::_reg_r( uint8_t reg, uint8_t *vp, int len )
171{
172 reg_r( reg, vp, len );
173}
174
175void PCF85063A::_reg_w( uint8_t reg, uint8_t val )
176{
177 reg_w( reg, val );
178}
179
180uint8_t PCF85063A::_reg_r( uint8_t reg )
181{
182 return reg_r( reg );
183}
184
185void PCF85063A::_bit_op8( uint8_t reg, uint8_t mask, uint8_t val )
186{
187 bit_op8( reg, mask, val );
188}
void set(struct tm *now_tm)
Definition PCF85063A.cpp:15
virtual void _bit_op8(uint8_t reg, uint8_t mask, uint8_t val)=0
bool oscillator_stop(void)
Definition PCF85063A.cpp:38
uint8_t int_clear(void)
Definition PCF85063A.cpp:60
void alarm_disable(void)
Definition PCF85063A.cpp:55
void alarm(alarm_setting digit, int val)
Definition PCF85063A.cpp:43
virtual void _reg_w(uint8_t reg, uint8_t *vp, int len)=0
void begin(void)
Definition PCF85063A.cpp:11
virtual void _reg_r(uint8_t reg, uint8_t *vp, int len)=0
virtual ~PCF85063_base()
Definition PCF85063A.cpp:7
void alarm_clear(void)
Definition PCF85063A.cpp:50
float timer(float period, bool pulse=false)
Definition PCF85063A.cpp:68
time_t rtc_time(void)
float set_clock_offset(float ratio)
void reg_r(uint8_t reg_adr, uint8_t *data, int size)
PCF85063A(uint8_t i2c_address=(0xA2 > > 1))
void reg_w(uint8_t reg_adr, uint8_t *data, int size)
void bit_op8(uint8_t reg, uint8_t mask, uint8_t value)
virtual ~PCF85063A()
alarm_setting
Definition RTC_NXP.h:35
static uint8_t dec2bcd(uint8_t v)
Definition RTC_NXP.cpp:24
static uint8_t bcd2dec(uint8_t v)
Definition RTC_NXP.cpp:19