Raspbian Jessie: Ausgänge des MCP23017 ansteuern
Der MCP23017 ist ein 16 Bit I/O Expander welcher auch am I²C-Bus des Raspberry Pi's eingesetzt werden kann. Dadurch kann man den Raspberry Pi um weitere 16 digitale Eingänge beziehungsweise Ausgänge erweitern. Über die Pins A0 - A2 können Sie dem MCP23017 Baustein mit einer individuellen I²C-Adresse von 0x20 bis 0x27 konfigurieren. Das bedeutet Sie können bis zu 8 Bausteine direkt am I²C-Bus des Raspberry Pi's betreiben.
In dieser Anleitung wird anhand eines einfachen Lauflichts gezeigt wie Sie die 16 digitalen Ports des MCP23017 in einem Python Skript als Ausgänge verwenden können.
I²C-Bus des Raspberry Pi's aktivieren
Bevor Sie den I²C-Bus des Raspberry Pi's unter Raspbian verwenden können, müssen Sie diesen aktivieren. Wie dies durchgeführt wird können Sie in der Anleitung Raspbian Wheezy: I²C-Unterstützung im Device Tree aktivieren nachlesen.
MCP23017 Adresse am I²C-Bus herausfinden
Damit Sie den MCP23017 ansprechen können, benötigen Sie dessen I²C Slave Adresse. Diese können Sie wie bereits in der Anleitung Raspbian Wheezy: Teilnehmer am I²C-Bus identifizieren beschrieben mit dem Befehl i2cdetect herausfinden. Wie Sie der folgenden Ausgabe entnehmen können, besitzt in diesem Fall der MCP23017 die Adresse 0x20. Über die Pins A0 - A2 können Sie eine individuelle I²C Adresse von 0x20 bis 0x27 konfigurieren.
pi@raspberrypi ~ $ sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Benötigte Python Module installieren
Als zusätzliches Python Modul wird das Paket python-smbus benötigt. Dieses enthält die Funktionalitäten für den Zugriff auf den System Management Bus (SMBus) und kann wie unter Raspbian Jessie: Python Modul für den SMBus-Zugriff installieren beschrieben installiert werden.
Python Skript für ein 8 Kanal Lauflicht erstellen
Für das folgende Python Skript erstellen Sie mit einem Editor die Datei mcp23017_8-bit_output.py.
pi@raspberrypi ~ $ vi mcp23017_8-bit_output.py
Fügen Sie in diese Datei den folgenden Python Code ein und speichern die Datei ab.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Creation: 07.04.2015
# Last Update: 18.02.2017
#
# Copyright (c) 2015-2017 by Georg Kainzbauer <http://www.gtkdb.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# import required modules
import smbus
import time
# define I2C bus
# 0: Raspberry Pi Model B Rev 1.0
# 1: Raspberry Pi Model B Rev 2.0, Model A, Model B+, Model A+, Raspberry Pi 2 Model B and Raspberry Pi 3 Model B
I2C_BUS = 1
# define I2C address of MCP23017 16-Bit I/O expander
# depends on the hardware pins A0 - A2
I2C_ADDR = 0x20
# define I/O direction (IODIR) registers
IODIRA = 0x00
IODIRB = 0x10
# define output latch (OLAT) registers
OLATA = 0x14
OLATB = 0x15
# main function
def main():
try:
# init I2C bus
i2c = smbus.SMBus(I2C_BUS)
# configure all GPA pins as outputs
i2c.write_byte_data(I2C_ADDR, IODIRA, 0x00)
# configure all GPB pins as outputs
i2c.write_byte_data(I2C_ADDR, IODIRB, 0x00)
# set all output pins to 0
i2c.write_byte_data(I2C_ADDR, OLATA, 0x00)
i2c.write_byte_data(I2C_ADDR, OLATB, 0x00)
# initialize output variable
data = 0x01
while True:
# get bit shift direction
# 0: shift to left
# 1: shift to right
if data == 0x01:
direction = 0
elif data == 0x80:
direction = 1
# output data on GPA port
i2c.write_byte_data(I2C_ADDR, OLATA, (data & 0xFF))
# shift bits
if direction == 0:
data = data << 1
else:
data = data >> 1
# wait 100ms
time.sleep(0.1)
except KeyboardInterrupt:
print("Execution stopped by user")
if __name__ == '__main__':
main()
# -*- coding: utf-8 -*-
#
# Creation: 07.04.2015
# Last Update: 18.02.2017
#
# Copyright (c) 2015-2017 by Georg Kainzbauer <http://www.gtkdb.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# import required modules
import smbus
import time
# define I2C bus
# 0: Raspberry Pi Model B Rev 1.0
# 1: Raspberry Pi Model B Rev 2.0, Model A, Model B+, Model A+, Raspberry Pi 2 Model B and Raspberry Pi 3 Model B
I2C_BUS = 1
# define I2C address of MCP23017 16-Bit I/O expander
# depends on the hardware pins A0 - A2
I2C_ADDR = 0x20
# define I/O direction (IODIR) registers
IODIRA = 0x00
IODIRB = 0x10
# define output latch (OLAT) registers
OLATA = 0x14
OLATB = 0x15
# main function
def main():
try:
# init I2C bus
i2c = smbus.SMBus(I2C_BUS)
# configure all GPA pins as outputs
i2c.write_byte_data(I2C_ADDR, IODIRA, 0x00)
# configure all GPB pins as outputs
i2c.write_byte_data(I2C_ADDR, IODIRB, 0x00)
# set all output pins to 0
i2c.write_byte_data(I2C_ADDR, OLATA, 0x00)
i2c.write_byte_data(I2C_ADDR, OLATB, 0x00)
# initialize output variable
data = 0x01
while True:
# get bit shift direction
# 0: shift to left
# 1: shift to right
if data == 0x01:
direction = 0
elif data == 0x80:
direction = 1
# output data on GPA port
i2c.write_byte_data(I2C_ADDR, OLATA, (data & 0xFF))
# shift bits
if direction == 0:
data = data << 1
else:
data = data >> 1
# wait 100ms
time.sleep(0.1)
except KeyboardInterrupt:
print("Execution stopped by user")
if __name__ == '__main__':
main()
Nachdem Sie Das Python Skript erstellt haben, sollten Sie die Rechte der Datei anpassen.
pi@raspberrypi ~ $ chmod +x mcp23017_8-bit_output.py
Jetzt können Sie das Python Skript wie folgt ausführen.
pi@raspberrypi ~ $ sudo ./mcp23017_8-bit_output.py
Python Skript für ein 16 Kanal Lauflicht erstellen
Für das folgende Python Skript erstellen Sie mit einem Editor die Datei mcp23017_16-bit_output.py.
pi@raspberrypi ~ $ vi mcp23017_16-bit_output.py
Fügen Sie in diese Datei den folgenden Python Code ein und speichern die Datei ab.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Creation: 07.04.2015
# Last Update: 18.02.2017
#
# Copyright (c) 2015-2017 by Georg Kainzbauer <http://www.gtkdb.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# import required modules
import smbus
import time
# define I2C bus
# 0: Raspberry Pi Model B Rev 1.0
# 1: Raspberry Pi Model B Rev 2.0, Model A, Model B+, Model A+, Raspberry Pi 2 Model B and Raspberry Pi 3 Model B
I2C_BUS = 1
# define I2C address of MCP23017 16-Bit I/O expander
# depends on the hardware pins A0 - A2
I2C_ADDR = 0x20
# define I/O direction (IODIR) registers
IODIRA = 0x00
IODIRB = 0x10
# define output latch (OLAT) registers
OLATA = 0x14
OLATB = 0x15
# main function
def main():
try:
# init I2C bus
i2c = smbus.SMBus(I2C_BUS)
# configure all GPA pins as outputs
i2c.write_byte_data(I2C_ADDR, IODIRA, 0x00)
# configure all GPB pins as outputs
i2c.write_byte_data(I2C_ADDR, IODIRB, 0x00)
# set all output pins to 0
i2c.write_byte_data(I2C_ADDR, OLATA, 0x00)
i2c.write_byte_data(I2C_ADDR, OLATB, 0x00)
# initialize output variable
data = 0x0001
while True:
# get bit shift direction
# 0: shift to left
# 1: shift to right
if data == 0x0001:
direction = 0
elif data == 0x8000:
direction = 1
# output data on GPA port
i2c.write_byte_data(I2C_ADDR, OLATA, (data & 0xFF))
# output data on GPB port
i2c.write_byte_data(I2C_ADDR, OLATB, (data << 8 & 0xFF))
# shift bits
if direction == 0:
data = data << 1
else:
data = data >> 1
# wait 100ms
time.sleep(0.1)
except KeyboardInterrupt:
print("Execution stopped by user")
if __name__ == '__main__':
main()
# -*- coding: utf-8 -*-
#
# Creation: 07.04.2015
# Last Update: 18.02.2017
#
# Copyright (c) 2015-2017 by Georg Kainzbauer <http://www.gtkdb.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# import required modules
import smbus
import time
# define I2C bus
# 0: Raspberry Pi Model B Rev 1.0
# 1: Raspberry Pi Model B Rev 2.0, Model A, Model B+, Model A+, Raspberry Pi 2 Model B and Raspberry Pi 3 Model B
I2C_BUS = 1
# define I2C address of MCP23017 16-Bit I/O expander
# depends on the hardware pins A0 - A2
I2C_ADDR = 0x20
# define I/O direction (IODIR) registers
IODIRA = 0x00
IODIRB = 0x10
# define output latch (OLAT) registers
OLATA = 0x14
OLATB = 0x15
# main function
def main():
try:
# init I2C bus
i2c = smbus.SMBus(I2C_BUS)
# configure all GPA pins as outputs
i2c.write_byte_data(I2C_ADDR, IODIRA, 0x00)
# configure all GPB pins as outputs
i2c.write_byte_data(I2C_ADDR, IODIRB, 0x00)
# set all output pins to 0
i2c.write_byte_data(I2C_ADDR, OLATA, 0x00)
i2c.write_byte_data(I2C_ADDR, OLATB, 0x00)
# initialize output variable
data = 0x0001
while True:
# get bit shift direction
# 0: shift to left
# 1: shift to right
if data == 0x0001:
direction = 0
elif data == 0x8000:
direction = 1
# output data on GPA port
i2c.write_byte_data(I2C_ADDR, OLATA, (data & 0xFF))
# output data on GPB port
i2c.write_byte_data(I2C_ADDR, OLATB, (data << 8 & 0xFF))
# shift bits
if direction == 0:
data = data << 1
else:
data = data >> 1
# wait 100ms
time.sleep(0.1)
except KeyboardInterrupt:
print("Execution stopped by user")
if __name__ == '__main__':
main()
Nachdem Sie Das Python Skript erstellt haben, sollten Sie die Rechte der Datei anpassen.
pi@raspberrypi ~ $ chmod +x mcp23017_16-bit_output.py
Jetzt können Sie das Python Skript wie folgt ausführen.
pi@raspberrypi ~ $ sudo ./mcp23017_16-bit_output.py
Dieser Eintrag wurde am 19.02.2017 erstellt.
Direkter Link zu dieser Seite: http://www.gtkdb.de/index_36_2915.html
[ Zur Startseite ] [ Zur Kategorie ]
© 2004-2021 by Georg Kainzbauer