So... neues Jahr, neues Glück. (Hoffe ich)
Ich habe mir nun ein Testprogramm aus den Vorschlägen erstellt, das zumindest bestätigt, das der MCP am Bus verfügbar ist.
Hex40 und Hex41 passt als Adresse und anscheinend wird alles soweit korrekt gesendet.
Leider bleiben GPA und auch GPB trotzdem stumm.
Theoretisch sollten aber jeweils alle Ausgänge auf 1 sein und nur ein Ausgang jeweils auf 0. (ähnlich Lauflicht)
Code:
$regfile = "M328pdef.dat"
$crystal = 16000000
$baud = 38400
'**********************************************************************
'** I2C pins **
'**********************************************************************
Config Sda = Portc.4 ' I2C Data.
Config Scl = Portc.5 ' I2C Clock.
Config I2cdelay = 10
'**********************************************************************
'** Declare subroutines **
'**********************************************************************
Declare Sub I2c_mcp23016_write(byval Cmd As Byte , Byval Lsb As Byte , Byval Msb As Byte)
Declare Sub I2c_mcp23016_read
'**********************************************************************
'** Define working variables and constants **
'**********************************************************************
Dim Busaddress As Byte
Dim Busaddress_read As Byte
Dim Chipaddress As Byte
Dim Erkannt As Bit
Dim Mcp23016_adress_w As Byte
Mcp23016_adress_w = &H40 'Write Adress: 0 1 0 0 _0 0 0_ 0
Dim Mcp23016_adress_r As Byte
Mcp23016_adress_r = &H41 'Read Adress: 0 1 0 0 _0 0 0_ 1
Dim Counter_1 As Byte
Dim Lsb As Byte
Dim Msb As Byte
'This is for the Microchip MCP23016 initialization, some examples below
'Call I2c_mcp23016_write(&H04 , &Hff , &Hff) 'Invert All Input Polarities
'Call I2c_mcp23016_write(&H0a , &H01 , &H01) 'Initialize Iares , For Fast Input Scan Mode
'Call I2c_mcp23016_write(&H06 , &H00 , &HFF) 'Initiallize It So The Lsbs Outputs Are & Msbs Are Inputs
'Call I2c_mcp23016_write(&H06 , &Hff , &Hff) 'Initiallize It So That Both Lsbs & Msbs Are Inputs
'Call I2c_mcp23016_write(&H02 , &HFF , &HFF) 'Initiallize The Ouput Latch
'Call I2c_mcp23016_write(&H06 , &H00 , &H00) 'Initiallize It So That Both Lsbs & Msbs Are Outputs
'**********************************************************************
'** Main-Programm **
'**********************************************************************
'Scan bus for valid I2C addresses on bus
'( test for ACK to come back from Busaddress )
Do
Print "Scan start"
For Busaddress = 0 To 255 'for all even addresses
I2cstart
I2cwbyte Busaddress
If Err = 0 Then
Erkannt = 1
Print "Slave at address: " ; Busaddress ; " Dezimal, = " ; Hex(busaddress) ; " hex"
Chipaddress = Busaddress \ 2
Print " with chip address " ; Hex(chipaddress) ; "hex"
End If
I2cstop
Next
Print "End Scan"
If Erkannt = 0 Then Print "Keine I2C Teilnehmer gefunden!!"
Waitms 250
Loop Until Erkannt = 1
Main:
Waitms 250 ' wait 250 ms for the MCP23016 powerup timer
Call I2c_mcp23016_write(&H02 , &HFF , &HFF) 'Initiallize The Ouput Latch
Call I2c_mcp23016_write(&H06 , &H00 , &H00) 'Initiallize It So That Both Lsbs & Msbs Are Outputs
Lsb = &HFE '0 => Led is "on" FE => 11111110
Msb = &HFE '0 => Led is "on" FE => 11111110
Counter_1 = 0
Do 'Chaser effect
Print Bin(lsb) 'Ausgabe zur Kontrolle
Print Bin(msb) 'Ausgabe zur Kontrolle
Call I2c_mcp23016_write(&H02 , Lsb , Msb) 'Write Lsb & Msb to MCP23016
Rotate Lsb , Left , 1 'Rotate the bits
Rotate Msb , Left , 1
Waitms 1000
Counter_1 = Counter_1 + 1 'Increase the counter
Loop
End
'**********************************************************************
'** Define Subroutines **
'**********************************************************************
Sub I2c_mcp23016_write(byval Cmd As Byte , Byval Lsb As Byte , Byval Data2 As Byte) ' Writes data to the I2C MCP23016.
I2cstart 'Generate A Start Condition
I2cwbyte Mcp23016_adress_w 'Transmit The "ADDRESS and WRITE" Byte
I2cwbyte Cmd 'Transmit The Command Byte
I2cwbyte Lsb 'Transmit First Data Byte
I2cwbyte Msb 'Transmit Second Data Byte
I2cstop 'Generate a STOP condition
Waitus 50 'Some delay may be necessary for back to back transmitions
End Sub
Sub I2c_mcp23016_read
' Read data from the I2C MCP23016
I2cstart 'Generate START condition
I2cwbyte Mcp23016_adress_w 'Transmit The "ADDRESS and WRITE" Byte
I2cwbyte &H00 'Transmit The Command Byte
I2cstop 'Generate a STOP condition
I2cstart 'Generate a START condition
I2cwbyte Mcp23016_adress_r 'Transmit ADDRESS with READ command
I2crbyte Lsb , Ack 'Receive first DATA byte (LSB) and acknowledge
I2crbyte Msb , Nack 'Receive second DATA byte (MSB) and don't acknowledge
I2cstop 'Generate a STOP condition
End Sub
Lesezeichen