from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
import smbus
import time
import RPi.GPIO as GPIO
from tkinter import messagebox
from time import sleep
# Open the bus
bus = smbus.SMBus(1) #Revision 2
#MCP23008
MCP23008address =0x20 # MCP23008 Adress IC2
MCP23008RegisterIODIR =0x00 # Register IODIR MCP23008 IO7 IO6 IO5 IO4
MCP23008RegisterIPOL =0x01 # Register IPOL MCP23008 IP7 IP6 IP5 IP4 IP3
MCP23008RegisterGPINTEN =0x02 # Register GPINTEN MCP23008 GPINT7
MCP23008RegisterDEFVAL =0x03 # Register DEFVAL MCP23008 DEF7 DEF6
MCP23008RegisterINTCON =0x04 # Register INTCON MCP23008 IOC7 IOC6
MCP23008RegisterIOCON =0x05 # Register IOCON MCP23008 — — SREAD
MCP23008RegisterGPPU =0x06 # Register GPPU MCP23008 PU7 PU6 PU5
MCP23008RegisterINTF =0x07 # Register INTF MCP23008 INT7 INT6 INT5
MCP23008RegisterINTCAP =0x08 # Register INTCAP MCP23008 ICP7 ICP6
MCP23008RegisterGPIO =0x09 # Register GPIO MCP23008 GP7 GP6 GP5
MCP23008RegisterOLAT =0x0A # Register OLAT MCP23008 OL7 OL6 OL5
MCP24C02address =0x50 #24C02 Control Register A0,A1,A2 =0 256*8bit
MCP24C02AddressPointer =0x00 #The internal Address Pointer will automatically
window = Tk()
# Set up the pins as output
#Address pins are always enabled on MCP23008. MCP23S08 IOCON muss bit 3
HAEN 1 gesetzt werden.
bus.write_byte_data( MCP23008address, MCP23008RegisterIODIR, 0x00 )
#Setzt MCP23008 auf I/O Ausgang (Outout)
bus.write_byte_data( MCP23008address, MCP23008RegisterGPIO, 0x00 )
#alle Relais aus 0x00 beim Starten
#Operator
Bezeichnung
Beispiel
#~x
Bitweises Not
Ausgabe=0x03 also b0000 0011 (Taste auf
Tastertur AltGr +*)
# Ausgabe=Ausgabe & ~0x01 (also b0000 0001)
Ausgabe: 0x02 (b0000 0010)
#|x
Binary OR
Ausgabe=0x03 (Taste auf Tastertur AltGr
<>)
# Ausgabe=Ausgabe & ~0x04 Ausgabe: 0x07
#https://wiki.python.org/moin/BitwiseOperators
def Relais():
global Ausgabe
Ausgabe=0x00
if (CheckVar1.get()==0):
Ausgabe=Ausgabe & ~0x01
bus.write_byte_data( MCP23008address, MCP23008RegisterGPIO, Ausgabe )
# Relais1 Ausschalten
if (CheckVar1.get()==1):
Ausgabe=Ausgabe | 0x01
bus.write_byte_data( MCP23008address, MCP23008RegisterGPIO, Ausgabe )
# Relais1 Einschalten
if (CheckVar2.get()==0):
Ausgabe=Ausgabe & ~0x02
bus.write_byte_data( MCP23008address, MCP23008RegisterGPIO, Ausgabe )
if (CheckVar2.get()==1):
Ausgabe=Ausgabe | 0x02
bus.write_byte_data( MCP23008address, MCP23008RegisterGPIO, Ausgabe )
if (CheckVar3.get()==0):
Ausgabe=Ausgabe & ~0x04
bus.write_byte_data( MCP23008address, MCP23008RegisterGPIO, Ausgabe )
if (CheckVar3.get()==1):
Ausgabe=Ausgabe | 0x04
bus.write_byte_data( MCP23008address, MCP23008RegisterGPIO, Ausgabe )
if (CheckVar4.get()==0):
Ausgabe=Ausgabe & ~0x08
bus.write_byte_data( MCP23008address, MCP23008RegisterGPIO, Ausgabe )
if (CheckVar4.get()==1):
Ausgabe=Ausgabe | 0x08
bus.write_byte_data( MCP23008address, MCP23008RegisterGPIO, Ausgabe )
if (CheckVar5.get()==0):
Ausgabe=Ausgabe & ~0x10
bus.write_byte_data( MCP23008address, MCP23008RegisterGPIO, Ausgabe )
if (CheckVar5.get()==1):
Ausgabe=Ausgabe | 0x10
bus.write_byte_data( MCP23008address, MCP23008RegisterGPIO, Ausgabe )
if (CheckVar6.get()==0):
Ausgabe=Ausgabe & ~MCP23008address
bus.write_byte_data( MCP23008address, MCP23008RegisterGPIO, Ausgabe )
if (CheckVar6.get()==1):
Ausgabe=Ausgabe | MCP23008address
bus.write_byte_data( MCP23008address, MCP23008RegisterGPIO, Ausgabe )
if (CheckVar7.get()==0):
Ausgabe=Ausgabe & ~0x40
bus.write_byte_data( MCP23008address, MCP23008RegisterGPIO, Ausgabe )
if (CheckVar7.get()==1):
Ausgabe=Ausgabe | 0x40
bus.write_byte_data( MCP23008address, MCP23008RegisterGPIO, Ausgabe )
if (CheckVar8.get()==0):
Ausgabe=Ausgabe & ~0x80
bus.write_byte_data( MCP23008address, MCP23008RegisterGPIO, Ausgabe )
# Relais 8 Ausschalten
if (CheckVar8.get()==1):
Ausgabe=Ausgabe | 0x80
bus.write_byte_data( MCP23008address, MCP23008RegisterGPIO, Ausgabe )
# Relais 8 Einschalten
def MCP24C02Read():
MCP24C02TextLoeschen()
MCP24C02AddressPointer=0x00
for x in range(0,32): #256*8Bit also 32 mal 8*8Bit 0xE0-0XFF dürfen nicht
überschrieben werden der Rest ist frei verfügbar!
MCP24C02AddressPointer=x*8
ReceivedBytes=""
ReceivedBytes=str.format('0x{:02X}', int(str(hex(MCP24C02AddressPointer)),
16))+" "
bus.write_byte(MCP24C02address, MCP24C02AddressPointer )
#Setze AddressPointer zum lesen schreiben in diesem Fall zum lesen
ReceivedBytes=ReceivedBytes+str.format('0x{:02X}',
int(str(hex(bus.read_byte(MCP24C02address))), 16))+" "
ReceivedBytes=ReceivedBytes+str.format('0x{:02X}',
int(str(hex(bus.read_byte(MCP24C02address))), 16))+" "
ReceivedBytes=ReceivedBytes+str.format('0x{:02X}',
int(str(hex(bus.read_byte(MCP24C02address))), 16))+" "
ReceivedBytes=ReceivedBytes+str.format('0x{:02X}',
int(str(hex(bus.read_byte(MCP24C02address))), 16))+" "
ReceivedBytes=ReceivedBytes+str.format('0x{:02X}',
int(str(hex(bus.read_byte(MCP24C02address))), 16))+" "
ReceivedBytes=ReceivedBytes+str.format('0x{:02X}',
int(str(hex(bus.read_byte(MCP24C02address))), 16))+" "
ReceivedBytes=ReceivedBytes+str.format('0x{:02X}',
int(str(hex(bus.read_byte(MCP24C02address))), 16))+" "
ReceivedBytes=ReceivedBytes+str.format('0x{:02X}',
int(str(hex(bus.read_byte(MCP24C02address))), 16))+" "
bus.write_byte(MCP24C02address, MCP24C02AddressPointer )
ReceivedBytes=ReceivedBytes+"|"+chr(bus.read_byte(MCP24C02address))
ReceivedBytes=ReceivedBytes+chr(bus.read_byte(MCP24C02address))
ReceivedBytes=ReceivedBytes+chr(bus.read_byte(MCP24C02address))
ReceivedBytes=ReceivedBytes+chr(bus.read_byte(MCP24C02address))
ReceivedBytes=ReceivedBytes+chr(bus.read_byte(MCP24C02address))
ReceivedBytes=ReceivedBytes+chr(bus.read_byte(MCP24C02address))
ReceivedBytes=ReceivedBytes+chr(bus.read_byte(MCP24C02address))
ReceivedBytes=ReceivedBytes+chr(bus.read_byte(MCP24C02address))+"|"
T.insert(END,ReceivedBytes)
T.insert(END,"\n")
test=" "
def MCP24C02TextLoeschen():
T.delete(1.0,END)
#chr()
Converts an integer to a character
#ord()
Converts a character to an integer
#len()
Returns the length of a string
#str()
Returns a string representation of an object
def MCP24C02Write():
if len(entry_widget.get()) <= 0:
messagebox.showinfo("Write","No Data!")
else:
if combo.get() == "0x00":
messagebox.showinfo("Write", str(entry_widget.get()))
MCP24C02AddressPointer=0x00
Daten=str(entry_widget.get())
for x in range(0,len(entry_widget.get())):
sleep(0.01)
bus.write_byte_data(MCP24C02address,
MCP24C02AddressPointer,ord(Daten[x]))
MCP24C02AddressPointer=MCP24C02AddressPointer+1
MCP24C02TextLoeschen()
sleep(0.01) #Muss noch durch
Abfrage ersetzt werden
MCP24C02Read()
if combo.get() == "0x08":
messagebox.showinfo("Write", str(entry_widget.get()))
MCP24C02AddressPointer=0x08
Daten=str(entry_widget.get())
for x in range(0,len(entry_widget.get())):
sleep(0.01)
bus.write_byte_data(MCP24C02address,
MCP24C02AddressPointer,ord(Daten[x]))
MCP24C02AddressPointer=MCP24C02AddressPointer+1
MCP24C02TextLoeschen()
sleep(0.01)
MCP24C02Read()
if combo.get() == "0x10":
messagebox.showinfo("Write", str(entry_widget.get()))
MCP24C02AddressPointer=0x10
Daten=str(entry_widget.get())
for x in range(0,len(entry_widget.get())):
sleep(0.01)
bus.write_byte_data(MCP24C02address,
MCP24C02AddressPointer,ord(Daten[x]))
MCP24C02AddressPointer=MCP24C02AddressPointer+1
MCP24C02TextLoeschen()
sleep(0.01)
MCP24C02Read()
if combo.get() == "0x18":
messagebox.showinfo("Write", str(entry_widget.get()))
MCP24C02AddressPointer=0x18
Daten=str(entry_widget.get())
for x in range(0,len(entry_widget.get())):
sleep(0.01)
bus.write_byte_data(MCP24C02address,
MCP24C02AddressPointer,ord(Daten[x]))
MCP24C02AddressPointer=MCP24C02AddressPointer+1
MCP24C02TextLoeschen()
sleep(0.01)
MCP24C02Read()
if combo.get() == "0x20":
messagebox.showinfo("Write", str(entry_widget.get()))
MCP24C02AddressPointer=0x20
Daten=str(entry_widget.get())
for x in range(0,len(entry_widget.get())):
sleep(0.01)
bus.write_byte_data(MCP24C02address,
MCP24C02AddressPointer,ord(Daten[x]))
MCP24C02AddressPointer=MCP24C02AddressPointer+1
MCP24C02TextLoeschen()
sleep(0.01)
MCP24C02Read()
if combo.get() == "0xD8":
messagebox.showinfo("Write", str(entry_widget.get()))
MCP24C02AddressPointer=0xD8
Daten=str(entry_widget.get())
for x in range(0,len(entry_widget.get())):
sleep(0.01)
bus.write_byte_data(MCP24C02address,
MCP24C02AddressPointer,ord(Daten[x]))
MCP24C02AddressPointer=MCP24C02AddressPointer+1
MCP24C02TextLoeschen()
sleep(0.01)
MCP24C02Read()
def character_limit(entry_text):
if len(entry_text.get()) > 8:
entry_text.set(entry_text.get()[:-1])
# Fenster Breite, Höhe
w = 1000 # Breite Tk window
h = 650 # Höhe Tk window
# Lese Bildschirm Auflösung
ws = window.winfo_screenwidth() # Breite Auflösung vom Bildschirm
hs = window.winfo_screenheight() # Höhe
# Berechnung der Mitte
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
window.geometry('%dx%d+%d+%d' % (w, h, x, y)) # Setze Fenster in Mitte
window.resizable(0,0) # Fenster ist in der Groesse nicht
veraenderbar
window.title("MCP23008 & 24C02 www.AnHaHe.de")
tab_control = ttk.Notebook(window)
tab1 = ttk.Frame(tab_control)
tab2 = ttk.Frame(tab_control)
tab_control.add(tab1, text='MCP23008')
tab_control.add(tab2, text='24C02')
tab_control.pack(expand=1, fill='both')
#Checkbox 8 Stück mit pack ins Fenster zeichen mit place die Position
CheckVar1 = IntVar()
C1 = Checkbutton(tab1, text = "Relais 1", variable = CheckVar1,command=Relais)
C1.pack()
C1.place(x=50, y=50) #von oben links
CheckVar2 = IntVar()
C2 = Checkbutton(tab1, text = "Relais 2", variable = CheckVar2,command=Relais)
C2.pack()
C2.place(x=50, y=75)
CheckVar3 = IntVar()
C3 = Checkbutton(tab1, text = "Relais 3", variable = CheckVar3,command=Relais)
C3.pack()
C3.place(x=50, y=100)
CheckVar4 = IntVar()
C4 = Checkbutton(tab1, text = "Relais 4", variable = CheckVar4,command=Relais)
C4.pack()
C4.place(x=50, y=125)
CheckVar5 = IntVar()
C5 = Checkbutton(tab1, text = "Relais 5", variable = CheckVar5,command=Relais)
C5.pack()
C5.place(x=50, y=150)
CheckVar6 = IntVar()
C6 = Checkbutton(tab1, text = "Relais 6", variable = CheckVar6,command=Relais)
C6.pack()
C6.place(x=50, y=175)
CheckVar7 = IntVar()
C7 = Checkbutton(tab1, text = "Relais 7", variable = CheckVar7,command=Relais)
C7.pack()
C7.place(x=50, y=200)
CheckVar8 = IntVar()
C8 = Checkbutton(tab1, text = "Relais 8", variable = CheckVar8,command=Relais)
C8.pack()
C8.place(x=50, y=225)
combo = Combobox(tab2, state='readonly')
combo['values']= ("0x00","0x08","0x10","0x18","0x20","0xD8")
combo.current(0) #set the selected item
combo.place(x=50, y=50)
T = Text(tab2, height=32, width=56) #für EEprom 256 x 8 32*8byte
T.pack()
T.place(x=350, y=20)
buttonTextMCP24C02Clear = Button(tab2, text='TextBox Inhalt Löschen', width=25,
command=MCP24C02TextLoeschen)
buttonTextMCP24C02Clear.pack()
buttonTextMCP24C02Clear.place(x=50, y=75)
buttonTextMCP24C02Read = Button(tab2, text='24C02 Lesen 0x00-0xFF', width=25,
command=MCP24C02Read)
buttonTextMCP24C02Read.pack()
buttonTextMCP24C02Read.place(x=50, y=125)
buttonTextMCP24C02Write = Button(tab2, text='24C02 Schreiben', width=25,
command=MCP24C02Write)
buttonTextMCP24C02Write.pack()
buttonTextMCP24C02Write.place(x=50, y=175)
entry_text = StringVar() # the text in your entry
entry_widget = Entry(tab2, width = 20, textvariable = entry_text) # the entry
entry_widget.pack()
entry_widget.place(x=50, y=225)
entry_text.trace("w", lambda *args: character_limit(entry_text))
#0xe0 2 fa-55 UniPi identification
#0xe2 2 1.1 UniPi version
#0xf0 4 0x40b089c5 (5.516818) AI1 coefficient
#0xf4 4 0x40b08b44 (5.517) AI2 coefficient
w = Label(tab2, text="0xe0 2 Bytes UniPi identification")
w.pack()
w.place(x=50, y=300)
w1 = Label(tab2, text="0xe2 2 Bytes UniPi version")
w1.pack()
w1.place(x=50, y=320)
w2 = Label(tab2, text="0xf0 4 Bytes AI1 coefficient")
w2.pack()
w2.place(x=50, y=340)
w3 = Label(tab2, text="0xf4 4 Bytes AI2 coefficient")
w3.pack()
w3.place(x=50, y=360)
w4 = Label(tab2, text="Daten max.8 Zeichen")
w4.pack()
w4.place(x=50, y=205)
w5 = Label(tab2, text="AddressPointer")
w5.pack()
w5.place(x=50, y=30)
buttonschliessen = Button(window, text='Fenster Schliessen', width=25,
command=window.destroy)
buttonschliessen.pack()
buttonschliessen.place(x=760, y=620)
window.mainloop()