|
此次使用PYB Nano开发板的GPIO直接驱动LIN-3643SR四位共阴数码管显示,实现模拟红绿灯的功能,这个实验可以用于大家练习在MicroPython下如何使用GPIO。
【1】硬件连接:
【2】程序源码:
- # main.py -- put your code here!
- import pyb
- from pyb import LED
- ledR = LED(1)
- ledG = LED(2)
- ledY = LED(3)
- ledB = LED(4)
- seg1 = pyb.Pin("Y5",pyb.Pin.OUT_PP)
- seg2 = pyb.Pin("Y2",pyb.Pin.OUT_PP)
- seg3 = pyb.Pin("Y1",pyb.Pin.OUT_PP)
- seg4 = pyb.Pin("X4",pyb.Pin.OUT_PP)
- a = pyb.Pin("Y4",pyb.Pin.OUT_PP)
- b = pyb.Pin("Y0",pyb.Pin.OUT_PP)
- c = pyb.Pin("X6",pyb.Pin.OUT_PP)
- d = pyb.Pin("X8",pyb.Pin.OUT_PP)
- e = pyb.Pin("X9",pyb.Pin.OUT_PP)
- f = pyb.Pin("Y3",pyb.Pin.OUT_PP)
- g = pyb.Pin("X5",pyb.Pin.OUT_PP)
- dp= pyb.Pin("X7",pyb.Pin.OUT_PP)
- seg1.low()
- seg2.low()
- seg3.low()
- seg4.low()
- dp.value(0)
- def nine():
- a.value(1)
- b.value(1)
- c.value(1)
- d.value(1)
- f.value(1)
- g.value(1)
- pyb.delay(1000)
- a.value(0)
- b.value(0)
- c.value(0)
- d.value(0)
- f.value(0)
- g.value(0)
- def eight():
- a.value(1)
- b.value(1)
- c.value(1)
- d.value(1)
- e.value(1)
- f.value(1)
- g.value(1)
- pyb.delay(1000)
- a.value(0)
- b.value(0)
- c.value(0)
- d.value(0)
- e.value(0)
- f.value(0)
- g.value(0)
- def seven():
- a.value(1)
- b.value(1)
- c.value(1)
- pyb.delay(1000)
- a.value(0)
- b.value(0)
- c.value(0)
- def six():
- a.value(1)
- f.value(1)
- e.value(1)
- d.value(1)
- c.value(1)
- g.value(1)
- pyb.delay(1000)
- a.value(0)
- f.value(0)
- e.value(0)
- d.value(0)
- c.value(0)
- g.value(0)
- def five():
- a.value(1)
- f.value(1)
- g.value(1)
- c.value(1)
- d.value(1)
- pyb.delay(1000)
- a.value(0)
- f.value(0)
- g.value(0)
- c.value(0)
- d.value(0)
- def four():
- b.value(1)
- c.value(1)
- g.value(1)
- f.value(1)
- pyb.delay(1000)
- b.value(0)
- c.value(0)
- g.value(0)
- f.value(0)
- def three():
- a.value(1)
- b.value(1)
- g.value(1)
- c.value(1)
- d.value(1)
- pyb.delay(1000)
- a.value(0)
- b.value(0)
- g.value(0)
- c.value(0)
- d.value(0)
- def two():
- a.value(1)
- b.value(1)
- g.value(1)
- e.value(1)
- d.value(1)
- pyb.delay(1000)
- a.value(0)
- b.value(0)
- g.value(0)
- e.value(0)
- d.value(0)
- def one():
- b.value(1)
- c.value(1)
- pyb.delay(1000)
- b.value(0)
- c.value(0)
- def zero():
- a.value(1)
- b.value(1)
- c.value(1)
- d.value(1)
- e.value(1)
- f.value(1)
- pyb.delay(1000)
- a.value(0)
- b.value(0)
- c.value(0)
- d.value(0)
- e.value(0)
- f.value(0)
- while True:
- ledR.on()
- nine()
- eight()
- seven()
- six()
- five()
- four()
- three()
- two()
- one()
- zero()
- ledR.off()
- ledG.on()
- nine()
- eight()
- seven()
- six()
- five()
- four()
- three()
- two()
- one()
- zero()
- ledG.off()
- ledY.on()
- three()
- two()
- one()
- zero()
- ledY.off()
复制代码
【3】效果演示:
视频演示:
PYB Nano驱动数码管模拟红绿灯
|
赞赏
-
1
查看全部赞赏
-
|