2115 views|3 replies

55

Posts

0

Resources
The OP
 

Micropython TurnipBit Electronic Clock Teen Programming Primer [Copy link]

 
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. There is no need to connect any external display, 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.sans-serif], so let's learn it together.
Required components: One TurnipBit
One TurnipBitexpansion board
One DS3231clock module
Several DuPont lines
One USBdata line
Experimental principle:
Principle of DS3231clock module
DS3231 font="] (Figure 11-12) is a clock module, which is actually like an electronic clock. It has its own battery. When the time is set, the DS3231 will automatically keep time. The communication between the DS3231 and the development board is completed through the I2C interface. The I2C bus is a simple, bidirectional two-wire synchronous serial bus developed by Philips. It only requires SDA (serial data line) and The SCL (serial clock) line is used to transfer information between devices connected to the bus. Both SDA and SCL are bidirectional I/O lines. When connecting, just connect the SDA and SCL on the DS3231 to the SDA and SCL on the TurnipBit expansion board. Wiring method: The Turnipbit expansion board and the LED interface correspond to the following table: TurnipBit expansion board
[/td][td=151]
DS3231 clock module
[/td][/tr] [tr][td=129]
3V3
[/td][td=151]
VCC
[/td][/tr] [tr][td=129]
GND
[/td][td=151]
GND
[/td][/tr] [tr][td=129]
SCL
[/td][td=151]
SCL
[/td][/tr] [tr][td=129]
SDA
[/td][td=151]
SDA
[/td][/tr] [/table]
About the code, we don't need to assemble and program it here, the code is too long and it will take a while to assemble and program 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)

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)

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)

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)

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)

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)

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)

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)

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)








Latest reply

Although open source things can be used for free, the copyright information of the original author must be retained.  Details Published on 2018-3-16 21:14
 
 

1w

Posts

25

Resources
2
 
The DS3231 program is in the forum. https://bbs.eeworld.com.cn/thread-489440-1-1.html
 
 
 

55

Posts

0

Resources
3
 
dcexpert posted on 2018-3-15 12:38 The DS3231 program is in the forum. https://bbs.eeworld.com.cn/thread-489440-1-1.html
Isn't this open source?

Comments

Although open source materials can be used for free, the copyright information of the original author must be retained.  Details Published on 2018-3-16 21:14
 
 
 

1w

Posts

25

Resources
4
 
loktar posted on 2018-3-16 10:19 Isn't this open source?
Although open source things can be used for free, the copyright information of the original author must be retained.
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list