查看: 3479|回复: 3

Python开发实例:TPYBoard制作红绿灯

[复制链接]
  • TA的每日心情
    慵懒
    2018-1-6 09:01
  • 签到天数: 7 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    发表于 2016-10-9 15:05:08 | 显示全部楼层 |阅读模式
    分享到:
        流水灯可能是大家最经常DIY的东西,一个是因为简单容易实现,再就是效果不错。为了演示MicroPython的强大功能,展现不同的用法,萝卜君的小伙伴自己用TPYBoard开发板DIY了一个小红绿灯板,先看下具体的视频演示吧,对micropython感兴趣的小伙伴也可以加入到(TPYBoard技术交流群:157816561)里来,一起体验python开发的魅力!



        关于TPYBoard

        TPYBoard单片机开发板,现有TPYBoardV101、v102两个版本,其中v102为v101的升级版,新增swd接口。采用python开发语言,提供了30个GPIO,轻松使用python开发物联网产品。支持Python3.0及以上版本的直接运行,支持重力加速度传感器,支持上百周边外设配件。

    092828osguoqxsoogm16b1.png.thumb.jpg


        1.实验目的

        1. 学习在PC机系统中扩展简单I/O 接口的方法。
        2. 进一步学习编制数据输出程序的设计方法。
        3. 学习模拟交通灯控制的方法。

        2.所需元器件

        220欧电阻一个
        红色LED数码管一个
        面包板一块
        TPYBoard板子一块
        数据线一条
        红、绿、黄三个led灯
        杜邦线若干

        3.点亮led灯

        将三个led灯插在面包板上,led负极插入面包板的负极(横向插孔),正极插入面包板的纵向插,将222欧电阻插入面包板的负极上(横向插孔)和纵向插孔中,将led灯的正极分别与TPYBoard的引脚连接起来,因为要做红绿灯,只需三个引脚即可,本人用的为Y1、Y2、Y3三个引脚,将三个led灯的正极通过杜邦线连接到TPYboard的Y1,、Y2、Y3的引脚上,然后将电阻纵向插孔用杜邦线接到TPYboard的GND引脚,在main.py文件中将Y1、Y2、Y3引脚的电平拉高,即可看到三个灯同时亮起来。代码为:
    1. # main.py -- put your code here!
    2. import pyb
    3. led1 = pyb.Pin("Y1",pyb.Pin.OUT_PP)
    4. led2 = pyb.Pin("Y2",pyb.Pin.OUT_PP)
    5. led3 = pyb.Pin("Y3",pyb.Pin.OUT_PP)
    6. While True:
    7.      led1.value(1)
    8.      led2.value(1)
    9.      led3.value(1)
    复制代码
        如下图:
    红绿灯 (1).png



        4.点亮数码管

        SM42056是0.56英寸一位共阴/红色LED数码管。一共十个引脚。当小数点在你的右下角时,上面一排五个引脚,从左至右依次为g,f,地,a,b,下面一排五个引脚,从左至右依次为 e,d,地,c,dp。我们要想让数码管亮起来只需要将g,f,a,b,e,d,c,dp(在这用不到)在main.py中拉高电平,把地与TPYboard的GND引脚接起来,这样就会显示为8。如下图:

    红绿灯 (2).png



        下面为0-9数字对应针脚的高电平,即对应TPYboard的引脚拉高电平

        数字高电平针脚

        0         a,b,c,d,e,f
        1         e,f
        2         a,b,g,e,d
        3         a,b,g,c,d
        4         b,c,g,f
        5         a,f,g,c,d
        6         a,f,e,d,c,g
        7         a,b,c
        8         a,b,c,d,e,f,g

        5.模拟红绿灯

        我们按照上面的步骤做完以后,然后通电,修改main.py文件,即可让灯随着数码管的变化而变化,具体代码如下:
    1. # main.py -- put your code here!
    2. import pyb
    3. led1 = pyb.Pin("Y1",pyb.Pin.OUT_PP)
    4. led2 = pyb.Pin("Y2",pyb.Pin.OUT_PP)
    5. led3 = pyb.Pin("Y3",pyb.Pin.OUT_PP)
    6. x1 = pyb.Pin("X1",pyb.Pin.OUT_PP)
    7. x2 = pyb.Pin("X2",pyb.Pin.OUT_PP)
    8. x3 = pyb.Pin("X3",pyb.Pin.OUT_PP)
    9. x4 = pyb.Pin("X4",pyb.Pin.OUT_PP)
    10. x5 = pyb.Pin("X5",pyb.Pin.OUT_PP)
    11. x6 = pyb.Pin("X6",pyb.Pin.OUT_PP)
    12. x8 = pyb.Pin("X8",pyb.Pin.OUT_PP)
    13. def six():
    14.    x1.value(1)
    15.    x2.value(1)
    16.    x3.value(1)
    17.    x5.value(1)
    18.    x6.value(1)
    19.    x8.value(1)
    20.    pyb.delay(1000)
    21.    x1.value(0)
    22.    x2.value(0)
    23.    x3.value(0)
    24.    x6.value(0)
    25.    x5.value(0)
    26.    x8.value(0)
    27. def nine():
    28.    x1.value(1)
    29.    x2.value(1)
    30.    x3.value(1)
    31.    x4.value(1)
    32.    x5.value(1)
    33.    x8.value(1)
    34.    pyb.delay(1000)
    35.    x1.value(0)
    36.    x2.value(0)
    37.    x3.value(0)
    38.    x4.value(0)
    39.    x5.value(0)
    40.    x8.value(0)
    41. def eight():
    42.    x1.value(1)
    43.    x2.value(1)
    44.    x3.value(1)
    45.    x4.value(1)
    46.    x5.value(1)
    47.    x6.value(1)
    48.    x8.value(1)
    49.    pyb.delay(1000)
    50.    x1.value(0)
    51.    x2.value(0)
    52.    x3.value(0)
    53.    x4.value(0)
    54.    x5.value(0)
    55.    x6.value(0)
    56.    x8.value(0)
    57. def zero():
    58.    x2.value(1)
    59.    x3.value(1)
    60.    x4.value(1)
    61.    x5.value(1)
    62.    x6.value(1)
    63.    x8.value(1)
    64.    pyb.delay(1000)
    65.    x2.value(0)
    66.    x3.value(0)
    67.    x4.value(0)
    68.    x5.value(0)
    69.    x6.value(0)
    70.    x8.value(0)
    71. def seven():
    72.    x3.value(1)
    73.    x4.value(1)
    74.    x8.value(1)
    75.    pyb.delay(1000)
    76.    x3.value(0)
    77.    x4.value(0)
    78.    x8.value(0)
    79. def five():
    80.    x1.value(1)
    81.    x2.value(1)
    82.    x3.value(1)
    83.    x5.value(1)
    84.    x8.value(1)
    85.    pyb.delay(1000)
    86.    x1.value(0)
    87.    x2.value(0)
    88.    x3.value(0)
    89.    x5.value(0)
    90.    x8.value(0)
    91. def four():
    92.    x1.value(1)
    93.    x2.value(1)
    94.    x4.value(1)
    95.    x8.value(1)
    96.    pyb.delay(1000)
    97.    x1.value(0)
    98.    x2.value(0)
    99.    x4.value(0)
    100.    x8.value(0)
    101. def three():
    102.    x1.value(1)
    103.    x3.value(1)
    104.    x4.value(1)
    105.    x5.value(1)
    106.    x8.value(1)
    107.    pyb.delay(1000)
    108.    x1.value(0)
    109.    x4.value(0)
    110.    x3.value(0)
    111.    x5.value(0)
    112.    x8.value(0)
    113. def two():
    114.    x1.value(1)
    115.    x3.value(1)
    116.    x4.value(1)
    117.    x5.value(1)
    118.    x6.value(1)
    119.    pyb.delay(1000)
    120.    x1.value(0)
    121.    x3.value(0)
    122.    x4.value(0)
    123.    x5.value(0)
    124.    x6.value(0)
    125. def one():
    126.    x2.value(1)
    127.    x6.value(1)
    128.    pyb.delay(1000)
    129.    x2.value(0)
    130.    x6.value(0)
    131. while True:
    132.    led1.value(1)
    133.    nine()
    134.    eight()
    135.    seven()
    136.    six()
    137.    five()
    138.    four()
    139.    three()
    140.    two()
    141.    one()
    142.    zero()
    143.    led1.value(0)
    144.    led2.value(1)
    145.    nine()
    146.    eight()
    147.    seven()
    148.    six()
    149.    five()
    150.    four()
    151.    three()
    152.    two()
    153.    one()
    154.    zero()
    155.    led2.value(0)
    156.    led3.value(1)
    157.    three()
    158.    two()
    159.    one()
    160.    zero()
    161.    led3.value(0)
    复制代码
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2019-3-22 00:48
  • 签到天数: 15 天

    连续签到: 1 天

    [LV.4]偶尔看看III

    发表于 2016-10-20 06:27:22 | 显示全部楼层
    看懂了。谢谢!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2019-3-22 00:48
  • 签到天数: 15 天

    连续签到: 1 天

    [LV.4]偶尔看看III

    发表于 2016-10-20 06:54:31 | 显示全部楼层
    好东西,支持,准备参加活动。
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2019-4-24 06:39
  • 签到天数: 4 天

    连续签到: 1 天

    [LV.2]偶尔看看I

    发表于 2016-10-24 08:57:50 | 显示全部楼层
    支持,好活动。
    回复 支持 反对

    使用道具 举报

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

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-20 13:16 , Processed in 0.158121 second(s), 22 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.