Micropython实例之TPYBoardv102位数码管显示实验
所需原器件1 TPYBoard v102 一块
2 四位数码管一个
3 杜邦线若干
4 面包板一块(为了接线方便,可不用)
接线图
四位数码管引脚图
开发板与四位数码管针脚对应:
程序源代码:
import pyb
from pyb import Pin
#数码管a~g dp对应的开发板引脚
d_Pins=]
#数码管位段1 2 3 4对应的引脚
w_Pins=]
number={
'0':
,#0
'1':
,#1
'2':
,#2
'3':
,#3
'4':
,#4
'5':
,#5
'6':
,#6
'7':
,#7
'8':
,#8
'9':
,#9
}
def display(num,dp):
global number
count=0
for pin in d_Pins:#显示num的值
pin.value(number)
count+=1
if dp==1:
d_Pins.value(0)
def clear():
for i in w_Pins:
i.value(0)
for i in d_Pins:
i.value(1)
def showData(num):
#分割出数值的百位、千位、个位和小数位的值
d_num=num
location=d_num.find('.')
if location>0:
d_num=d_num.replace('.','')
while len(d_num)<4:
d_num='0'+d_num
for i in range(0,4):
pyb.udelay(2000)
clear()
w_Pins.value(1)
if i==location-1:
display(d_num,1)
else:
display(d_num,0)
if location<0:
for i in range(0,4):
pyb.udelay(2000)
clear()
w_Pins.value(1)
display(d_num,0)
while True:
num='9016'
showData(num)
页:
[1]