树莓派GPIO入门(五) :简易电子钟

一、实验要求

利用树莓派驱动数码管,使其能显示时间和日期。通过按键,可以在时间和日期之间切换

二、实验材料

  • 按键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口

五、视频演示

由于采用的是四位数码管,所以在显示时间时只能显示出当前时间的分和秒,日期只能显示月和日。 

简易电子钟

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章