极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13063|回复: 0

[TPYBoard-Micropython之会python就能做硬件 3] 制作电子时钟

[复制链接]
发表于 2017-1-25 18:23:34 | 显示全部楼层 |阅读模式
转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi
欢迎加入讨论群 64770604

一、本次实验所需器材
1、TPYboard V102板  一块

2、DS3231模块   一块

3、NOKIA 5110 LCD 屏 一块

4、杜邦线:若干

===感谢某宝“萝卜城”提供的TPYboard V102豪华版套餐===

二、DS3231模块
1、什么是DS3231

      DS3231是低成本、高精度I2C实时时钟(RTC),具有集成的温补晶振(TCXO)和晶体。该器件包含电池输入端,断开主电源时仍可保持精确的计时。集成晶振提高了器件的长期精确度,并减少了生产线的元件数量。DS3231提供商用级和工业级温度范围,采用16引脚300mil的SO封装。RTC保存秒、分、时、星期、日期、月和年信息。少于31天的月份,将自动调整月末的日期,包括闰年的修正。时钟的工作格式可以是24小时或带/AM/PM指示的12小时格式。提供两个可设置的日历闹钟和一个可设置的方波输出。地址与数据通过I2C双向总线串行传输。

2、DS3231与TPYBoardv102的连接(IIC连接)

      DS3231与TPYBoard V102使用IIC连接方法,使用DS3231的SCL,SDA,VCC,GND四个针脚即可设定读出当前时间,对应如下表:


   这里IIC常用命令如下:

》  i2c.deinit(),解除I2C定义
》  i2c.init(mode, *, addr=0x12, baudrate=400000, gencall=False),初始化
       . mode,只能是 I2C.MASTER 或 I2C.SLAVE
       .addr,7位I2C地址
       .baudrate,时钟频率
       .gencall,通用调用模式
》 i2c.is_ready(addr),检测I2C设备是否响应,只对主模式有效
》 i2c.mem_read(data, addr, memaddr, *, timeout=5000, addr_size=8),读取数据
        .data,整数或者缓存
        .addr,设备地址
        . memaddr,内存地址
        . timeout,读取等待超时时间
        .addr_size,memaddr的大小。8位或16位
》 i2c.mem_write(data, addr, memaddr, *, timeout=5000, addr_size=8),写入数据,参数含义同上
》 i2c.recv(recv, addr=0x00, *, timeout=5000),从总线读取数据
        . recv,需要读取数据数量,或者缓冲区
        . addr,I2C地址
        . timeout,超时时间
》 i2c.send(send, addr=0x00, *, timeout=5000)
     . send,整数或者缓冲区
     . addr,I2C地址
     . timeout,超时时间
》 i2c.scan(),搜索I2C总线上设备。

三、调试DS3231模块
1、TPYBoard V102的调试

    如何能够时时对TPYBoard V102的输出进行调试和查看呢?这里需要用到putty工具。

第一步:将TPYBoard V102与pc通过usb线进行连接,然后通过设备管理器查看其对应的端口号。

第二步:打开putty,如下图进行填写。

第三步:点击确定,进入调试窗口,此时屏幕上显示"hello world",是因为main.py程序做了修改,否则不显示。

#main.py
  1. # main.py -- put your code here!
  2. import pyb
  3. print "hello world!!"
复制代码


第四步:这时就可以在命令行进行调试,常用的几个组合键如下:

Ctrl + C:终止程序

Ctrl + D:重新运行。

2、连接DS3231进行调试

      DS3231的连接方法见上文。

第一步:DS3231时间设定,以设定2017年1月25日为例。在TPYBoard V102里添加DS3231.py文件,修改main.py,具体代码如下:

#DS3231.py
  1. import pyb
  2. from pyb import I2C

  3. DS3231_ADDR       = const(0x68)
  4. DS3231_REG_SEC    = const(0x00)
  5. DS3231_REG_MIN    = const(0x01)
  6. DS3231_REG_HOUR   = const(0x02)
  7. DS3231_REG_WEEKDAY= const(0x03)
  8. DS3231_REG_DAY    = const(0x04)
  9. DS3231_REG_MONTH  = const(0x05)
  10. DS3231_REG_YEAR   = const(0x06)
  11. DS3231_REG_A1SEC  = const(0x07)
  12. DS3231_REG_A1MIN  = const(0x08)
  13. DS3231_REG_A1HOUR = const(0x09)
  14. DS3231_REG_A1DAY  = const(0x0A)
  15. DS3231_REG_A2MIN  = const(0x0B)
  16. DS3231_REG_A2HOUR = const(0x0C)
  17. DS3231_REG_A2DAY  = const(0x0D)
  18. DS3231_REG_CTRL   = const(0x0E)
  19. DS3231_REG_STA    = const(0x0F)
  20. DS3231_REG_OFF    = const(0x10)
  21. DS3231_REG_TEMP   = const(0x11)

  22. class ds3231(object):
  23.     def __init__(self, i2c_num):
  24.         self.i2c = I2C(i2c_num, I2C.MASTER, baudrate = 100000)

  25.     def DATE(self, dat=[]):
  26.         if dat==[]:
  27.             t = []
  28.             t.append(self.year())
  29.             t.append(self.month())
  30.             t.append(self.day())
  31.             return t
  32.         else:
  33.             self.year(dat[0])
  34.             self.month(dat[1])
  35.             self.day(dat[2])

  36.     def TIME(self, dat=[]):
  37.         if dat==[]:
  38.             t = []
  39.             t.append(self.hour())
  40.             t.append(self.min())
  41.             t.append(self.sec())
  42.             return t
  43.         else:
  44.             self.hour(dat[0])
  45.             self.min(dat[1])
  46.             self.sec(dat[2])

  47.     def DateTime(self, dat=[]):
  48.         if dat==[]:
  49.             return self.DATE() + self.TIME()
  50.         else:
  51.             self.year(dat[0])
  52.             self.month(dat[1])
  53.             self.day(dat[2])
  54.             self.hour(dat[3])
  55.             self.min(dat[4])
  56.             self.sec(dat[5])

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

  59.     def setREG(self, dat, reg):
  60.         buf = bytearray(2)
  61.         buf[0] = reg
  62.         buf[1] = dat
  63.         self.i2c.send(buf, DS3231_ADDR)
  64.          
  65.     def getREG_DEC(self, reg):
  66.         self.i2c.send(reg, DS3231_ADDR)
  67.         t = self.i2c.recv(1, DS3231_ADDR)[0]
  68.         return (t>>4)*10 + (t%16)

  69.     def sec(self, sec=''):
  70.         if sec == '':
  71.             return self.getREG_DEC(DS3231_REG_SEC)
  72.         else:
  73.             self.setREG(self.dec2hex(sec), DS3231_REG_SEC)

  74.     def min(self, min=''):
  75.         if min == '':
  76.             return self.getREG_DEC(DS3231_REG_MIN)
  77.         else:
  78.             self.setREG(self.dec2hex(min), DS3231_REG_MIN)

  79.     def hour(self, hour=''):
  80.         if hour=='':
  81.             return self.getREG_DEC(DS3231_REG_HOUR)
  82.         else:
  83.             self.setREG(self.dec2hex(hour), DS3231_REG_HOUR)

  84.     def day(self, day=''):
  85.         if day=='':
  86.             return self.getREG_DEC(DS3231_REG_DAY)
  87.         else:
  88.             self.setREG(self.dec2hex(day), DS3231_REG_DAY)

  89.     def month(self, month=''):
  90.         if month=='':
  91.             return self.getREG_DEC(DS3231_REG_MONTH)
  92.         else:
  93.             self.setREG(self.dec2hex(month), DS3231_REG_MONTH)

  94.     def year(self, year=''):
  95.         if year=='':
  96.             return self.getREG_DEC(DS3231_REG_YEAR)
  97.         else:
  98.             self.setREG(self.dec2hex(year), DS3231_REG_YEAR)

  99.     def TEMP(self):
  100.         self.i2c.send(DS3231_REG_TEMP, DS3231_ADDR)
  101.         t1 = self.i2c.recv(1, DS3231_ADDR)[0]
  102.         self.i2c.send(DS3231_REG_TEMP+1, DS3231_ADDR)
  103.         t2 = self.i2c.recv(1, DS3231_ADDR)[0]
  104.         if t1>0x7F:
  105.             return t1 - t2/256 -256
  106.         else:
  107.             return t1 + t2/256
复制代码

#main.py
  1. # main.py -- put your code here!
  2. from ds3231 import ds3231
  3. ds=ds3231(1)
  4. ds.DATE([17,01,25])
  5. ds.TIME([10,26,30])
  6. while True:
  7.     ds.TEMP()
  8.     print('Tem:',ds.TEMP())
  9.     print('Time:',ds.DateTime())
  10.     pyb.delay(1000)
复制代码

第二步:按一下RST键,此时DS3231的时间已经设定为2017年1月25日,具体见图:

四、利用DS3231和5110制作电子时钟
5110的使用方法请见:http://www.cnblogs.com/xiaowuyi/p/6347336.html

1、连接方法

(注意与上面不同,用的是TPYBoard的第二个I2C口)

2、程序原代码

      该程序涉及文件5个,分别为boot.py、main.py、、chinese.py、font.py、DS3231.py、upcd8544.py,具体代码如下:

#boot.py

//转载请注明:@小五义http://www.cnblogs.com/xiaowuyi  QQ群:64770604
  1. # boot.py -- run on boot-up
  2. # can run arbitrary Python, but best to keep it minimal

  3. import machine
  4. import pyb
  5. pyb.main('main.py') # main script to run after this one
  6. #pyb.usb_mode('CDC+MSC') # act as a serial and a storage device
  7. #pyb.usb_mode('CDC+HID') # act as a serial device and a mouse
复制代码

#main.py
  1. # main.py -- put your code here!
  2. import pyb
  3. import upcd8544
  4. from machine import SPI,Pin
  5. from DS3231 import DS3231

  6. ds=DS3231(1) #定义DS3231,用DS3231(2)的话就要选择IIC2

  7. # 用于设定时间和日期
  8. def setDateTime(year,month,day,time,minutes,seconds):
  9.     ds.DATE([year,month,day])
  10.     ds.TIME([time,minutes,seconds])
  11.   
  12. # 在LCD5110 显示时间或日期,separator 中间的分割符
  13. # x,y 在LCD5110 显示的位置
  14. def showTimeOrDate(why,x,y,separator=':'):
  15.     # [HH,MM,SS] >> HH:MM:SS
  16.     why = why.replace('[','')
  17.     why = why.replace(']','')
  18.     why = why.replace(',',separator)
  19.     print(why)
  20.     lcd_5110.lcd_write_string(why,x,y)


  21. def main():
  22.     lcd_5110.lcd_write_chinese('萝',14,0)
  23.     lcd_5110.lcd_write_chinese('卜',30,0)
  24.     lcd_5110.lcd_write_chinese('智',46,0)
  25.     lcd_5110.lcd_write_chinese('能',62,0)
  26.     lcd_5110.lcd_write_string('TEM:',14,2)
  27.     lcd_5110.lcd_write_string(str(ds.TEMP()),44,2)
  28.     lcd_5110.lcd_write_chinese("当",14,3)
  29.     lcd_5110.lcd_write_chinese("前",30,3)
  30.     lcd_5110.lcd_write_chinese("时",46,3)
  31.     lcd_5110.lcd_write_chinese("间",62,3)
  32.     showTimeOrDate(str(ds.TIME()),14,5)
  33.     print(str(ds.TIME()))
  34.     pyb.delay(1000)

  35. if __name__ == '__main__':
  36.     #setDateTime(2016,12,27,13,17,00)#设置时间
  37.     ds.DATE()
  38.     SPI = pyb.SPI(1) #DIN=>X8-MOSI/CLK=>X6-SCK
  39.     #DIN =>SPI(1).MOSI 'X8' data flow (Master out, Slave in)
  40.     #CLK =>SPI(1).SCK  'X6' SPI clock
  41.     RST    = pyb.Pin('X1')
  42.     CE     = pyb.Pin('X2')
  43.     DC     = pyb.Pin('X3')
  44.     LIGHT  = pyb.Pin('X4')
  45.     lcd_5110 = upcd8544.PCD8544(SPI, RST, CE, DC, LIGHT)
  46.     while(1):
  47.      main()
复制代码


#font.py

//转载请注明:@小五义http://www.cnblogs.com/xiaowuyi  QQ群:64770604
  1. class FONT6_8:
  2.     """docstring for FONT6_8"""
  3.     FONTTYPE6_8 = [
  4.         [0x00, 0x00, 0x00, 0x00, 0x00, 0x00] # 20
  5.         ,[0x00, 0x00, 0x00, 0x5f, 0x00, 0x00] # 21 !
  6.         ,[0x00, 0x00, 0x07, 0x00, 0x07, 0x00] # 22 "
  7.         ,[0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14] # 23 #
  8.         ,[0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12] # 24 $
  9.         ,[0x00, 0x23, 0x13, 0x08, 0x64, 0x62] # 25 %
  10.         ,[0x00, 0x36, 0x49, 0x55, 0x22, 0x50] # 26 &
  11.         ,[0x00, 0x00, 0x05, 0x03, 0x00, 0x00] # 27 '
  12.         ,[0x00, 0x00, 0x1c, 0x22, 0x41, 0x00] # 28 (
  13.         ,[0x00, 0x00, 0x41, 0x22, 0x1c, 0x00] # 29 )
  14.         ,[0x00, 0x14, 0x08, 0x3e, 0x08, 0x14] # 2a *
  15.         ,[0x00, 0x08, 0x08, 0x3e, 0x08, 0x08] # 2b +
  16.         ,[0x00, 0x00, 0x50, 0x30, 0x00, 0x00] # 2c ,
  17.         ,[0x00, 0x08, 0x08, 0x08, 0x08, 0x08] # 2d -
  18.         ,[0x00, 0x00, 0x60, 0x60, 0x00, 0x00] # 2e .
  19.         ,[0x00, 0x20, 0x10, 0x08, 0x04, 0x02] # 2f /
  20.         ,[0x00, 0x3e, 0x51, 0x49, 0x45, 0x3e] # 30 0
  21.         ,[0x00, 0x00, 0x42, 0x7f, 0x40, 0x00] # 31 1
  22.         ,[0x00, 0x42, 0x61, 0x51, 0x49, 0x46] # 32 2
  23.         ,[0x00, 0x21, 0x41, 0x45, 0x4b, 0x31] # 33 3
  24.         ,[0x00, 0x18, 0x14, 0x12, 0x7f, 0x10] # 34 4
  25.         ,[0x00, 0x27, 0x45, 0x45, 0x45, 0x39] # 35 5
  26.         ,[0x00, 0x3c, 0x4a, 0x49, 0x49, 0x30] # 36 6
  27.         ,[0x00, 0x01, 0x71, 0x09, 0x05, 0x03] # 37 7
  28.         ,[0x00, 0x36, 0x49, 0x49, 0x49, 0x36] # 38 8
  29.         ,[0x00, 0x06, 0x49, 0x49, 0x29, 0x1e] # 39 9
  30.         ,[0x00, 0x00, 0x36, 0x36, 0x00, 0x00] # 3a :
  31.         ,[0x00, 0x00, 0x56, 0x36, 0x00, 0x00] # 3b ;
  32.         ,[0x00, 0x08, 0x14, 0x22, 0x41, 0x00] # 3c <
  33.         ,[0x00, 0x14, 0x14, 0x14, 0x14, 0x14] # 3d =
  34.         ,[0x00, 0x00, 0x41, 0x22, 0x14, 0x08] # 3e >
  35.         ,[0x00, 0x02, 0x01, 0x51, 0x09, 0x06] # 3f ?
  36.         ,[0x00, 0x32, 0x49, 0x79, 0x41, 0x3e] # 40 @
  37.         ,[0x00, 0x7e, 0x11, 0x11, 0x11, 0x7e] # 41 A
  38.         ,[0x00, 0x7f, 0x49, 0x49, 0x49, 0x36] # 42 B
  39.         ,[0x00, 0x3e, 0x41, 0x41, 0x41, 0x22] # 43 C
  40.         ,[0x00, 0x7f, 0x41, 0x41, 0x22, 0x1c] # 44 D
  41.         ,[0x00, 0x7f, 0x49, 0x49, 0x49, 0x41] # 45 E
  42.         ,[0x00, 0x7f, 0x09, 0x09, 0x09, 0x01] # 46 F
  43.         ,[0x00, 0x3e, 0x41, 0x49, 0x49, 0x7a] # 47 G
  44.         ,[0x00, 0x7f, 0x08, 0x08, 0x08, 0x7f] # 48 H
  45.         ,[0x00, 0x00, 0x41, 0x7f, 0x41, 0x00] # 49 I
  46.         ,[0x00, 0x20, 0x40, 0x41, 0x3f, 0x01] # 4a J
  47.         ,[0x00, 0x7f, 0x08, 0x14, 0x22, 0x41] # 4b K
  48.         ,[0x00, 0x7f, 0x40, 0x40, 0x40, 0x40] # 4c L
  49.         ,[0x00, 0x7f, 0x02, 0x0c, 0x02, 0x7f] # 4d M
  50.         ,[0x00, 0x7f, 0x04, 0x08, 0x10, 0x7f] # 4e N
  51.         ,[0x00, 0x3e, 0x41, 0x41, 0x41, 0x3e] # 4f O
  52.         ,[0x00, 0x7f, 0x09, 0x09, 0x09, 0x06] # 50 P
  53.         ,[0x00, 0x3e, 0x41, 0x51, 0x21, 0x5e] # 51 Q
  54.         ,[0x00, 0x7f, 0x09, 0x19, 0x29, 0x46] # 52 R
  55.         ,[0x00, 0x46, 0x49, 0x49, 0x49, 0x31] # 53 S
  56.         ,[0x00, 0x01, 0x01, 0x7f, 0x01, 0x01] # 54 T
  57.         ,[0x00, 0x3f, 0x40, 0x40, 0x40, 0x3f] # 55 U
  58.         ,[0x00, 0x1f, 0x20, 0x40, 0x20, 0x1f] # 56 V
  59.         ,[0x00, 0x3f, 0x40, 0x38, 0x40, 0x3f] # 57 W
  60.         ,[0x00, 0x63, 0x14, 0x08, 0x14, 0x63] # 58 X
  61.         ,[0x00, 0x07, 0x08, 0x70, 0x08, 0x07] # 59 Y
  62.         ,[0x00, 0x61, 0x51, 0x49, 0x45, 0x43] # 5a Z
  63.         ,[0x00, 0x00, 0x7f, 0x41, 0x41, 0x00] # 5b [
  64.         ,[0x00, 0x02, 0x04, 0x08, 0x10, 0x20] # 5c \ #
  65.         ,[0x00, 0x00, 0x41, 0x41, 0x7f, 0x00] # 5d ]
  66.         ,[0x00, 0x04, 0x02, 0x01, 0x02, 0x04] # 5e ^
  67.         ,[0x00, 0x40, 0x40, 0x40, 0x40, 0x40] # 5f _
  68.         ,[0x00, 0x00, 0x01, 0x02, 0x04, 0x00] # 60 `
  69.         ,[0x00, 0x20, 0x54, 0x54, 0x54, 0x78] # 61 a
  70.         ,[0x00, 0x7f, 0x48, 0x44, 0x44, 0x38] # 62 b
  71.         ,[0x00, 0x38, 0x44, 0x44, 0x44, 0x20] # 63 c
  72.         ,[0x00, 0x38, 0x44, 0x44, 0x48, 0x7f] # 64 d
  73.         ,[0x00, 0x38, 0x54, 0x54, 0x54, 0x18] # 65 e
  74.         ,[0x00, 0x08, 0x7e, 0x09, 0x01, 0x02] # 66 f
  75.         ,[0x00, 0x0c, 0x52, 0x52, 0x52, 0x3e] # 67 g
  76.         ,[0x00, 0x7f, 0x08, 0x04, 0x04, 0x78] # 68 h
  77.         ,[0x00, 0x00, 0x44, 0x7d, 0x40, 0x00] # 69 i
  78.         ,[0x00, 0x20, 0x40, 0x44, 0x3d, 0x00] # 6a j
  79.         ,[0x00, 0x7f, 0x10, 0x28, 0x44, 0x00] # 6b k
  80.         ,[0x00, 0x00, 0x41, 0x7f, 0x40, 0x00] # 6c l
  81.         ,[0x00, 0x7c, 0x04, 0x18, 0x04, 0x78] # 6d m
  82.         ,[0x00, 0x7c, 0x08, 0x04, 0x04, 0x78] # 6e n
  83.         ,[0x00, 0x38, 0x44, 0x44, 0x44, 0x38] # 6f o
  84.         ,[0x00, 0x7c, 0x14, 0x14, 0x14, 0x08] # 70 p
  85.         ,[0x00, 0x08, 0x14, 0x14, 0x18, 0x7c] # 71 q
  86.         ,[0x00, 0x7c, 0x08, 0x04, 0x04, 0x08] # 72 r
  87.         ,[0x00, 0x48, 0x54, 0x54, 0x54, 0x20] # 73 s
  88.         ,[0x00, 0x04, 0x3f, 0x44, 0x40, 0x20] # 74 t
  89.         ,[0x00, 0x3c, 0x40, 0x40, 0x20, 0x7c] # 75 u
  90.         ,[0x00, 0x1c, 0x20, 0x40, 0x20, 0x1c] # 76 v
  91.         ,[0x00, 0x3c, 0x40, 0x30, 0x40, 0x3c] # 77 w
  92.         ,[0x00, 0x44, 0x28, 0x10, 0x28, 0x44] # 78 x
  93.         ,[0x00, 0x0c, 0x50, 0x50, 0x50, 0x3c] # 79 y
  94.         ,[0x00, 0x44, 0x64, 0x54, 0x4c, 0x44] # 7a z
  95.         ,[0x00, 0x00, 0x08, 0x36, 0x41, 0x00] # 7b [
  96.         ,[0x00, 0x00, 0x00, 0x7f, 0x00, 0x00] # 7c |
  97.         ,[0x00, 0x00, 0x41, 0x36, 0x08, 0x00] # 7d ]
  98.         ,[0x00, 0x10, 0x08, 0x08, 0x10, 0x08] # 7e ~
  99.         ,[0x00, 0x78, 0x46, 0x41, 0x46, 0x78] # 7f (delete)
  100.     ]
  101.          
  102.     def get_font6_8(self, data):
  103.         return self.FONTTYPE6_8[bytearray(data)[0] - 0x20]
复制代码


#chinese.py
  1. class CN_UTF8:
  2.     """docstring for CN_UTF8"""
  3.     #key:values
  4.     #key: 使用汉字的UTF-8码
  5.     #values: 16*16
  6.     #   [0] 8*16 上半部分
  7.     #   [1] 8*16 下半部分
  8.     UTF8_CHINESE = {
  9.         0xe6aca2:[
  10.                     [0x04,0x24,0x44,0x84,0x64,0x9C,0x40,0x30,0x0F,0xC8,0x08,0x08,0x28,0x18,0x00,0x00],
  11.                     [0x10,0x08,0x06,0x01,0x82,0x4C,0x20,0x18,0x06,0x01,0x06,0x18,0x20,0x40,0x80,0x00]
  12.                 ],#欢
  13.         0xe8bf8e:[
  14.                     [0x40,0x40,0x42,0xCC,0x00,0x00,0xFC,0x04,0x02,0x00,0xFC,0x04,0x04,0xFC,0x00,0x00],
  15.                     [0x00,0x40,0x20,0x1F,0x20,0x40,0x4F,0x44,0x42,0x40,0x7F,0x42,0x44,0x43,0x40,0x00]
  16.                 ],#迎
  17.         0xe4bdbf:[
  18.                     [0x80,0x60,0xF8,0x07,0x04,0xE4,0x24,0x24,0x24,0xFF,0x24,0x24,0x24,0xE4,0x04,0x00],
  19.                     [0x00,0x00,0xFF,0x00,0x80,0x81,0x45,0x29,0x11,0x2F,0x41,0x41,0x81,0x81,0x80,0x00]
  20.                 ],#使
  21.         0xe794a8:[
  22.                     [0x00,0x00,0xFE,0x22,0x22,0x22,0x22,0xFE,0x22,0x22,0x22,0x22,0xFE,0x00,0x00,0x00],
  23.                     [0x80,0x60,0x1F,0x02,0x02,0x02,0x02,0x7F,0x02,0x02,0x42,0x82,0x7F,0x00,0x00,0x00]
  24.                 ],#用
  25.         0xe78eb0:[
  26.                     [0x04,0x84,0x84,0xFC,0x84,0x84,0x00,0xFE,0x02,0x02,0xF2,0x02,0x02,0xFE,0x00,0x00],
  27.                     [0x20,0x60,0x20,0x1F,0x10,0x90,0x40,0x23,0x18,0x06,0x01,0x7E,0x80,0x83,0xE0,0x00]
  28.                 ],#现
  29.         0xe59ca8:[
  30.                     [0x08,0x08,0x88,0xC8,0x38,0x0C,0x0B,0x08,0x08,0xE8,0x08,0x08,0x08,0x08,0x08,0x00],
  31.                     [0x02,0x01,0x00,0xFF,0x40,0x41,0x41,0x41,0x41,0x7F,0x41,0x41,0x41,0x41,0x40,0x00]
  32.                 ],#在
  33.         0xe697b6:[
  34.                     [0x00,0xFC,0x84,0x84,0x84,0xFC,0x00,0x10,0x10,0x10,0x10,0x10,0xFF,0x10,0x10,0x00],
  35.                     [0x00,0x3F,0x10,0x10,0x10,0x3F,0x00,0x00,0x01,0x06,0x40,0x80,0x7F,0x00,0x00,0x00]
  36.                 ],#时
  37.         0xe997b4:[
  38.                     [0x00,0xF8,0x01,0x06,0x00,0xF0,0x12,0x12,0x12,0xF2,0x02,0x02,0x02,0xFE,0x00,0x00],
  39.                     [0x00,0xFF,0x00,0x00,0x00,0x1F,0x11,0x11,0x11,0x1F,0x00,0x40,0x80,0x7F,0x00,0x00]
  40.                 ], #间   
  41.         0xe68891:[
  42.                     [0x20,0x24,0x24,0x24,0xFE,0x23,0x22,0x20,0x20,0xFF,0x20,0x22,0x2C,0xA0,0x20,0x00],
  43.                     [0x00,0x08,0x48,0x84,0x7F,0x02,0x41,0x40,0x20,0x13,0x0C,0x14,0x22,0x41,0xF8,0x00]
  44.                 ], #我
  45.         0xe8909d:[
  46.                     [0x02,0x02,0xF2,0x92,0x97,0xF2,0x92,0x92,0x92,0xF2,0x97,0x92,0xF2,0x02,0x02,0x00],
  47.                     [0x00,0x80,0x88,0x88,0x44,0x4A,0x53,0x22,0x22,0x12,0x0A,0x06,0x00,0x00,0x00,0x00]
  48.                 ], #萝
  49.         0xe58d9c:[
  50.                     [0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00],
  51.                     [0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00]
  52.                 ], #卜
  53.         0xe699ba:[
  54.                     [0x10,0x94,0x53,0x32,0x1E,0x32,0x52,0x10,0x00,0x7E,0x42,0x42,0x42,0x7E,0x00,0x00],
  55.                     [0x00,0x00,0x00,0xFF,0x49,0x49,0x49,0x49,0x49,0x49,0x49,0xFF,0x00,0x00,0x00,0x00]
  56.                 ], #智
  57.         0xe883bd:[
  58.                     [0x08,0xCC,0x4A,0x49,0x48,0x4A,0xCC,0x18,0x00,0x7F,0x88,0x88,0x84,0x82,0xE0,0x00],
  59.                     [0x00,0xFF,0x12,0x12,0x52,0x92,0x7F,0x00,0x00,0x7E,0x88,0x88,0x84,0x82,0xE0,0x00]
  60.                 ],#能
  61.         0xe682a8:[
  62.                     [0x20,0x10,0x08,0xFC,0x23,0x10,0x88,0x67,0x04,0xF4,0x04,0x24,0x54,0x8C,0x00,0x00],
  63.                     [0x40,0x30,0x00,0x77,0x80,0x81,0x88,0xB2,0x84,0x83,0x80,0xE0,0x00,0x11,0x60,0x00]
  64.                 ],#您
  65.         0xe5bd93:[
  66.                     [0x00,0x40,0x42,0x44,0x58,0x40,0x40,0x7F,0x40,0x40,0x50,0x48,0xC6,0x00,0x00,0x00],
  67.                     [0x00,0x40,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0xFF,0x00,0x00,0x00]
  68.                 ],#当
  69.         0xE5898D:[
  70.                     [0x08,0x08,0xE8,0x29,0x2E,0x28,0xE8,0x08,0x08,0xC8,0x0C,0x0B,0xE8,0x08,0x08,0x00],
  71.                     [0x00,0x00,0xFF,0x09,0x49,0x89,0x7F,0x00,0x00,0x0F,0x40,0x80,0x7F,0x00,0x00,0x00]
  72.                 ]#前
  73.    
  74.                  
  75.          

  76.     }
  77.     #key 汉字的UTF-8码
  78.     #isBottom 确定这次是获取 某个字的 上半部分(0) 还是下半部分(1)
  79.     def get_chinese_utf8(self, key,isBottom = 0):
  80.         values = self.UTF8_CHINESE[key]
  81.         return values[isBottom]
复制代码

#upcd8544.py
  1. # -*- coding: utf-8 -*-
  2. """
  3. MicroPython PCD8544 driver
  4. (for Nokia 5110 displays)
  5. """


  6. try:
  7.     import pyb as machine
  8. except:
  9.     # WiPy
  10.     import machine
  11.      
  12. import sys
  13. import struct
  14. import time
  15. import font
  16. import chinese

  17. class PCD8544:
  18.     ADDRESSING_HORIZ = 0x00
  19.     ADDRESSING_VERT  = 0x02
  20.     INSTR_BASIC = 0x00
  21.     INSTR_EXT   = 0x01
  22.     POWER_UP   = 0x00
  23.     POWER_DOWN = 0x04
  24.     DISPLAY_BLANK   = 0x08
  25.     DISPLAY_ALL     = 0x09
  26.     DISPLAY_NORMAL  = 0x0c
  27.     DISPLAY_INVERSE = 0x0d
  28.     TEMP_COEFF_0 = 0x04
  29.     TEMP_COEFF_1 = 0x05
  30.     TEMP_COEFF_2 = 0x06
  31.     TEMP_COEFF_3 = 0x07
  32.     BIAS_1_4  = 0x17   # 1/4th
  33.     BIAS_1_5  = 0x16   # 1/5th
  34.     BIAS_1_6  = 0x15   # 1/6th
  35.     BIAS_1_7  = 0x14   # 1/7th
  36.     BIAS_1_8  = 0x13   # 1/8th
  37.     BIAS_1_9  = 0x12   # 1/9th
  38.     BIAS_1_10 = 0x11   # 1/10th
  39.     BIAS_1_11 = 0x10   # 1/11th

  40.     def __init__(self, spi, rst, ce, dc, light, pwr=None):
  41.         self.width  = 84
  42.         self.height = 48
  43.         self.power      = self.POWER_DOWN
  44.         self.addressing = self.ADDRESSING_HORIZ
  45.         self.instr      = self.INSTR_BASIC
  46.         self.display_mode = self.DISPLAY_BLANK
  47.         self.temp_coeff = self.TEMP_COEFF_0
  48.         self.bias       = self.BIAS_1_11
  49.         self.voltage    = 3060

  50.         # init the SPI bus and pins
  51.         spi.init(spi.MASTER, baudrate=328125, bits=8, polarity=0, phase=1, firstbit=spi.MSB)
  52.         if "OUT_PP" in dir(rst):
  53.             # pyBoard style
  54.             rst.init(rst.OUT_PP, rst.PULL_NONE)  # Reset line
  55.             ce.init(ce.OUT_PP, ce.PULL_NONE)     # Chip Enable
  56.             dc.init(dc.OUT_PP, dc.PULL_NONE)     # Data(1) / Command(0) mode
  57.             light.init(light.OUT_PP, light.PULL_NONE)
  58.             if pwr:
  59.                 pwr.init(pwr.OUT_PP, pwr.PULL_NONE)
  60.         else:
  61.             # WiPy style
  62.             rst.init(rst.OUT, None)
  63.             ce.init(ce.OUT, None)
  64.             dc.init(dc.OUT, None)
  65.             light.init(light.OUT, None)
  66.             if pwr:
  67.                 pwr.init(pwr.OUT, None)

  68.         self.spi   = spi
  69.         self.rst   = rst
  70.         self.ce    = ce
  71.         self.dc    = dc
  72.         self.light = light
  73.         self.pwr   = pwr

  74.         self.light_off()
  75.         self.power_on()
  76.         self.ce.value(1)  # set chip to disable (don't listen to input)
  77.         self.reset()
  78.         self.set_contrast(0xbf)
  79.         self.clear()
  80.         self.lcd_font = font.FONT6_8()
  81.         self.chinese = chinese.CN_UTF8()


  82.     def _set_function(self):
  83.         """ Write current power/addressing/instructionset values to lcd. """
  84.         value = 0x20 | self.power | self.addressing | self.instr
  85.         self.command([value])

  86.     def set_power(self, power, set=True):
  87.         """ Sets the power mode of the LCD controller """
  88.         assert power in [self.POWER_UP, self.POWER_DOWN], "Power must be POWER_UP or POWER_DOWN."
  89.         self.power = power
  90.         if set:
  91.             self._set_function()

  92.     def set_adressing(self, addr, set=True):
  93.         """ Sets the adressing mode """
  94.         assert addr in [self.ADDRESSING_HORIZ, self.ADDRESSING_VERT], "Addressing must be ADDRESSING_HORIZ or ADDRESSING_VERT."
  95.         self.addressing = addr
  96.         if set:
  97.             self._set_function()

  98.     def set_instr(self, instr, set=True):
  99.         """ Sets instruction set (basic/extended) """
  100.         assert instr in [self.INSTR_BASIC, self.INSTR_EXT], "Instr must be INSTR_BASIC or INSTR_EXT."
  101.         self.instr = instr
  102.         if set:
  103.             self._set_function()

  104.     def set_display(self, display_mode):
  105.         """ Sets display mode (blank, black, normal, inverse) """
  106.         assert display_mode in [self.DISPLAY_BLANK, self.DISPLAY_ALL, self.DISPLAY_NORMAL, self.DISPLAY_INVERSE], "Mode must be one of DISPLAY_BLANK, DISPLAY_ALL, DISPLAY_NORMAL or DISPLAY_INVERSE."
  107.         assert self.instr == self.INSTR_BASIC, "Please switch to basic instruction set first."
  108.         self.display_mode = display_mode
  109.         self.command([display_mode])

  110.     def set_temp_coeff(self, temp_coeff):
  111.         """ Sets temperature coefficient (0-3) """
  112.         assert 4 <= temp_coeff < 8, "Temperature coefficient must be one of TEMP_COEFF_0..TEMP_COEFF_3."
  113.         assert self.instr == self.INSTR_EXT, "Please switch to extended instruction set first."
  114.         self.temp_coeff = temp_coeff
  115.         self.command([temp_coeff])

  116.     def set_bias(self, bias):
  117.         """ Sets the LCD bias. """
  118.         assert 0x10 <= bias <= 0x17, "Bias must be one of BIAS_1_4..BIAS_1_11."
  119.         assert self.instr == self.INSTR_EXT, "Please switch to extended instruction set first."
  120.         self.bias = bias
  121.         self.command([bias])

  122.     def set_voltage(self, millivolts):
  123.         """ Sets the voltage of the LCD charge pump in millivolts. """
  124.         assert 3060 <= millivolts <= 10680, "Voltage must be between 3,060 and 10,680 mV."
  125.         assert self.instr == self.INSTR_EXT, "Please switch to extended instruction set first."
  126.         self.voltage = millivolts
  127.         basevoltage = millivolts - 3060
  128.         incrementor = basevoltage // 60
  129.         code = 0x80 & incrementor
  130.         self.command([code])

  131.     def set_contrast(self, value):
  132.         """ set LCD voltage, i.e. contrast """
  133.         assert 0x80 <= value <= 0xff, "contrast value must be between 0x80 and 0xff"
  134.         self.command([0x21, self.TEMP_COEFF_2, self.BIAS_1_7, value, 0x20, self.DISPLAY_NORMAL])
  135.         # 0x21 - enter extended instruction set (H=1)
  136.         # 0x06 - set temperature coefficient 2
  137.         # 0x14 - set BIAS system to n=3 (recomm. mux rate 1:40/1:34)
  138.         # value - (80-ff) - set Vop (80 = 3.00V, ff = 10.68V), 8b seems to work (0x3b/d70: 3.00+(70*0.06)=7.2V)
  139.         # 0x20 - back to basic instruction set
  140.         # 0x0c - normal display mode

  141.     def position(self, x, y):
  142.         """ set cursor to bank y, column x """
  143.         assert 0 <= x < self.width, "x must be between 0 and 83"
  144.         assert 0 <= y < self.height // 8, "y must be between 0 and 5"
  145.         assert self.instr == self.INSTR_BASIC, "Please switch to basic instruction set first."
  146.         self.command([x + 0x80, y + 0x40])

  147.     def clear(self):
  148.         """ clear screen """
  149.         self.position(0, 0)
  150.         self.data([0] * (self.height * self.width // 8))
  151.         self.position(0, 0)

  152.     def sleep_ms(self, mseconds):
  153.         try:
  154.             time.sleep_ms(mseconds)
  155.         except AttributeError:
  156.             machine.delay(mseconds)

  157.     def sleep_us(self, useconds):
  158.         try:
  159.             time.sleep_us(useconds)
  160.         except AttributeError:
  161.             machine.udelay(useconds)

  162.     def power_on(self):
  163.         if self.pwr:
  164.             self.pwr.value(1)
  165.         self.reset()

  166.     def reset(self):
  167.         """ issue reset impulse to reset the display """
  168.         self.rst.value(0)  # RST on
  169.         self.sleep_us(100) # reset impulse has to be >100 ns and <100 ms
  170.         self.rst.value(1)  # RST off
  171.         # Defaults after reset:
  172.         self.power      = self.POWER_DOWN
  173.         self.addressing = self.ADDRESSING_HORIZ
  174.         self.instr      = self.INSTR_BASIC
  175.         self.display_mode = self.DISPLAY_BLANK
  176.         self.temp_coeff = self.TEMP_COEFF_0
  177.         self.bias       = self.BIAS_1_11
  178.         self.voltage    = 3060

  179.     def power_off(self):
  180.         self.clear()
  181.         self.command([0x20, 0x08])
  182.         # 0x20 - basic instruction set
  183.         # 0x08 - set display to blank (doesn't delete contents)
  184.         self.sleep_ms(10)
  185.         if self.pwr:
  186.             self.pwr.value(0) # turn off power

  187.     def command(self, arr):
  188.         """ send bytes in command mode """
  189.         self.bitmap(arr, 0)

  190.     def data(self, arr):
  191.         """ send bytes in data mode """
  192.         self.bitmap(arr, 1)

  193.     def bitmap(self, arr, dc):
  194.         self.dc.value(dc)
  195.         buf = struct.pack('B'*len(arr), *arr)
  196.         self.ce.value(0) # set chip to listening/enable
  197.         try:
  198.             self.spi.send(buf)
  199.         except AttributeError:
  200.             self.spi.write(buf)
  201.         self.ce.value(1) # set chip to disable

  202.     def light_on(self):
  203.         self.light.value(0)  # pull to GND

  204.     def light_off(self):
  205.         self.light.value(1)  # set to HIGH

  206.     def lcd_write_string(self, string, x, y):
  207.         self.position(x,y)
  208.         for i in string:
  209.             self.data(self.lcd_font.get_font6_8(i))
  210.      
  211.     def lcd_write_chineses(str,x,y,space = 9):
  212.         # i,j=0,0
  213.         # lsLen = len(str)
  214.         # while (j<lsLen)
  215.             # self.lcd_write_chinese(str[j],x+(i*space),y)
  216.             # i+=1
  217.             # j+=1
  218.         return 0
  219.      
  220.     def lcd_write_chinese(self,data,x,y):
  221.         #获取 字 的UTF8码
  222.         code = 0x00 #赋初值
  223.         data_code = data.encode("UTF-8")
  224.         code |= data_code[0]<<16
  225.         code |= data_code[1]<<8
  226.         code |= data_code[2]
  227.         #获取 字 的UTF8码 END
  228.         self.position(x,y)
  229.         self.data(self.chinese.get_chinese_utf8(code,0))
  230.         self.position(x,y+1)
  231.         self.data(self.chinese.get_chinese_utf8(code,1))
复制代码

#DS3231.py
  1. import pyb
  2. from pyb import I2C
  3. DS3231_ADDR       = const(0x68)
  4. DS3231_REG_SEC    = const(0x00)
  5. DS3231_REG_MIN    = const(0x01)
  6. DS3231_REG_HOUR   = const(0x02)
  7. DS3231_REG_WEEKDAY= const(0x03)
  8. DS3231_REG_DAY    = const(0x04)
  9. DS3231_REG_MONTH  = const(0x05)
  10. DS3231_REG_YEAR   = const(0x06)
  11. DS3231_REG_A1SEC  = const(0x07)
  12. DS3231_REG_A1MIN  = const(0x08)
  13. DS3231_REG_A1HOUR = const(0x09)
  14. DS3231_REG_A1DAY  = const(0x0A)
  15. DS3231_REG_A2MIN  = const(0x0B)
  16. DS3231_REG_A2HOUR = const(0x0C)
  17. DS3231_REG_A2DAY  = const(0x0D)
  18. DS3231_REG_CTRL   = const(0x0E)
  19. DS3231_REG_STA    = const(0x0F)
  20. DS3231_REG_OFF    = const(0x10)
  21. DS3231_REG_TEMP   = const(0x11)

  22. class DS3231(object):
  23.     def __init__(self, i2c_num):
  24.         self.i2c = I2C(i2c_num, I2C.MASTER, baudrate = 100000)

  25.     def DATE(self, dat=[]):
  26.         if dat==[]:
  27.             t = []
  28.             t.append(self.year())
  29.             t.append(self.month())
  30.             t.append(self.day())
  31.             return t
  32.         else:
  33.             self.year(dat[0])
  34.             self.month(dat[1])
  35.             self.day(dat[2])

  36.     def TIME(self, dat=[]):
  37.         if dat==[]:
  38.             t = []
  39.             t.append(self.hour())
  40.             t.append(self.min())
  41.             t.append(self.sec())
  42.             # t = ""
  43.             # t+=self.hour()+":"
  44.             # t+=self.min()+":"
  45.             # t+=self.sec()
  46.             return t
  47.         else:
  48.             self.hour(dat[0])
  49.             self.min(dat[1])
  50.             self.sec(dat[2])

  51.     def DateTime(self, dat=[]):
  52.         if dat==[]:
  53.             return self.DATE() + self.TIME()
  54.         else:
  55.             self.year(dat[0])
  56.             self.month(dat[1])
  57.             self.day(dat[2])
  58.             self.hour(dat[3])
  59.             self.min(dat[4])
  60.             self.sec(dat[5])

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

  63.     def setREG(self, dat, reg):
  64.         buf = bytearray(2)
  65.         buf[0] = reg
  66.         buf[1] = dat
  67.         self.i2c.send(buf, DS3231_ADDR)
  68.          
  69.     def getREG_DEC(self, reg):
  70.         self.i2c.send(reg, DS3231_ADDR)
  71.         t = self.i2c.recv(1, DS3231_ADDR)[0]
  72.         return (t>>4)*10 + (t%16)

  73.     def sec(self, sec=''):
  74.         if sec == '':
  75.             return self.getREG_DEC(DS3231_REG_SEC)
  76.         else:
  77.             self.setREG(self.dec2hex(sec), DS3231_REG_SEC)

  78.     def min(self, min=''):
  79.         if min == '':
  80.             return self.getREG_DEC(DS3231_REG_MIN)
  81.         else:
  82.             self.setREG(self.dec2hex(min), DS3231_REG_MIN)

  83.     def hour(self, hour=''):
  84.         if hour=='':
  85.             return self.getREG_DEC(DS3231_REG_HOUR)
  86.         else:
  87.             self.setREG(self.dec2hex(hour), DS3231_REG_HOUR)

  88.     def day(self, day=''):
  89.         if day=='':
  90.             return self.getREG_DEC(DS3231_REG_DAY)
  91.         else:
  92.             self.setREG(self.dec2hex(day), DS3231_REG_DAY)

  93.     def month(self, month=''):
  94.         if month=='':
  95.             return self.getREG_DEC(DS3231_REG_MONTH)
  96.         else:
  97.             self.setREG(self.dec2hex(month), DS3231_REG_MONTH)

  98.     def year(self, year=''):
  99.         if year=='':
  100.             return self.getREG_DEC(DS3231_REG_YEAR)
  101.         else:
  102.             self.setREG(self.dec2hex(year), DS3231_REG_YEAR)

  103.     def TEMP(self):
  104.         self.i2c.send(DS3231_REG_TEMP, DS3231_ADDR)
  105.         t1 = self.i2c.recv(1, DS3231_ADDR)[0]
  106.         self.i2c.send(DS3231_REG_TEMP+1, DS3231_ADDR)
  107.         t2 = self.i2c.recv(1, DS3231_ADDR)[0]
  108.         if t1>0x7F:
  109.             return t1 - t2/256 -256
  110.         else:
  111.             return t1 + t2/256
复制代码

五、实现效果


原程序及putty工具下载地址:http://pan.baidu.com/s/1bLUhaQ 

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-28 22:29 , Processed in 0.046344 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表