microbit 模拟时钟

使用microbit的屏幕显示模拟时钟,利用亮度表示秒。

 

makecode程序:

python程序

from microbit import *

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

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

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

    def write_byte(self, dat):
        for i in range(8):
            self.dio.write_digital((dat >> i) & 1)
            self.clk.write_digital(1)
            self.clk.write_digital(0)

    def read_byte(self):
        d = 0
        for i in range(8):
            d = d | (self.dio.read_digital()<<i)
            self.clk.write_digital(1)
            self.clk.write_digital(0)
        return d

    def getReg(self, reg):
        self.cs.write_digital(1)
        self.write_byte(reg)
        t = self.read_byte()
        self.cs.write_digital(0)
        return t

    def setReg(self, reg, dat):
        self.cs.write_digital(1)
        self.write_byte(reg)
        self.write_byte(dat)
        self.cs.write_digital(0)

    def wr(self, reg, dat):
        self.setReg(DS1302_REG_WP, 0)
        self.setReg(reg, dat)
        self.setReg(DS1302_REG_WP, 0x80)
                
    def start(self):
        t = self.getReg(DS1302_REG_SECOND + 1)
        self.wr(DS1302_REG_SECOND, t & 0x7f)

    def stop(self):
        t = self.getReg(DS1302_REG_SECOND + 1)
        self.wr(DS1302_REG_SECOND, t | 0x80)
        
    def Second(self, second = None):
        if second == None:
            return self.HexToDec(self.getReg(DS1302_REG_SECOND+1))%60
        else:
            self.wr(DS1302_REG_SECOND, self.DecToHex(second%60))

    def Minute(self, minute = None):
        if minute == None:
            return self.HexToDec(self.getReg(DS1302_REG_MINUTE+1))
        else:
            self.wr(DS1302_REG_MINUTE, self.DecToHex(minute%60))

    def Hour(self, hour = None):
        if hour == None:
            return self.HexToDec(self.getReg(DS1302_REG_HOUR+1))
        else:
            self.wr(DS1302_REG_HOUR, self.DecToHex(hour%24))

    def Weekday(self, weekday = None):
        if weekday == None:
            return self.HexToDec(self.getReg(DS1302_REG_WEEKDAY+1))
        else:
            self.wr(DS1302_REG_WEEKDAY, self.DecToHex(weekday%8))

    def Day(self, day = None):
        if day == None:
            return self.HexToDec(self.getReg(DS1302_REG_DAY+1))
        else:
            self.wr(DS1302_REG_DAY, self.DecToHex(day%32))

    def Month(self, month = None):
        if month == None:
            return self.HexToDec(self.getReg(DS1302_REG_MONTH+1))
        else:
            self.wr(DS1302_REG_MONTH, self.DecToHex(month%13))

    def Year(self, year = None):
        if year == None:
            return self.HexToDec(self.getReg(DS1302_REG_YEAR+1)) + 2000
        else:
            self.wr(DS1302_REG_YEAR, self.DecToHex(year%100))

mx=(2,3,4,4,4,3,2,1,0,0,0,1)
my=(0,0,1,2,3,4,4,4,3,2,1,0)

ds=DS1302(clk=pin13,dio=pin14,cs=pin15)
old = -1
while 1:
    s = ds.Second()
    if old != s:
        old = s
        m=ds.Minute()
        a=s//5
        b=s%5
        if b==4:
            display.set_pixel(mx[a],my[a],0)
        else:
            display.set_pixel(mx[a],my[a],b*3)
        if s%2:
            display.set_pixel(2, 2, 5)
        else:
            display.set_pixel(2, 2, 0)
    sleep(200)

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注