RTC_NXP_Arduino 1.2.1
RTC driver device operation sample code for Arduino
Loading...
Searching...
No Matches
PCF85263A.cpp
1#include "RTC_NXP.h"
2
3PCF85263A::PCF85263A( uint8_t i2c_address ) : I2C_device( i2c_address )
4{
5}
6
7PCF85263A::PCF85263A( TwoWire& wire, uint8_t i2c_address ) : I2C_device( wire, i2c_address )
8{
9}
10
14
15void PCF85263A::begin( void )
16{
17}
18
19void PCF85263A::set( struct tm* now_tmp )
20{
21 // refer datasheet 7.2.6
22
23 time_t now_time;
24 struct tm* cnv_tmp;
25 uint8_t bf[ 10 ];
26
27 bf[ 0 ] = 0x01;
28 bf[ 1 ] = 0xA4;
29 bf[ 2 ] = 0;
30 bf[ 3 ] = dec2bcd( now_tmp->tm_sec );
31 bf[ 4 ] = dec2bcd( now_tmp->tm_min );
32 bf[ 5 ] = dec2bcd( now_tmp->tm_hour );
33 bf[ 6 ] = dec2bcd( now_tmp->tm_mday );
34 bf[ 8 ] = dec2bcd( now_tmp->tm_mon + 1 );
35 bf[ 9 ] = dec2bcd( now_tmp->tm_year - 100 );
36
37 now_time = mktime( now_tmp );
38 cnv_tmp = localtime( &now_time );
39 bf[ 5 ] = dec2bcd( cnv_tmp->tm_wday );
40
41 reg_w( Stop_enable, bf, sizeof( bf ) );
42 reg_w( Stop_enable, 0x00 );
43}
44
46{
47 return reg_r( Seconds ) & 0x80;
48}
49
50void PCF85263A::alarm( alarm_setting digit, int val )
51{
52 alarm( digit, val, 0 );
53}
54
55void PCF85263A::alarm( alarm_setting digit, int val, int int_sel )
56{
57 if ( WEEKDAY == digit ) {
58 Serial.println("*** 'WEEKDAY' for PCF85263A is unsupported alarm setting ***");
59 return;
60 }
61
62 set_alarm( digit, val, int_sel );
63}
64
65void PCF85263A::alarm( alarm_setting_85263A digit, int val, int int_sel )
66{
67 set_alarm( digit, val, int_sel );
68}
69
70void PCF85263A::set_alarm( int digit, int val, int int_sel )
71{
72 uint8_t en;
73
74 en = ((val & 80) ? 0x0 : 0x1) << digit;
75
76 reg_w( Second_alarm1 + digit, dec2bcd( val ) );
77 bit_op8( Alarm_enables, ~en, en );
78
79 uint8_t en_bit = (digit < 5) ? 0x10 : 0x80;
80 bit_op8( INTA_enable + int_sel, ~en_bit, en_bit );
81}
82
84{
85 reg_w( Alarm_enables, 0x00 );
86}
87
89{
90 bit_op8( INTA_enable, ~0x18, 0x18 );
91 bit_op8( INTB_enable, ~0x18, 0x18 );
92}
93
94uint8_t PCF85263A::int_clear( void )
95{
96 uint8_t status = reg_r( Flags );
97
98 reg_w( Flags, ~status );
99 return status;
100}
101
103{
104 struct tm now_tm;
105 uint8_t bf[ 8 ];
106
107 reg_r( _100th_seconds, bf, sizeof( bf ) );
108
109 now_tm.tm_sec = bcd2dec( bf[ 1 ] );
110 now_tm.tm_min = bcd2dec( bf[ 2 ] );
111 now_tm.tm_hour = bcd2dec( bf[ 3 ] );
112 now_tm.tm_mday = bcd2dec( bf[ 4 ] );
113 now_tm.tm_mon = bcd2dec( bf[ 6 ] ) - 1;
114 now_tm.tm_year = bcd2dec( bf[ 7 ] ) + 100;
115 now_tm.tm_isdst = 0;
116
117 return mktime( &now_tm );
118}
119
121{
122 bit_op8( Function, ~0x60, sel << 5 );
123 bit_op8( Function, ~0x60, sel << 5 );
124
125 bit_op8( INTA_enable + int_sel, ~0x40, (sel ? 1 : 0) << 6 );
126}
127
128void PCF85263A::pin_config( inta cfg_a, intb cfg_b )
129{
130 bit_op8(Pin_IO, ~0x0F, (cfg_b << 2) | cfg_a);
131}
132
133void PCF85263A::ts_config( int setting )
134{
135 bit_op8( Pin_IO, 0x0F, setting );
136 bit_op8( INTA_enable, ~0x04, 0x04 );
137}
138
139float PCF85263A::set_clock_offset( float ratio )
140{
141 const float ppm = ratio * 1.0e6f;
142 const float step_normal = 2.170f; // OFFM=0: correction triggered every 4 hours
143 const float step_fast = 2.0345f; // OFFM=1: correction triggered every 8 minutes
144
145 int v_normal = (int)(ppm / step_normal + (ppm < 0 ? -0.5f : 0.5f));
146 int v_fast = (int)(ppm / step_fast + (ppm < 0 ? -0.5f : 0.5f));
147
148 v_normal = (v_normal > 127) ? 127 : (v_normal < -128) ? -128 : v_normal;
149 v_fast = (v_fast > 127) ? 127 : (v_fast < -128) ? -128 : v_fast;
150
151 float err_normal = ppm - v_normal * step_normal;
152 float err_fast = ppm - v_fast * step_fast;
153
154 err_normal = (err_normal < 0) ? -err_normal : err_normal;
155 err_fast = (err_fast < 0) ? -err_fast : err_fast;
156
157 bool fast = err_fast < err_normal;
158 int v = fast ? v_fast : v_normal;
159
160 bit_op8( Oscillator, ~0x40, fast ? 0x40 : 0x00 );
161 reg_w( Offset, (uint8_t)v );
162
163 return v * (fast ? step_fast : step_normal) * 1.0e-6f;
164}
165
166time_t PCF85263A::timestamp( int num )
167{
168 struct tm ts_tm;
169 uint8_t bf[ 6 ];
170 const int offset = 6;
171
172 reg_r( TSR1_seconds + offset * num, bf, sizeof( bf ) );
173
174 ts_tm.tm_sec = bcd2dec( bf[ 0 ] );
175 ts_tm.tm_min = bcd2dec( bf[ 1 ] );
176 ts_tm.tm_hour = bcd2dec( bf[ 2 ] );
177 ts_tm.tm_mday = bcd2dec( bf[ 3 ] );
178 ts_tm.tm_mon = bcd2dec( bf[ 4 ] ) - 1;
179 ts_tm.tm_year = bcd2dec( bf[ 5 ] ) + 100;
180 ts_tm.tm_isdst = 0;
181
182 return mktime( &ts_tm );
183
184
185}
void alarm_disable(void)
Definition PCF85263A.cpp:88
void alarm(alarm_setting digit, int val)
Definition PCF85263A.cpp:50
void periodic_interrupt_enable(periodic_int_select sel, int int_sel=0)
uint8_t int_clear(void)
Definition PCF85263A.cpp:94
virtual ~PCF85263A()
Definition PCF85263A.cpp:11
time_t timestamp(int num)
void begin(void)
Definition PCF85263A.cpp:15
void ts_config(int setting)
alarm_setting_85263A
Definition RTC_NXP.h:1223
void reg_r(uint8_t reg_adr, uint8_t *data, int size)
bool oscillator_stop(void)
Definition PCF85263A.cpp:45
void bit_op8(uint8_t reg, uint8_t mask, uint8_t value)
PCF85263A(uint8_t i2c_address=(0xA2 > > 1))
Definition PCF85263A.cpp:3
void alarm_clear(void)
Definition PCF85263A.cpp:83
void pin_config(inta cfg_a, intb cfg_b)
void reg_w(uint8_t reg_adr, uint8_t *data, int size)
periodic_int_select
Definition RTC_NXP.h:1235
time_t rtc_time(void)
float set_clock_offset(float ratio)
void set(struct tm *now_tm)
Definition PCF85263A.cpp:19
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