Micropython TurnipBit Electronic Clock Getting Started with Teen Programming

The electronic clock is a very common but very simple toy to make. For Micropython beginners, making an electronic clock is a very simple and easy experiment to test their learning results. Compared with other development boards, TurnipBit makes it easier to make an electronic clock. No external display is required. Because Turnipbit has its own display function, you only need to connect the Turnipbit expansion board to the clock module and write the corresponding code. , let's learn together.                    

Required components:

 TurnipBit One Piece

One TurnipBit expansion board

One DS3231 clock module

 Several Dupont lines

One USB cable

Experimental principle:

DS3231 clock module principle

DS3231 (Figure 11-12) is a clock module, which is actually like an electronic clock. It has its own battery. When the time is set, DS3231 will automatically run. The communication between the DS3231 and the development board is done through the I2C interface. The I2C bus is a simple, bidirectional two-wire synchronous serial bus developed by Philips. It only requires two lines, SDA (serial data line) and SCL (serial clock line), to transmit information between devices connected to the bus. Both SDA and SCL are bidirectional I/O lines. When connecting, you only need to connect SDA and SCL on DS3231 to SDA and SCL on the TurnipBit expansion board.

Wiring method:

The corresponding table of Turnipbit expansion board and LED interface is as follows:

  

TurnipBit Expansion Board

  

DS3231 Clock Module

3V3

VCC

GND

GND

SCL

SCL

SDA

SDA

Regarding the code, we don't need to insert programming here. If the code is too long, it will take a little time to insert it.

Source code:

from microbit import *

DS3231_ADDR       = 0x68
DS3231_REG_SEC    = b'\x00'
DS3231_REG_MIN    = b'\x01'
DS3231_REG_HOUR   = b'\x02'
DS3231_REG_DAY    = b'\x04'
DS3231_REG_MONTH  = b'\x05'
DS3231_REG_YEAR   = b'\x06'
DS3231_REG_TEMP   = b'\x11'
class DS3231(object):
   
    def __init__(self):
        pass
    def DATE(self, dat=[]):
        if dat==[]:
            t = []
            t.append(self.year())
            t.append(self.month())
            t.append(self.day())
            return t
        else:
            self.year(dat[0])
            self.month(dat[1])
            self.day(dat[2])
           
    def TIME(self, dat=[]):
        if dat==[]:
            t = []
            t.append(self.hour())
            t.append(self.min())
            t.append(self.sec())
            return t
        else:
            self.hour(dat[0])
            self.min(dat[1])
            self.sec(dat[2])
    def DateTime(self, dat=[]):
        if dat==[]:
            return self.DATE() + self.TIME()
        else:
            self.year(dat[0])
            self.month(dat[1])
            self.day(dat[2])
            self.hour(dat[3])
            self.min(dat[4])
            self.sec(dat[5])

    def dec2hex(self, dat):
        return (int(dat/10)<<4) + (dat%10)

    def setREG(self, dat,reg):
        buf = bytearray(2)
        buf[0] = reg[0]
        buf[1] = dat
        i2c.write(DS3231_ADDR,buf, repeat=False)
       
    def getREG_DEC(self,reg):
        i2c.write(DS3231_ADDR,reg)
        t = i2c.read(DS3231_ADDR,1)[0]
        #print("--===",t,"----------")
        return (t>>4)*10 + (t%16)

    def sec(self, sec=''):
        if sec == '':
            return self.getREG_DEC(DS3231_REG_SEC)
        else:
            self.setREG(self.dec2hex(sec), DS3231_REG_SEC)

    def min(self, min=''):
        if min == '':
            return self.getREG_DEC(DS3231_REG_MIN)
        else:
            self.setREG(self.dec2hex(min), DS3231_REG_MIN)

    def hour(self, hour=''):
        if hour=='':
            return self.getREG_DEC(DS3231_REG_HOUR)
        else:
            self.setREG(self.dec2hex(hour), DS3231_REG_HOUR)

    def day(self, day=''):
        if day=='':
            return self.getREG_DEC(DS3231_REG_DAY)
        else:
            self.setREG(self.dec2hex(day), DS3231_REG_DAY)

    def month(self, month=''):
        if month=='':
            return self.getREG_DEC(DS3231_REG_MONTH)
        else:
            self.setREG(self.dec2hex(month), DS3231_REG_MONTH)

    def year(self, year=''):
        if year=='':
            return self.getREG_DEC(DS3231_REG_YEAR)
        else:
            self.setREG(self.dec2hex(year), DS3231_REG_YEAR)

    def TEMP(self):
        i2c.write(DS3231_ADDR,DS3231_REG_TEMP, repeat=False)
        t1 = i2c.read(DS3231_ADDR,1, repeat=False)[0]
        i2c.write(DS3231_ADDR,b'\x12', repeat=False)
        t2 = i2c.read(DS3231_ADDR,1, repeat=False)[0]
        if t1>0x7F:
            return t1 - t2/256 -256
        else:
            return t1 + t2/256
ds=DS3231()
ds.DATE([17,10,24])
ds.TIME([10,03,00])
while True:
    Time=ds.TIME()
    Time_s=''
    for i in Time:
        Time_s+=str(i)+':'
    display.scroll(Time_s[:-1]+"-")
   
    #print(Time)
    #print(ds.TEMP())
    sleep(1000)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325197254&siteId=291194637