Compare commits

...

3 commits

Author SHA1 Message Date
Jeff Epler
a7e535a9fb Improvements based on review 2020-07-19 11:24:31 -05:00
Jeff Epler
069c9c7e4a pylint 2020-07-16 13:27:33 -05:00
Jeff Epler
95844ac5fb 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
https://github.com/adafruit/Adafruit_CircuitPython_Register/pull/39 as the
temperature and calibration registers hold signed values.
2020-07-16 08:27:05 -05:00

View file

@ -56,6 +56,7 @@ Implementation Notes
"""
from adafruit_bus_device.i2c_device import I2CDevice
from adafruit_register import i2c_bit
from adafruit_register import i2c_bits
from adafruit_register import i2c_bcd_alarm
from adafruit_register import i2c_bcd_datetime
@ -93,6 +94,15 @@ class DS3231:
alarm2_status = i2c_bit.RWBit(0x0F, 1)
"""True if alarm2 is alarming. Set to False to reset."""
_calibration = i2c_bits.RWBits(8, 0x10, 0, 8, signed=True)
_temperature = i2c_bits.RWBits(
10, 0x11, 6, register_width=2, lsb_first=False, signed=True
)
_busy = i2c_bit.ROBit(0x0F, 2)
_conv = i2c_bit.RWBit(0x0E, 5)
def __init__(self, i2c):
self.i2c_device = I2CDevice(i2c, 0x68)
@ -107,3 +117,33 @@ class DS3231:
self.datetime_register = value
self.disable_oscillator = False
self.lost_power = False
@property
def temperature(self):
"""Returns the last temperature measurement. Temperature is updated
only every 64 seconds, or when a conversion is forced."""
return self._temperature / 4
def force_temperature_conversion(self):
"""Forces a conversion and returns the new temperature"""
while self._busy:
pass # Wait for any normal in-progress conversion to complete
self._conv = True
while self._conv:
pass # Wait for manual conversion request to complete
return self.temperature
@property
def calibration(self):
"""Calibrate the frequency of the crystal oscillator by adding or
removing capacitance. The datasheet calls this the Aging Offset.
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 immediately"""
return self._calibration
@calibration.setter
def calibration(self, value):
self._calibration = value
self.force_temperature_conversion()