Skip to content

Commit 95844ac

Browse files
committed
DS3231: Add calibration, temperature, and force_conversion
This device has a temperature sensor with 0.25C precision, and an "aging offset" which can be used to calibrate the crystal frequency for increased timekeeping accuracy. Expose these as properties. This requires adafruit/Adafruit_CircuitPython_Register#39 as the temperature and calibration registers hold signed values.
1 parent 974d5e6 commit 95844ac

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

adafruit_ds3231.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"""
5757
from adafruit_bus_device.i2c_device import I2CDevice
5858
from adafruit_register import i2c_bit
59+
from adafruit_register import i2c_bits
5960
from adafruit_register import i2c_bcd_alarm
6061
from adafruit_register import i2c_bcd_datetime
6162

@@ -93,6 +94,15 @@ class DS3231:
9394
alarm2_status = i2c_bit.RWBit(0x0F, 1)
9495
"""True if alarm2 is alarming. Set to False to reset."""
9596

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+
96106
def __init__(self, i2c):
97107
self.i2c_device = I2CDevice(i2c, 0x68)
98108

@@ -107,3 +117,27 @@ def datetime(self, value):
107117
self.datetime_register = value
108118
self.disable_oscillator = False
109119
self.lost_power = False
120+
121+
@property
122+
def temperature(self):
123+
"""Returns the last temperature measurement. Temperature is updated only every 64 seconds, or when a conversion is forced."""
124+
return self._temperature / 4
125+
126+
def force_conversion(self):
127+
"""Forces a conversion and returns the new temperature"""
128+
while self._busy:
129+
pass
130+
self._conv = True
131+
while self._busy:
132+
pass
133+
return self.temperature
134+
135+
@property
136+
def calibration(self):
137+
"""Calibration values range from -128 to 127; each step is approximately 0.1ppm, and positive values decrease the frequency (increase the period). When set, a temperature conversion is forced so the result of calibration can be seen directly at the 32kHz pin after the next temperature conversion."""
138+
return self._calibration
139+
140+
@calibration.setter
141+
def calibration(self, value):
142+
self._calibration = value
143+
self.force_conversion()

0 commit comments

Comments
 (0)