原理非常简单,控制各个针脚的高低电平来显示数字,这里我用的是共阳的数码管。不过有一个问题,我第一次显示的时候代码输入数字是9.876不过数码管显示9.875,刚开始我以为代码错了,仔细检查了几遍没有问题。然后用putty跑了一下,打印出来的是9875999  。后来又试了几组数字有的正常显示有的最末尾会减一。求助各位大佬到底为什么?

所需原器件

1   TPYBoard v102 一块

2   四位数码管一个

3   杜邦线若干

4   面包板一块(为了接线方便,可不用)

接线图

四位数码管引脚图

python 绘制数码管 micropython数码管_数码管

python 绘制数码管 micropython数码管_开发板_02



 

开发板与四位数码管针脚对应:

数码管引脚

a

b

c

d

e

f

g

dp

1

2

3

4

TPYBoard开发板

X1

X2

X3

X4

X5

X6

X7

X8

Y9

Y10

Y11

Y12

fritzing接线图

python 绘制数码管 micropython数码管_python_03

  程序源代码:

 

 

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='9.601'   
         showData(num)