老邓的又一个小模块,DS3231。 这是一个为树莓派香蕉派扩展的高精度时钟模块。因为香蕉派有BUG,本身内部的RTC不能正常使用,所以如果想在断电后时间不丢失,需要插上一个这样的扩展模块。
近照
unit Uit_DS3231; {$mode objfpc}{$H+} interface uses Classes, SysUtils, TWI; const DS3231_WriteAddress 0xD0 //器件写地址 DS3231_ReadAddress 0xD1 //器件读地址 DS3231_SECOND 0x00 //秒 DS3231_MINUTE 0x01 //分 DS3231_HOUR 0x02 //时 DS3231_WEEK 0x03 //星期 DS3231_DAY 0x04 //日 DS3231_MONTH 0x05 //月 DS3231_YEAR 0x06 //年 DS3231_SALARM1ECOND 0x07 //秒 DS3231_ALARM1MINUTE 0x08 //分 DS3231_ALARM1HOUR 0x09 //时 DS3231_ALARM1WEEK 0x0A //星期/日 DS3231_ALARM2MINUTE 0x0b //分 DS3231_ALARM2HOUR 0x0c //时 DS3231_ALARM2WEEK 0x0d //星期/日 DS3231_CONTROL 0x0e //控制寄存器 DS3231_STATUS 0x0f //状态寄存器 BSY 2 //忙 OSF 7 //振荡器停止标志 DS3231_XTAL 0x10 //晶体老化寄存器 DS3231_TEMPERATUREH 0x11 //温度寄存器高字节(8位) DS3231_TEMPERATUREL 0x12 //温度寄存器低字节(高2位)
type TDS3231 = class private class var FInstance: TDS3231; class function GetInstance: TDS3231; static; public class procedure Release; class property Instance: TDS3231 read GetInstance; private FTWI0: TTWI; constructor Create; destructor Destroy; override; public function GetByte(Addr, Reg: Byte): Byte; end; implementation const TDS3231ADDR = $34; class function TDS3231.GetInstance: TDS3231; begin if FInstance = nil then FInstance:= TDS3231.Create; Result:= FInstance; end; class procedure TDS3231.Release; begin FreeAndNil(FInstance); end; (******************************************************************************) constructor TDS3231.Create; begin inherited Create; FTWI0:= TTWI.Create(TWI_0); end; destructor TDS3231.Destroy; begin FTWI0.Free; inherited Destroy; end; function TDS3231.GetByte(Addr, Reg: Byte): Byte; begin FTWI0.Read(TDS3231ADDR, Reg, Result); end; end.
|