树莓派日记玩转树莓派树莓派

树莓派使用F5461BH

2020-03-15  本文已影响0人  坐怀乱

F5461BH:八位共阳极数码管

外观

F5461BH

小数点所在的一侧为下

引脚

以左下角的引脚为1,逆时针旋转计数,左上角为12。

12 11 10 9 8 7
b BIT2 BIT3 f a BIT4
e d p c g BIT1
1 2 3 4 5 6

直观一点:

     __a__         __a__          __a__          __a__
    |     |       |     |        |     |        |     |
    f     b       f     b        f     b        f     b
    |__g__|       |__g__|        |__g__|        |__g__|
    |     |       |     |        |     |        |     |
    e     c       e     c        e     c        e     c
    |__d__| .dp   |__d__| .dp    |__d__| .dp    |__d__| .dp 
      BIT4          BIT3           BIT2           BIT1 

对于共阳极数码管

BIT[1-4]对应从左往右数各数位,高电平显示,低电平隐藏

a-g对应各数码管,低电平显示,高电平隐藏

使用

12个引脚接上树莓派12个GPIO口后,可使用python或shell直接操作GPIO口电平。

# encoding: utf-8

import RPi.GPIO as GPIO
import time

BIT1 = 8    #数码管1
BIT2 = 5    #数码管2
BIT3 = 6    #数码管3
BIT4 = 26  #数码管4

#将0-9转换成数码管对应的16进制表示
#例:0
#对应数码管:
#   __a__      共阳极数码管0亮1不亮
# |       |    p g f e d c b a
# f       b    1 1 0 0 0 0 0 0
# |       |    转换为16进制
# |       |    c  0
# e       c    0xc0
# | __d__ |    所以0对应0xc0

transCode = [0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90]# 使用的gpio口,顺序无要求

pins = [19,22,12,20,21,13,7,16,8,6,5,26]
bits = [BIT1, BIT2, BIT3, BIT4]

def digitalWriteByte(val):
  GPIO.output(19, val & (0x01 << 0))
  GPIO.output(22, val & (0x01 << 1))
  GPIO.output(12, val & (0x01 << 2))
  GPIO.output(20, val & (0x01 << 3))
  GPIO.output(21, val & (0x01 << 4))
  GPIO.output(13, val & (0x01 << 5))
  GPIO.output(7, val & (0x01 << 6))
  GPIO.output(16, val & (0x01 << 7))

def hide():
  GPIO.output(BIT1, GPIO.LOW)
  GPIO.output(BIT2, GPIO.LOW)
  GPIO.output(BIT3, GPIO.LOW)
  GPIO.output(BIT4, GPIO.LOW)

def show():
  GPIO.output(BIT1, GPIO.HIGH)
  GPIO.output(BIT2, GPIO.HIGH)
  GPIO.output(BIT3, GPIO.HIGH)
  GPIO.output(BIT4, GPIO.HIGH)

def showNum(bit,num):
  hide()
  GPIO.output(bits[bit], GPIO.HIGH)
  digitalWriteByte(transCode[num])
  time.sleep(0.005)

def display(num):
  b0 = num % 10
  b1 = int(num % 100 / 10)
  b2 = int(num % 1000 / 100)
  b3 = int(num / 1000)
  if num < 10:
    showNum(0,b0)
  elif num >= 10 and num < 100:
    showNum(0,b0)
    showNum(1,b1)
  elif num >= 100 and num < 1000:
    showNum(0,b0)
    showNum(1,b1)
    showNum(2,b2)
  elif num >= 1000 and num < 10000:
    showNum(0,b0)
    showNum(1,b1)
    showNum(2,b2)
    showNum(3,b3)

def setup():
  GPIO.setmode(GPIO.BCM)
  for pin in pins:
    GPIO.setup(pin, GPIO.OUT)
    GPIO.output(pin, GPIO.HIGH)

def loop():
  while True:
    num = int(input("跟哥说说想看几:"))
    if num < 10000:
      for i in range(50):
        display(num)
    else:
      print ("哥,五位数显示不来。")
    hide()

def destroy():
  for pin in pins:
    GPIO.output(pin, GPIO.LOW)
    GPIO.setup(pin, GPIO.IN)

if __name__ == '__main__':
  setup()
  try:
    loop()
  except KeyboardInterrupt:
    destroy()

注意事项

2020-03-15:女朋友比板子重要的多,少玩板子多陪姑娘。

上一篇下一篇

猜你喜欢

热点阅读