.. note::
こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。
**参加する理由は?**
- **エキスパートサポート**:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
- **学び&共有**:ヒントやチュートリアルを交換してスキルを向上させましょう。
- **独占的なプレビュー**:新製品の発表や先行プレビューに早期アクセスしましょう。
- **特別割引**:最新製品の独占割引をお楽しみください。
- **祭りのプロモーションとギフト**:ギフトや祝日のプロモーションに参加しましょう。
👉 私たちと一緒に探索し、創造する準備はできていますか?[|link_sf_facebook|]をクリックして今すぐ参加しましょう!
.. _1.1.5_py:
1.1.5 4桁の7セグメントディスプレイ
====================================
はじめに
-----------------
次に、4桁の7セグメントディスプレイの制御を一緒に試してみましょう。
必要な部品
------------------------------
このプロジェクトでは、以下のコンポーネントが必要です。
.. image:: ../img/list_4_digit.png
キット全体を購入するのは確かに便利です、購入先のリンクはこちらです:
.. list-table::
:widths: 20 20 20
:header-rows: 1
* - 名前
- このキットのアイテム
- リンク
* - Raphael K
- 337
- |link_Raphael_kit|
以下のリンクから個別にも購入できます。
.. list-table::
:widths: 30 20
:header-rows: 1
* - コンポーネントの紹介
- 購入先のリンク
* - :ref:`cpn_gpio_board`
- |link_gpio_board_buy|
* - :ref:`cpn_breadboard`
- |link_breadboard_buy|
* - :ref:`cpn_wires`
- |link_wires_buy|
* - :ref:`cpn_resistor`
- |link_resistor_buy|
* - :ref:`cpn_4_digit`
- \-
* - :ref:`cpn_74hc595`
- |link_74hc595_buy|
回路図
--------------------------
============ ======== ======== ===
T-Board Name physical wiringPi BCM
GPIO17 Pin 11 0 17
GPIO27 Pin 13 2 27
GPIO22 Pin 15 3 22
SPIMOSI Pin 19 12 10
GPIO18 Pin 12 1 18
GPIO23 Pin 16 4 23
GPIO24 Pin 18 5 24
============ ======== ======== ===
.. image:: ../img/schmatic_4_digit.png
実験手順
-----------------------------------
**ステップ1**: 回路を組み立てます。
.. image:: ../img/image80.png
**ステップ2**: コードが保存されているフォルダに移動します。
.. raw:: html
.. code-block::
cd ~/raphael-kit/python/
**ステップ3**: 実行可能ファイルを実行します。
.. raw:: html
.. code-block::
sudo python3 1.1.5_4-Digit.py
コードを実行後、プログラムはカウントを開始し、1秒ごとに1増加します。4桁のディスプレイにはそのカウントが表示されます。
**コード**
.. note::
下記のコードを **修正/リセット/コピー/実行/停止** することができます。ただし、その前にソースコードのパス(例: ``raphael-kit/python`` )に移動する必要があります。コードを修正した後、その効果を直接確認することができます。
.. raw:: html
.. code-block:: python
import RPi.GPIO as GPIO
import time
import threading
SDI = 24
RCLK = 23
SRCLK = 18
placePin = (10, 22, 27, 17)
number = (0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90)
counter = 0
timer1 = 0
def clearDisplay():
for i in range(8):
GPIO.output(SDI, 1)
GPIO.output(SRCLK, GPIO.HIGH)
GPIO.output(SRCLK, GPIO.LOW)
GPIO.output(RCLK, GPIO.HIGH)
GPIO.output(RCLK, GPIO.LOW)
def hc595_shift(data):
for i in range(8):
GPIO.output(SDI, 0x80 & (data << i))
GPIO.output(SRCLK, GPIO.HIGH)
GPIO.output(SRCLK, GPIO.LOW)
GPIO.output(RCLK, GPIO.HIGH)
GPIO.output(RCLK, GPIO.LOW)
def pickDigit(digit):
for i in placePin:
GPIO.output(i,GPIO.LOW)
GPIO.output(placePin[digit], GPIO.HIGH)
def timer():
global counter
global timer1
timer1 = threading.Timer(1.0, timer)
timer1.start()
counter += 1
print("%d" % counter)
def loop():
global counter
while True:
clearDisplay()
pickDigit(0)
hc595_shift(number[counter % 10])
clearDisplay()
pickDigit(1)
hc595_shift(number[counter % 100//10])
clearDisplay()
pickDigit(2)
hc595_shift(number[counter % 1000//100])
clearDisplay()
pickDigit(3)
hc595_shift(number[counter % 10000//1000])
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(SDI, GPIO.OUT)
GPIO.setup(RCLK, GPIO.OUT)
GPIO.setup(SRCLK, GPIO.OUT)
for i in placePin:
GPIO.setup(i, GPIO.OUT)
global timer1
timer1 = threading.Timer(1.0, timer)
timer1.start()
def destroy(): # When "Ctrl+C" is pressed, the function is executed.
global timer1
GPIO.cleanup()
timer1.cancel() # cancel the timer
if __name__ == '__main__':
setup()
try:
loop()
except KeyboardInterrupt:
destroy()
**コード説明**
.. code-block:: python
placePin = (10, 22, 27, 17)
この4つのピンは、4桁の7セグメントディスプレイの共通アノードピンを制御します。
.. code-block:: python
number = (0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90)
これは0から9までのセグメントコードの配列です(共通アノード、16進数表記)。
.. code-block:: python
def clearDisplay():
for i in range(8):
GPIO.output(SDI, 1)
GPIO.output(SRCLK, GPIO.HIGH)
GPIO.output(SRCLK, GPIO.LOW)
GPIO.output(RCLK, GPIO.HIGH)
GPIO.output(RCLK, GPIO.LOW)
8回「1」をSDIに書き込むことで、7セグメントディスプレイ上の8つのLEDが消灯し、表示内容がクリアされます。
.. code-block:: python
def pickDigit(digit):
for i in placePin:
GPIO.output(i,GPIO.LOW)
GPIO.output(placePin[digit], GPIO.HIGH)
値の位置を選択します。一度に有効にすべきは1つの位置だけです。有効な位置は高い状態にされます。
.. code-block:: python
def loop():
global counter
while True:
clearDisplay()
pickDigit(0)
hc595_shift(number[counter % 10])
clearDisplay()
pickDigit(1)
hc595_shift(number[counter % 100//10])
clearDisplay()
pickDigit(2)
hc595_shift(number[counter % 1000//100])
clearDisplay()
pickDigit(3)
hc595_shift(number[counter % 10000//1000])
この関数は、4桁の7セグメントディスプレイに表示される数字を設定します。
まず、最も右の桁(一桁目)から表示を始め、次に十の位、百の位、千の位と進めます。表示が高速に更新されるため、完全な4桁の数字が表示されるように見えます。
.. code-block:: python
timer1 = threading.Timer(1.0, timer)
timer1.start()
こちらはPythonの一般的なスレッドモジュールであるthreadingのサブクラス、Timerを使用しています。
プロトタイプは次の通りです。
.. code-block:: python
class threading.Timer(interval, function, args=[], kwargs={})
指定された間隔が経過すると、関数が実行されます。ここでは、間隔が1.0で、関数はtimer()です。
start()はこの時点でタイマーが開始されることを意味します。
.. code-block:: python
def timer():
global counter
global timer1
timer1 = threading.Timer(1.0, timer)
timer1.start()
counter += 1
print("%d" % counter)
タイマーが1.0秒経過すると、timer関数が呼び出され、counterに1が加算されます。そして、タイマーは再び1秒ごとに自身を繰り返し実行します。
現象の画像
------------------------
.. image:: ../img/image81.jpeg