1.1. [Micropython]TPYBoard控制LCD5110汉字显示温度时间

版权声明:翻译整理属于TPYBoard,转载时请以超链接形式标明文章原始出处和作者信息及本声明

1.1.1. 1. 实验目的

  1. 学习在PC机系统中扩展简单I/O 接口的方法。
  2. 什么是SPI接口。
  3. 学习TPYBoard I2C接口的用法。
  4. 学习LCD5110接线方法。
  5. DS3231的接线方法。
  6. 16*16汉字显示温度与当前时间。

1.1.2. 2. 所需元器件

DS3231模块一个 TPYBoard板子一块 LCD5110显示屏一个 数据线一条 杜邦线若干

1.1.3. 3.TPYBoard的SPI接口

LCD5110需要SPI接口与TPYBoard进行连接传输数据,SPI接口是在CPU和外围低速器件之间进行同步串行数据传输,TPYBoard有两个SPI接口,我们用的为SPI1接口。
http://www.micropython.net.cn/ueditor/php/upload/image/20161229/1482973399550325.png

1.1.4. 4.TPYBoard的I2C接口

DS3231是I2C接口通信的,它通过I2C接口与TPYboard进行数据通讯,DS3231通过这个接口与TPYBoard进行双向通讯,进行数据传输,TPYBoard有两个I2C接口,我们在这用的是I2C接口1。
http://www.micropython.net.cn/ueditor/php/upload/image/20161229/1482973431559379.png

1.1.5. 5.DS3231的接线方法

DS会我们在这只用到DS3231的SCL,SDA,VCC,GND四个针脚即可设定读出当前时间,我们用的是TPYBoard的I2C接口1,即DS3231的针脚SCL接TPYboard的针脚X9,针脚SDA接TPYBoard的针脚X10,VCC接TPYBoard的3.3V,GND接TPYBoard的GND。
http://www.micropython.net.cn/ueditor/php/upload/image/20161229/1482973477928543.png

1.1.6. 6.控制5110显示屏显示16x16汉字

先看一下LCD5110针脚含义吧(注意:LCD5110的针脚有些不一样的) TPYBoard的针脚与5110的针脚对应关系如下:

TPYBoard | LCD5110 | memo |

+================+=================+====================================================+ | Pin | RST | Reset pin (0=reset, 1=normal) |

Pin | CE | Chip Enable (0=listen for input, 1=ignore input) | +—————-+—————–+—————————————————-+
Pin | DC | Data/Command (0=commands, 1=data) | +—————-+—————–+—————————————————-+
MOSI | DIN | data flow (Master out, Slave in) | +—————-+—————–+—————————————————-+
SCK | CLK | SPI clock | +—————-+—————–+—————————————————-+
3V3/Pin | VCC | 3.3V logic voltage (0=off, 1=on) | +—————-+—————–+—————————————————-+
Pin | LIGHT | Light (0=on, 1=off) | +—————-+—————–+—————————————————-+
GND | GND | +—————-+—————–+—————————————————-+

还是看不明白的话,直接上针脚编号吧

TPYBoard | LCD5110 | memo |

+================+=================+====================================================+ | X1 | RST | Reset pin (0=reset, 1=normal) |

X2 | CE | Chip Enable (0=listen for input, 1=ignore input) | +—————-+—————–+—————————————————-+
X3 | DC | Data/Command (0=commands, 1=data) | +—————-+—————–+—————————————————-+
X8 | DIN | data flow (Master out, Slave in) | +—————-+—————–+—————————————————-+
X6 | CLK | SPI clock | +—————-+—————–+—————————————————-+
VCC | +—————-+—————–+—————————————————-+
X4 | LIGHT | Light (0=on, 1=off) | +—————-+—————–+—————————————————-+
GND | +—————-+—————–+—————————————————-+
http://www.micropython.net.cn/ueditor/php/upload/image/20161229/1482973640480164.png http://www.micropython.net.cn/ueditor/php/upload/image/20161229/1482973647210289.png

接线ok后,并且导入font.py文件、upcd8544.py、chinsese.py文件以及DS3231.py,编写main.py设定时间,运行main.py即可将当前温度与时间显示在5110显示屏上,如上图所示。

1.1.7. 7.源代码

# main.py -- put your code here!
import pyb
import upcd8544
from machine import SPI,Pin
from DS3231 import DS3231

ds=DS3231(1) #定义DS3231

# 用于设定时间和日期
def setDateTime(year,month,day,time,minutes,seconds):
        ds.DATE([year,month,day])
        ds.TIME([time,minutes,seconds])

# 在LCD5110 显示时间或日期,separator 中间的分割符
# x,y 在LCD5110 显示的位置
def showTimeOrDate(why,x,y,separator=':'):
        # [HH,MM,SS] >> HH:MM:SS
        why = why.replace('[','')
        why = why.replace(']','')
        why = why.replace(',',separator)
        print(why)
        lcd_5110.lcd_write_string(why,x,y)


def main():
        lcd_5110.lcd_write_chinese('萝',14,0)
        lcd_5110.lcd_write_chinese('卜',30,0)
        lcd_5110.lcd_write_chinese('智',46,0)
        lcd_5110.lcd_write_chinese('能',62,0)
        lcd_5110.lcd_write_string('TEM:',14,2)
        lcd_5110.lcd_write_string(str(ds.TEMP()),44,2)
        lcd_5110.lcd_write_chinese("当",14,3)
        lcd_5110.lcd_write_chinese("前",30,3)
        lcd_5110.lcd_write_chinese("时",46,3)
        lcd_5110.lcd_write_chinese("间",62,3)
        showTimeOrDate(str(ds.TIME()),14,5)
        print(str(ds.TIME()))
        pyb.delay(1000)

if __name__ == '__main__':
        #setDateTime(2016,12,27,13,17,00)#设置时间
        ds.DATE()
        SPI = pyb.SPI(1) #DIN=>X8-MOSI/CLK=>X6-SCK
        #DIN =>SPI(1).MOSI 'X8' data flow (Master out, Slave in)
        #CLK =>SPI(1).SCK  'X6' SPI clock
        RST    = pyb.Pin('X1')
        CE     = pyb.Pin('X2')
        DC     = pyb.Pin('X3')
        LIGHT  = pyb.Pin('X4')
        lcd_5110 = upcd8544.PCD8544(SPI, RST, CE, DC, LIGHT)
        while(1):
        main()

LCD5110 ..LINK:http://www.micropython.net.cn/ueditor/php/upload/file/20161229/1482973799786626.zip