[Micropython]TPYBoard DIY电子时钟 | 典型实例 | 技术支持 | MicroPython


当前位置:首页>技术支持>典型实例>[Micropython]TPYBoard DIY电子时钟

[Micropython]TPYBoard DIY电子时钟

原创版权归山东萝卜科技有限公司所有,转载必须以链接形式注明作者和原始出处。


    实验目的


    1. 学习在PC机系统中扩展简单I/O 接口的方法。

    2. 什么是SPI接口。

    3. 学习TPYBoard I2C接口的用法。

    4. 学习LCD5110接线方法。

    5. 设定时钟并将当前时间显示在LCD5110上。


    所需元器件


    DS3231模块一个

    TPYBoard板子一块

    LCD5110显示屏一个

    数据线一条

    杜邦线若干


    TPYBoard的SPI接口


    LCD5110需要SPI接口与TPYBoard进行连接传输数据,SPI接口是在CPU和外围低速器件之间进行同步串行数据传输,TPYBoard有两个SPI接口,我们用的为SPI1接口。


blob.png


    TPYBoard的I2C接口


    DS3231是I2C接口通信的,它通过I2C接口与TPYboard进行数据通讯,DS3231通过这个接口与TPYBoard进行双向通讯,进行数据传输,TPYBoard有两个I2C接口,我们在这用的是I2C接口1。


blob.pngblob.png


    DS3231的接线方法


    DS会我们在这只用到DS3231的SCL,SDA,VCC,GND四个针脚即可设定读出当前时间,我们用的位I2C接口1,即DS3231的针脚SCL接TPYboard的针脚X9,针脚SDA接TPYBoard的针脚X10。


blob.png


    控制5110显示屏显示6x8字符


    先看一下LCD5110针脚含义吧(注意:LCD5110的针脚有些不一样的)


    TPYBoard的针脚与5110的针脚对应关系如下:


    TPYBoard       LCD5110    memo

    ----------------------------

    # any   Pin        => RST       Reset pin (0=reset, 1=normal)

    # any   Pin        => CE        Chip Enable (0=listen for input,   1=ignore input)

    # any   Pin        => DC        Data/Command (0=commands, 1=data)

    #   MOSI          => DIN       data flow (Master out, Slave in)

    #   SCK           => CLK       SPI clock

    # 3V3   or any Pin  => VCC       3.3V logic voltage (0=off, 1=on)

    # any   Pin        => LIGHT     Light (0=on, 1=off)

    #   GND           => GND


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


    TPYBoard       LCD5110    memo

    ----------------------------

    Y10        => RST       Reset pin (0=reset, 1=normal)

    Y11        => CE        Chip Enable (0=listen for input, 1=ignore input)

    Y9         => DC        Data/Command (0=commands, 1=data)

    X8         => DIN       data flow (Master out, Slave in)

    X6         => CLK       SPI clock

    VCC

    Y12        => LIGHT     Light (0=on, 1=off)

    GND


blob.png

    源代码


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

# main.py -- put your code here!
import pyb
import upcd8544
from machine import SPI,Pin
from DS3231 import DS3231
 
ds=DS3231(1)
ds.DATE([16,11,26])
ds.TIME([16,14,6])
 
while True:
    ds.TEMP()
    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('Y10')
    CE     = pyb.Pin('Y11')
    DC     = pyb.Pin('Y9')
    LIGHT  = pyb.Pin('Y12')
    lcd_5110 = upcd8544.PCD8544(SPI, RST, CE, DC, LIGHT)
    lcd_5110.lcd_write_string('Date',0,0)
    lcd_5110.lcd_write_string(str(ds.DATE()),0,1)
    lcd_5110.lcd_write_string('Time',0,2)
    lcd_5110.lcd_write_string(str(ds.TIME()),0,3)
    lcd_5110.lcd_write_string('Tem',0,4 )
    lcd_5110.lcd_write_string(str(ds.TEMP()),0,5)
    pyb.delay(1000)

DS3231.rar