RTC_NXP_Arduino
1.1.2
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 )
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
_reg_w
( Timer_value, v );
95
_reg_w
( Timer_mode, tcf << 3 | 0x06 );
96
97
return
v * sf[tcf];
98
}
99
100
time_t
PCF85063_base::rtc_time
(
void
)
101
{
102
struct
tm now_tm;
103
104
uint8_t bf[ 7 ];
105
106
_reg_r
( Seconds, bf,
sizeof
( bf ) );
107
108
now_tm.tm_sec =
bcd2dec
( bf[ 0 ] );
109
now_tm.tm_min =
bcd2dec
( bf[ 1 ] );
110
now_tm.tm_hour =
bcd2dec
( bf[ 2 ] );
111
now_tm.tm_mday =
bcd2dec
( bf[ 3 ] );
112
now_tm.tm_mon =
bcd2dec
( bf[ 5 ] ) - 1;
113
now_tm.tm_year =
bcd2dec
( bf[ 6 ] ) + 100;
114
now_tm.tm_isdst = 0;
115
116
return
mktime(&now_tm);
117
}
118
119
120
PCF85063A::PCF85063A
( uint8_t i2c_address ) : I2C_device( i2c_address )
121
{
122
}
123
124
PCF85063A::PCF85063A
( TwoWire& wire, uint8_t i2c_address ) : I2C_device( wire, i2c_address )
125
{
126
}
127
128
PCF85063A::~PCF85063A
()
129
{
130
}
131
132
void
PCF85063A::_reg_w( uint8_t reg, uint8_t *vp,
int
len )
133
{
134
reg_w
( reg, vp, len );
135
}
136
137
void
PCF85063A::_reg_r( uint8_t reg, uint8_t *vp,
int
len )
138
{
139
reg_r
( reg, vp, len );
140
}
141
142
void
PCF85063A::_reg_w( uint8_t reg, uint8_t val )
143
{
144
reg_w
( reg, val );
145
}
146
147
uint8_t PCF85063A::_reg_r( uint8_t reg )
148
{
149
return
reg_r
( reg );
150
}
151
152
void
PCF85063A::_bit_op8( uint8_t reg, uint8_t mask, uint8_t val )
153
{
154
bit_op8
( reg, mask, val );
155
}
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::timer
float timer(float period)
Definition
PCF85063A.cpp:68
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::rtc_time
time_t rtc_time(void)
Definition
PCF85063A.cpp:100
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:120
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:128
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