Skip to content

Instantly share code, notes, and snippets.

@u-one
Last active December 26, 2021 09:07
Show Gist options
  • Save u-one/557f05d84563e8632968861770328862 to your computer and use it in GitHub Desktop.
Save u-one/557f05d84563e8632968861770328862 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# aitendoの"4-Bit LED Digital Tube Module" 4桁7セグLEDモジュール[TM4D595]を
# raspbery pi zeroで駆動するプログラム
# 現在時刻を表示する
import RPi.GPIO as GPIO
import time
from datetime import datetime
PIN_SCLK = 17
PIN_RCLK = 27
PIN_DI = 22
CHAR = {
0 : 0xC0, #0
1 : 0xF9, #1
2 : 0xA4, #2
3 : 0xB0, #3
4 : 0x99, #4
5 : 0x92, #5
6 : 0x82, #6
7 : 0xF8, #7
8 : 0x80, #8
9 : 0x90, #9
10 : 0x88, #A
11 : 0x83, #b
12 : 0xC6, #C
13 : 0xA1, #d
14 : 0x86, #E
15 : 0x8E, #F
16 : 0xC2, #G
17 : 0x89, #H
18 : 0xF9, #I
19 : 0xF1, #J
20 : 0xC3, #L
21 : 0xA9, #n
22 : 0xC0, #O
23 : 0x8C, #P
24 : 0x98, #q
25 : 0x92, #S
26 : 0xC1, #U
27 : 0x91, #Y
28 : 0xFE, #hight -
}
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_SCLK,GPIO.OUT)
GPIO.setup(PIN_RCLK,GPIO.OUT)
GPIO.setup(PIN_DI,GPIO.OUT)
def clock():
while True:
now = datetime.now().strftime("%H%M")
digit4(int(now),10)
time.sleep(100/1000) # 適当に 100ms に一回更新
def digit4(val, repeat):
n1 = val % 10
n2 = (val % 100) / 10
n3 = (val % 1000) / 100
n4 = (val % 10000) / 1000
for i in range(repeat):
send_with_port(CHAR[n1], 0b0001)
if n2 >= 0:
send_with_port(CHAR[n2], 0b0010)
if n3 >= 0:
send_with_port(CHAR[n3], 0b0100)
if n4 >= 0:
send_with_port(CHAR[n4], 0b1000)
def send_with_port(val, port):
send(val)
send(port)
set_rclk(0)
set_rclk(1)
def send(val):
for i in range(8,0,-1):
if (val & 0x80):
set_dio(1)
else:
set_dio(0)
val = val << 1
set_sclk(0)
set_sclk(1)
def set_dio(val):
set_pin(PIN_DI, val)
#print("DIO:" + str(val))
def set_sclk(val):
set_pin(PIN_SCLK, val)
#print("SCLK:" + str(val))
def set_rclk(val):
set_pin(PIN_RCLK, val)
#print("RCLK:" + str(val))
def set_pin(pin, val):
if val == 0:
GPIO.output(pin, False)
else:
GPIO.output(pin, True)
setup()
clock()
GPIO.cleanup()
# -*- coding: utf-8 -*-
# aitendoの"4-Bit LED Digital Tube Module" 4桁7セグLEDモジュール[TM4D595]を
# raspbery pi zeroで駆動するプログラム
import RPi.GPIO as GPIO
import time
from datetime import datetime
PIN_SCLK = 17
PIN_RCLK = 27
PIN_DI = 22
CHAR = {
0 : 0xC0, #0
1 : 0xF9, #1
2 : 0xA4, #2
3 : 0xB0, #3
4 : 0x99, #4
5 : 0x92, #5
6 : 0x82, #6
7 : 0xF8, #7
8 : 0x80, #8
9 : 0x90, #9
10 : 0x88, #A
11 : 0x83, #b
12 : 0xC6, #C
13 : 0xA1, #d
14 : 0x86, #E
15 : 0x8E, #F
16 : 0xC2, #G
17 : 0x89, #H
18 : 0xF9, #I
19 : 0xF1, #J
20 : 0xC3, #L
21 : 0xA9, #n
22 : 0xC0, #O
23 : 0x8C, #P
24 : 0x98, #q
25 : 0x92, #S
26 : 0xC1, #U
27 : 0x91, #Y
28 : 0xFE, #hight -
}
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_SCLK,GPIO.OUT)
GPIO.setup(PIN_RCLK,GPIO.OUT)
GPIO.setup(PIN_DI,GPIO.OUT)
def set_pin(pin, val):
if val == 0:
GPIO.output(pin, False)
else:
GPIO.output(pin, True)
def set_dio(val):
set_pin(PIN_DI, val)
#print("DIO:" + str(val))
def set_sclk(val):
set_pin(PIN_SCLK, val)
#print("SCLK:" + str(val))
def set_rclk(val):
set_pin(PIN_RCLK, val)
#print("RCLK:" + str(val))
def send(val):
for i in range(8,0,-1):
if (val & 0x80):
set_dio(1)
else:
set_dio(0)
val = val << 1
set_sclk(0)
set_sclk(1)
def send_with_port(val, port):
send(val)
send(port)
set_rclk(0)
set_rclk(1)
def digit4(val, repeat):
n1 = val % 10
n2 = (val % 100) / 10
n3 = (val % 1000) / 100
n4 = (val % 10000) / 1000
for i in range(repeat):
send_with_port(CHAR[n1], 0b0001)
if n2 >= 0:
send_with_port(CHAR[n2], 0b0010)
if n3 >= 0:
send_with_port(CHAR[n3], 0b0100)
if n4 >= 0:
send_with_port(CHAR[n4], 0b1000)
def digit_all(val):
send_with_port(CHAR[val], 0b1111)
def digit(val):
send_with_port(val, 0b0001)
setup()
def segment_test():
list = [0b1, 0b10, 0b100, 0b1000, 0b10000, 0b100000, 0b1000000, 0b10000000]
# _ _ _ _ _ _ _
# |_| |_ |_| |_| |_| _| | | |_|
# |_| |_| |_ | | _| |_| |_| |_|
#
# _
# |_| 0b0011100
#
#
#
# _ 0b0100011
# |_|
#
# _
# |_|
# |_|
#
for i in list:
digit(i)
time.sleep(1)
#
#digit(0b1111111)
segment_test()
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment