03.LED時鐘

-----程式碼----------------

import RPi.GPIO as GPIO #使用GPIO模組(GPIO.setmode)

import time , threading #使用時間 (time.sleep),與執行緒()

NUMBER = [0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F]

NUMBER_TEXT = [0,0,0,0]

# GPIO DEFINE

seg_data_list = [3,5,7,8,10,11,12,13]

seg_com_list = [15,16,18,19]

global time_int = 5

dot = 0

# Use physical pin numbers

GPIO.setmode(GPIO.BOARD)

def Timer1():

global dot

if dot == 1 :

dot = 0

else:

dot = 1

#local = time.localtime(time.time())

#NUMBER_TEXT[0] = int(local.tm_hour / 10)

#NUMBER_TEXT[1] = int(local.tm_hour % 10)

#NUMBER_TEXT[2] = int(local.tm_min / 10)

#NUMBER_TEXT[3] = int(local.tm_min % 10)

for x in range(10):

NUMBER_TEXT[0] = x

time.sleep(0.2)

global t1

t1 = threading.Timer(5 , Timer1)

t1.start()

# MAIN

# 7SEG DATA

for i in range(8):

GPIO.setup(seg_data_list[i], GPIO.OUT)

for i in range(8):

GPIO.output(seg_data_list[i], True)

# 7SEG COM SCAN

for i in range(4):

GPIO.setup(seg_com_list[i], GPIO.OUT)

for i in range(4):

GPIO.output(seg_com_list[i], True)

SegCount = 0

dot = 0

print ("03.7SEG CLOCK DEMO Starting... ")

t1 = threading.Timer(0.5 , Timer1)

t1.start()

while True:

# close scan

for i in range(4):

GPIO.output(seg_com_list[i], True)

time.sleep(0.001)

seg7_data = NUMBER[NUMBER_TEXT[SegCount]];

# data

if dot == 1 :

seg7_data = seg7_data | 0x80

else:

seg7_data = seg7_data & 0x7F

data_index = 0x01

for i in range(8):

if (seg7_data & data_index) == 0 :

GPIO.output(seg_data_list[i], True)

else:

GPIO.output(seg_data_list[i], False)

data_index = data_index << 1

# open scan

GPIO.output(seg_com_list[SegCount], False)

time.sleep(0.003)

SegCount = SegCount + 1

SegCount = SegCount % 4