温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python怎么实现数码管显示

发布时间:2022-01-13 10:34:06 来源:亿速云 阅读:191 作者:iii 栏目:互联网科技

这篇文章主要介绍“python怎么实现数码管显示”,在日常操作中,相信很多人在python怎么实现数码管显示问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”python怎么实现数码管显示”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

import pyb
from pyb import Pin
 
#数码管a~g dp对应的开发板引脚
d_Pins=[Pin(i,Pin.OUT_PP) for i in ['X1','X2','X3','X4','X5','X6','X7','X8']]
#数码管位段1 2 3 4对应的引脚
w_Pins=[Pin(i,Pin.OUT_PP) for i in ['Y9','Y10','Y11','Y12']]
 
number={
'0':
[0,0,0,0,0,0,1,1],#0
'1':
[1,1,1,1,0,0,1,1],#1
'2':
[0,0,1,0,0,1,0,1],#2
'3':
[0,0,0,0,1,1,0,1],#3
'4':
[1,0,0,1,1,0,0,1],#4
'5':
[0,1,0,0,1,0,0,1],#5
'6':
[0,1,0,0,0,0,0,1],#6
'7':
[0,0,0,1,1,1,1,1],#7
'8':
[0,0,0,0,0,0,0,1],#8
'9':
[0,0,0,0,1,0,0,1],#9
}
 
def display(num,dp):
    global number
    count=0
    for pin in d_Pins:#显示num的值
        pin.value(number[num][count])
        count+=1
    if dp==1:
        d_Pins[7].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[3-i].value(1)
            if i==location-1:
                display(d_num[i],1)
            else:
                display(d_num[i],0)
    if location<0:
        for i in range(0,4):
            pyb.udelay(2000)
            clear()
            w_Pins[3-i].value(1)
            display(d_num[i],0)
while True:
 
    num='9016'
    showData(num)

    所需原器件

    1   TPYBoard v102 一块
    2   四位数码管一个
    3   杜邦线若干
    4   面包板一块(为了接线方便,可不用)

    接线图
    四位数码管引脚图

python怎么实现数码管显示

python怎么实现数码管显示
    开发板与四位数码管针脚对应:

python怎么实现数码管显示
    程序源代码:

import pyb
from pyb import Pin
 
#数码管a~g dp对应的开发板引脚
d_Pins=[Pin(i,Pin.OUT_PP) for i in ['X1','X2','X3','X4','X5','X6','X7','X8']]
#数码管位段1 2 3 4对应的引脚
w_Pins=[Pin(i,Pin.OUT_PP) for i in ['Y9','Y10','Y11','Y12']]
 
number={
'0':
[0,0,0,0,0,0,1,1],#0
'1':
[1,1,1,1,0,0,1,1],#1
'2':
[0,0,1,0,0,1,0,1],#2
'3':
[0,0,0,0,1,1,0,1],#3
'4':
[1,0,0,1,1,0,0,1],#4
'5':
[0,1,0,0,1,0,0,1],#5
'6':
[0,1,0,0,0,0,0,1],#6
'7':
[0,0,0,1,1,1,1,1],#7
'8':
[0,0,0,0,0,0,0,1],#8
'9':
[0,0,0,0,1,0,0,1],#9
}
 
def display(num,dp):
    global number
    count=0
    for pin in d_Pins:#显示num的值
        pin.value(number[num][count])
        count+=1
    if dp==1:
        d_Pins[7].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[3-i].value(1)
            if i==location-1:
                display(d_num[i],1)
            else:
                display(d_num[i],0)
    if location<0:
        for i in range(0,4):
            pyb.udelay(2000)
            clear()
            w_Pins[3-i].value(1)
            display(d_num[i],0)
while True:
 
    num='9016'
    showData(num)

到此,关于“python怎么实现数码管显示”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI