溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python怎么實現(xiàn)數(shù)碼管顯示

發(fā)布時間:2022-01-13 10:34:06 來源:億速云 閱讀:189 作者:iii 欄目:互聯(lián)網(wǎng)科技

這篇文章主要介紹“python怎么實現(xiàn)數(shù)碼管顯示”,在日常操作中,相信很多人在python怎么實現(xiàn)數(shù)碼管顯示問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”python怎么實現(xiàn)數(shù)碼管顯示”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

import pyb
from pyb import Pin
 
#數(shù)碼管a~g dp對應(yīng)的開發(fā)板引腳
d_Pins=[Pin(i,Pin.OUT_PP) for i in ['X1','X2','X3','X4','X5','X6','X7','X8']]
#數(shù)碼管位段1 2 3 4對應(yīng)的引腳
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):
    #分割出數(shù)值的百位、千位、個位和小數(shù)位的值
    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   四位數(shù)碼管一個
    3   杜邦線若干
    4   面包板一塊(為了接線方便,可不用)

    接線圖
    四位數(shù)碼管引腳圖

python怎么實現(xiàn)數(shù)碼管顯示

python怎么實現(xiàn)數(shù)碼管顯示
    開發(fā)板與四位數(shù)碼管針腳對應(yīng):

python怎么實現(xiàn)數(shù)碼管顯示
    程序源代碼:

import pyb
from pyb import Pin
 
#數(shù)碼管a~g dp對應(yīng)的開發(fā)板引腳
d_Pins=[Pin(i,Pin.OUT_PP) for i in ['X1','X2','X3','X4','X5','X6','X7','X8']]
#數(shù)碼管位段1 2 3 4對應(yīng)的引腳
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):
    #分割出數(shù)值的百位、千位、個位和小數(shù)位的值
    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)

到此,關(guān)于“python怎么實現(xiàn)數(shù)碼管顯示”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI