RTC_NXP_Arduino 1.2.1
RTC driver device operation sample code for Arduino
Loading...
Searching...
No Matches
PCF85053A.cpp
1#include "RTC_NXP.h"
2
3PCF85053A::PCF85053A( uint8_t i2c_address ) : I2C_device( i2c_address ){}
4PCF85053A::PCF85053A( TwoWire& wire, uint8_t i2c_address ) : I2C_device( wire, i2c_address ){}
6
7void PCF85053A::begin( void ){}
8
9void PCF85053A::set( struct tm* now_tmp )
10{
11 time_t now_time;
12 struct tm* cnv_tmp;
13 uint8_t bf[ 10 ];
14
15 bf[ 0 ] = dec2bcd( now_tmp->tm_sec );
16 bf[ 1 ] = 0xFF; // Seconds_alarm clear
17 bf[ 2 ] = dec2bcd( now_tmp->tm_min );
18 bf[ 3 ] = 0xFF; // Minutes_alarm clear
19 bf[ 4 ] = dec2bcd( now_tmp->tm_hour );
20 bf[ 5 ] = 0xFF; // Hours_alarm clear
21 bf[ 7 ] = dec2bcd( now_tmp->tm_mday );
22 bf[ 8 ] = dec2bcd( now_tmp->tm_mon + 1 );
23 bf[ 9 ] = dec2bcd( now_tmp->tm_year - 100 );
24
25 now_time = mktime( now_tmp );
26 cnv_tmp = localtime( &now_time );
27 bf[ 6 ] = dec2bcd( cnv_tmp->tm_wday );
28
29 reg_w( Seconds, bf, sizeof( bf ) );
30}
31
33{
34 return reg_r( Status_Register ) & 0x40;
35 bit_op8( Status_Register, ~0x40, 0x40 );
36}
37
38void PCF85053A::alarm( alarm_setting digit, int val )
39{
40 reg_w( alarm_offsets[ digit ], dec2bcd( val ) );
41 bit_op8( Control_Register, ~0x08, 0x08 );
42}
43
45{
46 reg_w( alarm_offsets[ SECOND ], 0xFF );
47 reg_w( alarm_offsets[ MINUTE ], 0xFF );
48 reg_w( alarm_offsets[ HOUR ], 0xFF );
49}
50
52{
53 bit_op8( Control_Register, ~0x08, 0x00 );
54}
55
56uint8_t PCF85053A::int_clear( void )
57{
58 uint8_t status = reg_r( Status_Register );
59
60 reg_w( Status_Register, ~(status & 0xF0) );
61 return status;
62}
63
64time_t PCF85053A::rtc_time( void )
65{
66 struct tm now_tm;
67 uint8_t bf[ 10 ];
68
69 reg_r( Seconds, bf, sizeof( bf ) );
70
71 now_tm.tm_sec = bcd2dec( bf[ 0 ] );
72 now_tm.tm_min = bcd2dec( bf[ 2 ] );
73 now_tm.tm_hour = bcd2dec( bf[ 4 ] );
74 now_tm.tm_mday = bcd2dec( bf[ 7 ] );
75 now_tm.tm_mon = bcd2dec( bf[ 8 ] ) - 1;
76 now_tm.tm_year = bcd2dec( bf[ 9 ] ) + 100;
77 now_tm.tm_isdst = 0;
78
79 return mktime( &now_tm );
80}
81
82float PCF85053A::set_clock_offset( float ratio )
83{
84 const float ppm = ratio * 1.0e6f;
85 const float step_normal = 2.170f; // OFFM=0: correction triggered every 4 hours
86 const float step_fast = 2.0345f; // OFFM=1: correction triggered every 8 minutes
87
88 int v_normal = (int)(ppm / step_normal + (ppm < 0 ? -0.5f : 0.5f));
89 int v_fast = (int)(ppm / step_fast + (ppm < 0 ? -0.5f : 0.5f));
90
91 v_normal = (v_normal > 127) ? 127 : (v_normal < -128) ? -128 : v_normal;
92 v_fast = (v_fast > 127) ? 127 : (v_fast < -128) ? -128 : v_fast;
93
94 float err_normal = ppm - v_normal * step_normal;
95 float err_fast = ppm - v_fast * step_fast;
96
97 err_normal = (err_normal < 0) ? -err_normal : err_normal;
98 err_fast = (err_fast < 0) ? -err_fast : err_fast;
99
100 bool fast = err_fast < err_normal;
101 int v = fast ? v_fast : v_normal;
102
103 bit_op8( Oscillator, ~0x40, fast ? 0x40 : 0x00 );
104 reg_w( Offset, (uint8_t)v );
105
106 return v * (fast ? step_fast : step_normal) * 1.0e-6f;
107}
108
109int PCF85053A::alarm_offsets[ 3 ] = { 1, 3, 5 };
void begin(void)
Definition PCF85053A.cpp:7
void reg_w(uint8_t reg_adr, uint8_t *data, int size)
virtual ~PCF85053A()
Definition PCF85053A.cpp:5
void alarm_disable(void)
Definition PCF85053A.cpp:51
void bit_op8(uint8_t reg, uint8_t mask, uint8_t value)
void alarm_clear(void)
Definition PCF85053A.cpp:44
bool oscillator_stop(void)
Definition PCF85053A.cpp:32
void reg_r(uint8_t reg_adr, uint8_t *data, int size)
PCF85053A(uint8_t i2c_address=(0xDE > > 1))
Definition PCF85053A.cpp:3
uint8_t int_clear(void)
Definition PCF85053A.cpp:56
float set_clock_offset(float ratio)
Definition PCF85053A.cpp:82
time_t rtc_time(void)
Definition PCF85053A.cpp:64
void alarm(alarm_setting digit, int val)
Definition PCF85053A.cpp:38
void set(struct tm *now_tm)
Definition PCF85053A.cpp:9
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