Dieses Programm hab ich bisher zusammengestellt.
Es erkennt bisher die Busteilnehmeradresse und Schaltet am MCP23017 alle auf Ausgang.
Die Ausgänge Toggeln im Sekundentakt zur Kontrolle.
Ob es an der "falschen" busgeschwindigkeit lag (jetzt auf delay=10) oder ob das ansprechen der Register
falsch war werde ich wohl noch genauer testen.
Code:
'**********************************************************************
'** Programmbeschreibung **
'**********************************************************************
$regfile = "M328pdef.dat"
$crystal = 16000000
$baud = 38400
'**********************************************************************
'** Data Direction Registers und Ports - Do this before defining I2C pins!
'**********************************************************************
'**********************************************************************
'** I2C-Pins **
'**********************************************************************
Config Sda = Portc.4 ' I2C Data.
Config Scl = Portc.5 ' I2C Clock.
Config I2cdelay = 10
'**********************************************************************
'** Subroutinen **
'**********************************************************************
'
' This subroutine writes data to the I2C MCP23016.
'
Declare Sub I2c_mcp23016_write(byval Cmd As Byte , Byval Abyte As Byte , Byval Bbyte As Byte)
'**********************************************************************
'** Variablen **
'**********************************************************************
Dim Busaddress As Byte
Dim Busaddress_read As Byte
Dim Chipaddress As Byte
Dim Erkannt As Bit
Dim Abyte As Byte '
Dim Bbyte As Byte
Dim Mcp23016_adress_w As Byte
Mcp23016_adress_w = &H40 'Write Adresse des MCP23017: 0 1 0 0 _0 0 0_ 0
Dim Mcp23016_adress_r As Byte
Mcp23016_adress_r = &H41 'Read Adresse des MCP23017: 0 1 0 0 _0 0 0_ 1
Dim Counter_1 As Byte
'**********************************************************************
'** Main-Programm **
'**********************************************************************
Waitms 250
'Warte 250ms auf den MCP23016 Powerup-Timer
'I2C-Bus nach allen verfügbaren Adressen (Dezimal 0-255) scannen
Do
Print "Scan gestartet"
Waitms 250
For Busaddress = 0 To 255
I2cstart
I2cwbyte Busaddress
If Err = 0 Then 'wird kein Bus-Fehler gemeldet, ist die Adresse aktiv
Erkannt = 1
Print "Slave at addresse gefunden! Dezimal=" ; Busaddress ; " Hexadezimal=" ; Hex(busaddress)
End If
I2cstop 'I2C-Bus durch "Stop" wieder frei machen für nächste Übertragung
Next
Print "Scan Beendet"
If Erkannt = 0 Then Print "Keine I2C Teilnehmer gefunden!!"
Waitms 100
Loop Until Erkannt = 1
'
'Folgende Zeilen sind für den MCP23017 !!!
'Alle GPA und GPB werden auf Ausgang eingestellt und im Sekundentakt getoggelt
'
Call I2c_mcp23016_write(&H00 , &H00 , &H00) 'IODIRA und IODIRB alle auf Ausgänge einstellen
Counter_1 = 1
Do
Call I2c_mcp23016_write(&H14 , &HFF , &HFF) 'OLATA und OLATB alle auf 1 stellen
Waitms 1000
Call I2c_mcp23016_write(&H14 , &H00 , &H00) 'OLATA und OLATB alle auf 0 Stellen
Waitms 1000
Counter_1 = Counter_1 + 1
Loop
End
'**********************************************************************
'** Define Subroutines **
'**********************************************************************
Sub I2c_mcp23016_write(byval Cmd As Byte , Byval Abyte As Byte , Byval Bbyte As Byte)
' Daten auf MCP23017 schreiben/senden
I2cstart 'Generate A Start Condition
I2cwbyte Mcp23016_adress_w 'Transmit The "ADDRESS and WRITE" Byte
I2cwbyte Cmd 'Transmit The Command Byte
I2cwbyte Abyte 'Transmit First Data Byte
I2cwbyte Bbyte 'Transmit Second Data Byte
I2cstop 'Generate a STOP condition
Waitus 50 'Some delay may be necessary for back to back transmitions
End Sub
ist das richtig, das ich hierbei...
Code:
Call I2c_mcp23016_write(&H14 , &HFF , &HFF)
jeweils die Bytes für A und auch für B sende,
da der IC die Registeradresse automatisch beim zweiten gesendeten Byte inkrementell erhöht?
Wasmich auch noch etwas verwirrt... warum muss ich die Ausgänge auf das Register OLATx, und nicht auf GPIOx schreiben. bzw warum haben die Ausgänge überhaupt 2 Register?
Lesezeichen