四位共阴管从上手到搞定

四位共阴管从上手到搞定_第1张图片

这个图里的1,2,3,4就是公共端,也就是分别管着对应的第1-4位接低电势或者接高电势(取决于你买了共阴还是共阳),剩下的a~h就是8个led小管子。 

举个形象的例子,比如让第一个位数的b小灯泡亮,可以这么干:

四位共阴管从上手到搞定_第2张图片

接下来废话少说 上代码,有大妈亲测的0~9二进制表达:

# 4位共阴管
"""
tubeA -> GPIO0(Pin27)
tubeB -> GPIO6(Pin31)
tubeC -> GPIO7(Pin26)
tubeD -> GPIO20(Pin38)
tubeE -> GPIO21(Pin40)
tubeF -> GPIO5(Pin29)
tubeG -> GPIO1(Pin28)
tubeH -> GPIO16(Pin36)  #小数点位

G1 -> GPIO22(Pin15)
G2 -> GPIO26(Pin37)
G3 -> GPIO17(Pin11)
G4 -> GPIO14(Pin8)
"""

import RPi.GPIO as GPIO
import random
import time

NUMBER_OF_DIGITS = 4

def format_num(num):
    digits_left = NUMBER_OF_DIGITS
    format_str = ''
    if num < 0:
        digits_left -= 1
        format_str += '-'
        num *= -1
    if isinstance(num, int):
        len_int = len(str(num))
    else:
        len_int = len(str(int(round(num))))
    if len_int > digits_left:
        return ''
    else:
        if isinstance(num, int):
            format_str += '%d'
        else:
            len_frac = digits_left - len_int
            format_str += '%%.%df' % len_frac
    return format_str % num

GPIO.setmode(GPIO.BCM) #用BCM就是用GPIO序号,用BOARD就是用树莓派物理上的pin位置。

# GPIO ports for the 7seg pins
segments = (0,6,7,20,21,5,1,16)

for segment in segments:
    GPIO.setup(segment, GPIO.OUT)
    GPIO.output(segment, 1)

# GPIO ports for the digit 0-3 pins
digits = (22,26,17,14)

for digit in digits:
    GPIO.setup(digit, GPIO.OUT)
    GPIO.output(digit, 1)

#大妈顶着老花近视眼,笨办法个个数字调试出来的。前提第一个' '一定要对。
num = {
    ' ':(0,0,0,0,0,0,0),
    '-':(0,0,1,0,0,0,0),
    '6':(0,1,1,1,1,1,1),
    '1':(0,0,0,0,1,1,0),
    '8':(1,1,1,1,1,1,1),
    '5':(0,1,1,1,0,1,1),
    '0':(1,1,0,1,1,1,1),
    '9':(1,1,1,1,0,1,1),
    '4':(1,0,1,0,0,1,1),
    '2':(1,1,1,1,1,0,0),
    '3':(1,1,1,1,0,0,1),
    '7':(1,1,0,0,0,1,1)
}

RAND_MAX = (10 ** NUMBER_OF_DIGITS) - 1
RAND_MIN = (-10 ** (NUMBER_OF_DIGITS - 1)) + 1

r = random.uniform(-1, 1)
max_scale = NUMBER_OF_DIGITS
if r < 0:
    max_scale -= 1
n = r * (10 ** random.randint(0, max_scale))
print(n)
next_mode = 'int'
last_change = time.time()

try:
    while True:
        s = format_num(n).rjust(NUMBER_OF_DIGITS)
        #s = format_num(4).rjust(NUMBER_OF_DIGITS) #猜单个数字的byte的时候用的。
        digit = 0
        for char in s:
            if char == '.':
                continue
            for loop in range(0,7):
                GPIO.output(segments[loop], num[char][loop])
                if digit < (NUMBER_OF_DIGITS-1) and s[digit+1] == '.':
                    GPIO.output(16, 1)
                else:
                    GPIO.output(16, 0)
            GPIO.output(digits[digit], 0)
            time.sleep(0.001)
            GPIO.output(digits[digit], 1)
            digit += 1
        if (time.time() - last_change) > 2:
            r = random.uniform(-1, 1)
            max_scale = NUMBER_OF_DIGITS
            if r < 0:
                max_scale -= 1
            n = r * (10 ** random.randint(0, max_scale))
            if next_mode == 'int':
                n = int(n)
                next_mode = 'float'
            elif next_mode == 'float':
                next_mode = 'int'
            print(n)
            last_change = time.time()
finally:
    GPIO.cleanup()

你可能感兴趣的:(pcb上有颗芯,树莓派,4位共阴管)