56
56
"""
57
57
from adafruit_bus_device .i2c_device import I2CDevice
58
58
from adafruit_register import i2c_bit
59
+ from adafruit_register import i2c_bits
59
60
from adafruit_register import i2c_bcd_alarm
60
61
from adafruit_register import i2c_bcd_datetime
61
62
@@ -93,6 +94,15 @@ class DS3231:
93
94
alarm2_status = i2c_bit .RWBit (0x0F , 1 )
94
95
"""True if alarm2 is alarming. Set to False to reset."""
95
96
97
+ _calibration = i2c_bits .RWBits (8 , 0x10 , 0 , 8 , signed = True )
98
+
99
+ _temperature = i2c_bits .RWBits (
100
+ 10 , 0x11 , 6 , register_width = 2 , lsb_first = False , signed = True
101
+ )
102
+
103
+ _busy = i2c_bit .ROBit (0x0F , 2 )
104
+ _conv = i2c_bit .RWBit (0x0E , 5 )
105
+
96
106
def __init__ (self , i2c ):
97
107
self .i2c_device = I2CDevice (i2c , 0x68 )
98
108
@@ -107,3 +117,33 @@ def datetime(self, value):
107
117
self .datetime_register = value
108
118
self .disable_oscillator = False
109
119
self .lost_power = False
120
+
121
+ @property
122
+ def temperature (self ):
123
+ """Returns the last temperature measurement. Temperature is updated
124
+ only every 64 seconds, or when a conversion is forced."""
125
+ return self ._temperature / 4
126
+
127
+ def force_temperature_conversion (self ):
128
+ """Forces a conversion and returns the new temperature"""
129
+ while self ._busy :
130
+ pass # Wait for any normal in-progress conversion to complete
131
+ self ._conv = True
132
+ while self ._conv :
133
+ pass # Wait for manual conversion request to complete
134
+ return self .temperature
135
+
136
+ @property
137
+ def calibration (self ):
138
+ """Calibrate the frequency of the crystal oscillator by adding or
139
+ removing capacitance. The datasheet calls this the Aging Offset.
140
+ Calibration values range from -128 to 127; each step is approximately
141
+ 0.1ppm, and positive values decrease the frequency (increase the
142
+ period). When set, a temperature conversion is forced so the result of
143
+ calibration can be seen directly at the 32kHz pin immediately"""
144
+ return self ._calibration
145
+
146
+ @calibration .setter
147
+ def calibration (self , value ):
148
+ self ._calibration = value
149
+ self .force_temperature_conversion ()
0 commit comments