极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13462|回复: 0

Micropython教程之TPYBoard DIY电子时钟(萝卜学科编程教育)

[复制链接]
发表于 2018-12-4 10:41:47 | 显示全部楼层 |阅读模式
    1.实验目的

    1. 学习在PC机系统中扩展简单I/O?接口的方法。
    2. 什么是SPI接口。
    3. 学习TPYBoard I2C接口的用法。
    4. 学习LCD5110接线方法。
    5. 设定时钟并将当前时间显示在LCD5110上。

    2.所需元器件

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

    3.TPYBoard的SPI接口

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



    4.TPYBoard的I2C接口

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



    5.DS3231的接线方法

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



    6.控制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



    7.源代码

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

  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)
  7. ds.DATE([16,11,26])
  8. ds.TIME([16,14,6])

  9. while True:
  10.     ds.TEMP()
  11.     ds.DATE()
  12.     SPI = pyb.SPI(1) #DIN=>X8-MOSI/CLK=>X6-SCK
  13.     #DIN =>SPI(1).MOSI 'X8' data flow (Master out, Slave in)
  14.     #CLK =>SPI(1).SCK  'X6' SPI clock
  15.     RST    = pyb.Pin('X1')
  16.     CE     = pyb.Pin('X2')
  17.     DC     = pyb.Pin('X3')
  18.     LIGHT  = pyb.Pin('X4')
  19.     lcd_5110 = upcd8544.PCD8544(SPI, RST, CE, DC, LIGHT)
  20.     lcd_5110.lcd_write_string('Date',0,0)
  21.     lcd_5110.lcd_write_string(str(ds.DATE()),0,1)
  22.     lcd_5110.lcd_write_string('Time',0,2)
  23.     lcd_5110.lcd_write_string(str(ds.TIME()),0,3)
  24.     lcd_5110.lcd_write_string('Tem',0,4 )
  25.     lcd_5110.lcd_write_string(str(ds.TEMP()),0,5)
  26.     pyb.delay(1000)
复制代码

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

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

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 00:00 , Processed in 0.044872 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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