一、實驗要求
利用樹莓派驅動數碼管,使其能顯示時間和日期。通過按鍵,可以在時間和日期之間切換
二、實驗材料
- 按鍵1個
- 4位8段數碼管(共陰極) 1個
- 杜邦線若干
- 樹莓派4B、RPi.GPIO庫
三、原理介紹
關於如何驅動數碼管請參考:《樹莓派GPIO入門(四) :用數碼管做個簡單的計數器》
四、實驗代碼
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
# 定義單個數碼管各段led對應的GPIO口
LED_A = 26
LED_B = 19
LED_C = 13
LED_D = 6
LED_E = 5
LED_F = 22
LED_G = 27
LED_DP = 17
LED_pins = [LED_A,LED_B,LED_C,LED_D,LED_E,LED_F,LED_G]
# 定義1到4號數碼管陰極對應的GPIO口
DIG1 = 12
DIG2 = 16
DIG3 = 20
DIG4 = 21
All_dig = [DIG1, DIG2, DIG3, DIG4]
All_pins = [LED_A,LED_B,LED_C,LED_D,LED_E,LED_F,LED_G,LED_DP,DIG1,DIG2,DIG3,DIG4]
# 定義按鈕輸入的GPIO口
btn = 24
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP) #在24號引腳處設置上拉電阻
#將數碼管LED段引腳設置爲輸出模式
GPIO.setup(All_pins, GPIO.OUT)
#0-9的各段LED狀態,1表示高電平,0表示低電平
zero = [1, 1, 1, 1, 1, 1, 0]
one = [0, 1, 1, 0, 0, 0, 0]
two = [1, 1, 0, 1, 1, 0, 1]
three = [1, 1, 1, 1, 0, 0, 1]
four = [0, 1, 1, 0, 0, 1, 1]
five = [1, 0, 1, 1, 0, 1, 1]
six = [1, 0, 1, 1, 1, 1, 1]
seven = [1, 1, 1, 0, 0, 0, 0]
eight = [1, 1, 1, 1, 1, 1, 1]
nine = [1, 1, 1, 1, 0, 1, 1]
#初始化,讓所有LED段熄滅狀態
GPIO.output(All_pins, False)
def showDigit(order, num, showDotPoint):
# 先將負極拉高,關掉顯示
GPIO.output(All_dig, True)
if (num == 0):
GPIO.output(LED_pins, zero)
GPIO.output(LED_DP, showDotPoint)
elif (num == 1):
GPIO.output(LED_pins, one)
GPIO.output(LED_DP, showDotPoint)
elif (num == 2):
GPIO.output(LED_pins, two)
GPIO.output(LED_DP, showDotPoint)
elif (num == 3):
GPIO.output(LED_pins, three)
GPIO.output(LED_DP, showDotPoint)
elif (num == 4):
GPIO.output(LED_pins, four)
GPIO.output(LED_DP, showDotPoint)
elif (num == 5):
GPIO.output(LED_pins, five)
GPIO.output(LED_DP, showDotPoint)
elif (num == 6):
GPIO.output(LED_pins, six)
GPIO.output(LED_DP, showDotPoint)
elif (num == 7):
GPIO.output(LED_pins, seven)
GPIO.output(LED_DP, showDotPoint)
elif (num == 8):
GPIO.output(LED_pins, eight)
GPIO.output(LED_DP, showDotPoint)
elif (num == 9):
GPIO.output(LED_pins, nine)
GPIO.output(LED_DP, showDotPoint)
if (order == 1):
GPIO.output(DIG1, False)
elif (order == 2):
GPIO.output(DIG2, False)
elif (order == 3):
GPIO.output(DIG3, False)
elif (order == 4):
GPIO.output(DIG4, False)
try:
t=0.005
while True:
# 按鈕按下時顯示日期,否則顯示時間
# 爲了區別左右的數字,讓第二個數碼管的小數點顯示出來
#(本來應該是一個冒號,我們這個數碼管沒有,就用小數點代替了)
if (GPIO.input(btn) == 0):
time.sleep(t)
showDigit(1, int(int(time.strftime("%m",time.localtime()))/10), False) #月份的第一位
time.sleep(t)
showDigit(2, int(int(time.strftime("%m",time.localtime()))%10), True) #月份的第二位
time.sleep(t)
showDigit(3, int(int(time.strftime("%d",time.localtime()))/10), False) #日的第一位
time.sleep(t)
showDigit(4, int(int(time.strftime("%d",time.localtime()))%10), False) #日的第二位
else:
time.sleep(t)
showDigit(1, int(int(time.strftime("%M",time.localtime()))/10), False) #時的第一位
time.sleep(t)
showDigit(2, int(int(time.strftime("%M",time.localtime()))%10), True) #時的第二位
time.sleep(t)
showDigit(3, int(int(time.strftime("%S",time.localtime()))/10), False) #分的第一位
time.sleep(t)
showDigit(4, int(int(time.strftime("%S",time.localtime()))%10), False) #分的第二位
except KeyboardInterrupt:
pass
GPIO.cleanup() #最後清理GPIO口
五、視頻演示
由於採用的是四位數碼管,所以在顯示時間時只能顯示出當前時間的分和秒,日期只能顯示月和日。
簡易電子鐘