View previous topic :: View next topic |
Author |
Message |
DirkBischof
Joined: 25 May 2013 Posts: 2
|
Posted: Mon Jun 14, 2021 11:38 pm Post subject: Read Data from MCP23017 I2C |
|
|
Hello,
i use a few MCP23017 for output and inputs.
The outputs working well...but i can't read the inputs.
i start with the AN245 for the MCP23016
[code]
'**********************************************************************
' Application Note: Using the MCP23017 I/O expander
'-----------------------------------------------------
'
' Program-ID.: AN245 MCP23017.bas.
' Date...... : 22 / 05 / 2005
' Description: Example program for I2C MCP23017 I/O expander
' write and read routines.
'
' Author : Roland van leusden (sayang@zonnet.nl).
'
' Setup for the AT90S2313 using portd.6 for SDA and Portd.5 for SCL.
' (Futurlec ET-JRAVR board)
' See AN245 page 9 from Microchip for MCP23017 shematic.
'
' This program provides an example for writing to and reading from
' an I2C MCP23017 device wired for device 0 (AO,A1 and A2 all pulled
' to Gnd).
'
'
'**********************************************************************
$regfile = "m328pdef.dat" ' Change for your AVR
$crystal = 16000000 ' 4 MHz Crystal.
'$regfile = "m8def.dat" ' Change for your AVR.
'$crystal = 20000000 ' 4 MHz Crystal.
$baud = 9600 ' Output at 19200 baud.
'**********************************************************************
'** Set up Data Direction Registers and ports - Do this before defining I2C pins!
'**********************************************************************
' Port B
Portb = &B0000_0000 ' Set All Port Pins Low
Ddrb = &B1111_1111 ' Set Unused Pins As Outputs.
' Port D
Portd = &B0000_0000 ' All low - with push-pull output
Ddrd = &B1111_1111 ' with internal pullups.
'**********************************************************************
'** Define and initialize I2C pins **
'**********************************************************************
Config Sda = PortC.4 ' I2C Data.
Config Scl = Portc.5 ' I2C Clock.
'**********************************************************************
'
'** Declare subroutines **
'**********************************************************************
'
' This subroutine writes data to the I2C MCP23017.
'
Declare Sub I2c_mcp23017_write(byval Cmd As Byte , Byval Lsb As Byte , Byval Msb As Byte)
'
' This subroutine reads data from the I2C MCP23017.
'
Declare Sub I2c_mcp23017_read
Declare Sub I2c_scan
'**********************************************************************
'** Define working variables and constants **
'**********************************************************************
'
' Change the 3 "AD" bits to reflect the I2C address of the device
' (corresponding to A0,A1 and A2)..
'
Dim Mcp23017_adress_w As Byte
Mcp23017_adress_w = &H40 'Write Adress: 0 1 0 0 A2 A1 A0 0
Dim Mcp23017_adress_r As Byte
Mcp23017_adress_r = &H41 'Read Adress: 0 1 0 0 A2 A1 A0 1
Dim Counter_1 As Byte
Dim Lsb As Byte
Dim Msb As Byte
Dim B As Byte
'This is for the Microchip MCP23017 initialization, some examples below
'Call I2c_mcp23017_write(&H04 , &Hff , &Hff) 'Invert All Input Polarities
'Call I2c_mcp23017_write(&H0a , &H01 , &H01) 'Initialize Iares , For Fast Input Scan Mode
'Call I2c_mcp23017_write(&H06 , &H00 , &HFF) 'Initiallize It So The Lsbs Outputs Are & Msbs Are Inputs
'Call I2c_mcp23017_write(&H06 , &Hff , &Hff) 'Initiallize It So That Both Lsbs & Msbs Are Inputs
'Call I2c_mcp23017_write(&H02 , &HFF , &HFF) 'Initiallize The Ouput Latch
'Call I2c_mcp23017_write(&H06 , &H00 , &H00) 'Initiallize It So That Both Lsbs & Msbs Are Outputs
'**********************************************************************
'** Actual work starts here. **
'**********************************************************************
Main:
Wait 1
Print "MCP23017 "
Wait 1
Print "scanne I2C-Bus"
Print ".."
Call I2c_scan
Waitms 250 ' wait 250 ms for the MCP23017 powerup timer
Call I2c_mcp23017_write(&H02 , &HFF , &HFF) 'Initiallize The Ouput Latch
Call I2c_mcp23017_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
Call I2c_mcp23017_write(&H02 , Lsb , Msb) 'Write Lsb & Msb to MCP23017
Rotate Lsb , Left , 1 'Rotate the bits
Rotate Msb , Left , 1
Waitms 100 'Wait 100ms
Counter_1 = Counter_1 + 1 ' Increase the counter
Print "Counter_1: " ; Counter_1
Loop Until Counter_1 = 255 ' As long as the counter is not 255 keep writing & rotating
Lsb = &HFF '0 => Led is "on" FF => 11111111
Msb = &HFF
Call I2c_mcp23017_write(&H02 , Lsb , Msb) 'All leds off
Call I2c_mcp23017_write(&H06 , &H00 , &HFF) 'Initiallize It So The Lsbs Outputs Are & Msbs Are Inputs
Do 'Read Msb and display on Lsb
Call I2c_mcp23017_read ' Read Msb
Print "Read Msb: " ; Msb ' Send value of Msb to serial port
Lsb = Msb ' Copy Msb to Lsb
Call I2c_mcp23017_write(&H02 , Lsb , Msb) ' Write Lsb to MCP23017
Waitms 100 'Wait 100ms
Loop
End 'end program
'**********************************************************************
'** Define Subroutines **
'**********************************************************************
Sub I2c_mcp23017_write(byval Cmd As Byte , Byval Lsb As Byte , Byval Data2 As Byte)
' Writes data to the I2C MCP23017.
I2cstart 'Generate A Start Condition
I2cwbyte Mcp23017_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
If Err > 0 Then
Print " MCP23017 Error : " ; Err ' show error
End If
I2cstop 'Generate a STOP condition
Waitus 50 'Some delay may be necessary for back to back transmitions
End Sub
Sub I2c_mcp23017_read
' Read data from the I2C MCP23017
I2cstart 'Generate START condition
I2cwbyte Mcp23017_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 Mcp23017_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
If Err > 0 Then
Print " MCP23017 Error : " ; Err ' show error
End If
I2cstop 'Generate a STOP condition
End Sub
Sub I2c_scan ' check all devices on the I2c-bus
Print "Scan start I2C Bus"
For B = 0 To 254 Step 2 'for all odd addresses
I2cstart 'send start
I2cwbyte B 'send address
If Err = 0 Then 'we got an ack
Print "Slave at found " ; Hex(b)
End If
I2cstop 'free bus
Next
Print "End Scan I2C Bus"
End Sub
Do someone have a tip for me ?
Thanks a lot |
|
Back to top |
|
|
O-Family
Joined: 23 May 2010 Posts: 320 Location: Japan
|
Posted: Tue Jun 15, 2021 2:41 am Post subject: |
|
|
The register addresses of MCP23016 and MCP23017 are different.
Code: | $regfile = "m328pdef.dat" ' Change for your AVR
$crystal = 16000000
$hwstack = 64
$swstack = 10
$framesize = 24
$baud = 9600
Dim I2cbuff(10) As Byte 'I2C send / receive data buffer.
Dim Mcp23017_gpa As Byte 'MCP23017 GPIOA
Dim Mcp23017_gpb As Byte 'MCP23017 GPIOB
'**********************************************************************
'** Define and initialize I2C pins **
'**********************************************************************
Config Sda = Portc.4 ' I2C Data.
Config Scl = Portc.5 ' I2C Clock.
I2cinit 'Initialize the I2C bus.
Dim Mcp23017_adress As Byte
Mcp23017_adress = &H40 'MCP23017 Adress: 0 1 0 0 A2 A1 A0 0
Wait 1
Print "MCP23017 "
Wait 1
'
' * Initial setting of MCP23017 *
'
I2cbuff(1) = &H00 'Register address = [IODIRA]
I2cbuff(2) = &B0000_0000 'Set the GPA port to output.
I2cbuff(3) = &B1111_1111 'Set the GPB port to input.
I2csend Mcp23017_adress , I2cbuff(1) , 3 'Send 3 bytes of data to MCP23017.
I2cbuff(1) = &H0D 'Register address = [GPPUB]
I2cbuff(2) = &B1111_1111 'Enable GPB port pull-up.
I2csend Mcp23017_adress , I2cbuff(1) , 2 'Send 2 bytes of data to MCP23017.
Main:
Do 'Read Msb and display on Lsb
I2cbuff(1) = &H13 'Register address = [GPIOB]
I2creceive Mcp23017_adress , I2cbuff(1) , 1 , 1 'Sends 1 byte to MCP23017 and receives 1 byte.
Mcp23017_gpb = I2cbuff(1)
Print "Read GPB: " ; Bin(mcp23017_gpb)
I2cbuff(1) = &H12 'Register address = [GPIOA]
I2cbuff(2) = Mcp23017_gpb 'Data to be output to [GPIOA].
I2csend Mcp23017_adress , I2cbuff(1) , 2 'Send 2 bytes of data to MCP23017.
Waitms 100 'Wait 100ms
Loop
End 'end program |
|
|
Back to top |
|
|
DirkBischof
Joined: 25 May 2013 Posts: 2
|
Posted: Tue Jun 15, 2021 5:19 pm Post subject: reading from MCP23017 |
|
|
hello O-Family
MANY THANKS it works fine !
greetings from Hamburg Germany to Japan ! |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|