DS1306實時時鐘模組-micropython


DS1306實時時鐘模組-micropython

tags: micropython ds1302

這是廣華電子上的介紹
◎ 時鐘具有能計算2100 年之前的秒、分、時、日、星期、月、年的能力,還有閏年調整的能力
◎ 31 8 位暫存數據存儲RAM
◎ 串行 I/O 口方式使得管腳數量最少
◎ 寬範圍工作電壓2.0 5.5V
◎ 工作電流 2.0V 時,小於300nA
◎ 讀 / 寫時鐘或RAM 數據時有兩種傳送方式單字節傳送和多字節傳送字符組方式
◎ 8 腳DIP 封裝或可選的8 腳SOIC 封裝根據表面裝配
◎ 簡單 3 線接口
◎ 與 TTL 相容Vcc=5V
◎ 雙電源管用於主電源和備份電源供應
◎ 電池:CR2032 電壓3V,電流260mAh
◎ 晶振32.768KHz,匹配電容為6pF
◎ DS1302為8腳直插大晶片,晶片下面有IC座,方便更換及插拔片
6.模塊工作電壓兼容3.3V/5V,可與5V及3.3V單片機連接
◎帶4個定位孔,直徑3.1mm
◎工作溫度:0°~70°

◎ 安裝注意!
1.VCC與GND千萬不要接反,以免燒壞晶片
2.51系列單晶片P0接腳需要連接上拉電阻,如果單晶片沒有連接上拉電阻,可以將數據線接到其他接腳

步驟1:

下載sd1306.py並上傳到微控板上
https://github.com/omarbenhamid/micropython-ds1302-rtc

from machine import Pin

DS1302_REG_SECOND = (0x80)
DS1302_REG_MINUTE = (0x82)
DS1302_REG_HOUR   = (0x84)
DS1302_REG_DAY    = (0x86)
DS1302_REG_MONTH  = (0x88)
DS1302_REG_WEEKDAY= (0x8A)
DS1302_REG_YEAR   = (0x8C)
DS1302_REG_WP     = (0x8E)
DS1302_REG_CTRL   = (0x90)
DS1302_REG_RAM    = (0xC0)

class DS1302:
    def __init__(self, clk, dio, cs):
        self.clk = clk
        self.dio = dio
        self.cs = cs
        self.clk.init(Pin.OUT)
        self.cs.init(Pin.OUT)

    def _dec2hex(self, dat):
        return (dat//10) * 16 + (dat % 10)

    def _hex2dec(self, dat):
        return (dat//16) * 10 + (dat % 16)

    def _write_byte(self, dat):
        self.dio.init(Pin.OUT)
        for i in range(8):
            self.dio.value((dat >> i) & 1)
            self.clk.value(1)
            self.clk.value(0)

    def _read_byte(self):
        d = 0
        self.dio.init(Pin.IN)
        for i in range(8):
            d = d | (self.dio.value() << i)
            self.clk.value(1)
            self.clk.value(0)
        return d

    def _get_reg(self, reg):
        self.cs.value(1)
        self._write_byte(reg)
        t = self._read_byte()
        self.cs.value(0)
        return t

    def _set_reg(self, reg, dat):
        self.cs.value(1)
        self._write_byte(reg)
        self._write_byte(dat)
        self.cs.value(0)

    def _wr(self, reg, dat):
        self._set_reg(DS1302_REG_WP, 0)
        self._set_reg(reg, dat)
        self._set_reg(DS1302_REG_WP, 0x80)

    def start(self):
        t = self._get_reg(DS1302_REG_SECOND + 1)
        self._wr(DS1302_REG_SECOND, t & 0x7f)

    def stop(self):
        t = self._get_reg(DS1302_REG_SECOND + 1)
        self._wr(DS1302_REG_SECOND, t | 0x80)

    def second(self, second=None):
        if second == None:
            return self._hex2dec(self._get_reg(DS1302_REG_SECOND+1)) % 60
        else:
            self._wr(DS1302_REG_SECOND, self._dec2hex(second % 60))

    def minute(self, minute=None):
        if minute == None:
            return self._hex2dec(self._get_reg(DS1302_REG_MINUTE+1))
        else:
            self._wr(DS1302_REG_MINUTE, self._dec2hex(minute % 60))

    def hour(self, hour=None):
        if hour == None:
            return self._hex2dec(self._get_reg(DS1302_REG_HOUR+1))
        else:
            self._wr(DS1302_REG_HOUR, self._dec2hex(hour % 24))

    def weekday(self, weekday=None):
        if weekday == None:
            return self._hex2dec(self._get_reg(DS1302_REG_WEEKDAY+1))
        else:
            self._wr(DS1302_REG_WEEKDAY, self._dec2hex(weekday % 8))

    def day(self, day=None):
        if day == None:
            return self._hex2dec(self._get_reg(DS1302_REG_DAY+1))
        else:
            self._wr(DS1302_REG_DAY, self._dec2hex(day % 32))

    def month(self, month=None):
        if month == None:
            return self._hex2dec(self._get_reg(DS1302_REG_MONTH+1))
        else:
            self._wr(DS1302_REG_MONTH, self._dec2hex(month % 13))

    def year(self, year=None):
        if year == None:
            return self._hex2dec(self._get_reg(DS1302_REG_YEAR+1)) + 2000
        else:
            self._wr(DS1302_REG_YEAR, self._dec2hex(year % 100))

    def date_time(self, dat=None):
        if dat == None:
            return [self.year(), self.month(), self.day(), self.weekday(), self.hour(), self.minute(), self.second()]
        else:
            self.year(dat[0])
            self.month(dat[1])
            self.day(dat[2])
            self.weekday(dat[3])
            self.hour(dat[4])
            self.minute(dat[5])
            self.second(dat[6])

    def ram(self, reg, dat=None):
        if dat == None:
            return self._get_reg(DS1302_REG_RAM + 1 + (reg % 31)*2)
        else:
            self._wr(DS1302_REG_RAM + (reg % 31)*2, dat)

步驟2:

// DS1302 CLK/SCLK -->  5
// DS1302 DAT/IO -->  4
// DS1302 RST/CE -->  2
// DS1302 VCC -->  3.3v - 5v
// DS1302 GND -->  GND

步驟3:

上傳程式碼

import ds1302
from machine import PIN_WAKE, Pin
import time

ds = ds1302.DS1302(Pin(5), Pin(4), Pin(2))

while True:
    print("Today is {}/{}/{}".format(ds.year(), ds.month(), ds.day()))
    print("Time: {}:{}:{}".format(ds.hour(), ds.minute(), ds.second()))
    print("="*10)
    time.sleep(1)
Today is 2021/7/28
Time: 12:19:18
==========
Today is 2021/7/28
Time: 12:19:19
==========
Today is 2021/7/28
Time: 12:19:20
==========
Today is 2021/7/28
Time: 12:19:21
==========

步驟4:

再將ssd1306.py下載後並上傳到微控板

步驟5:

oled接線

步驟6:

import ds1302, ssd1306
from machine import PIN_WAKE, Pin
from machine import SoftI2C
import time
width = 128
height = 64
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
oled = ssd1306.SSD1306_I2C(width, height, i2c)
ds = ds1302.DS1302(Pin(5), Pin(4), Pin(2))

while True:
    oled.text(str(ds.year()), 0, 1)
    oled.text("/", 30, 1)
    oled.text(str(ds.month()), 40, 1)
    oled.text("/", 50, 1)
    oled.text(str(ds.day()), 60, 1)
    
    oled.text(str(ds.hour()), 30, 30)
    oled.text(":", 50, 30)
    oled.text(str(ds.minute()), 60, 30)
    oled.text(":", 80, 30)
    oled.text(str(ds.second()), 90, 30)
    oled.show()
    time.sleep(1)

文章作者: blairan
版權聲明: 本博客所有文章除特別聲明外,均採用 CC BY 4.0 許可協議。轉載請註明來源 blairan !
评论
  目錄