Skip to content

Commit 5d81a9e

Browse files
authored
Merge pull request #27 from jepler/calibration-and-temperature
DS3231: Add calibration, temperature, and force_conversion
2 parents 974d5e6 + a7e535a commit 5d81a9e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

adafruit_ds3231.py

+40
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,33 @@ 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
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

Comments
 (0)