here i want to post this my example with mcp23017...you should put your switch between the ground and the input gpioa/b to work....This example took me 2 weeks to perfect this and working....
This example get as input 2 mcp23017 and 2 as output like leds.....
I hope this help.....
Best regards,
lestroso
http://www.fasasoftware.com
- Code: Select all
' Test program for MCP23017
'TEST INPUT .and OUTPUT...BY FAsasoftware!!!!
'This Is an example working ...these exam. use 2 mcp For Input And 2 mcp For leds Output
Const Device = CB220
Ramclear
Set I2c 9,8
' Register Definitions (Bank = 0)
#define IODIRA 0x00
#define IODIRB 0x01
#define IOPOLA 0x02
#define IOPOLB 0x03
#define GPINTENA 0x04
#define GPINTENB 0x05
#define DEFVALA 0x06
#define DEFVALB 0x07
#define INTCONA 0x08
#define INTCONB 0x09
#define IOCONA 0x0A
#define IOCONB 0x0B
#define GPPUA 0x0C
#define GPPUB 0x0D
#define INTFA 0x0E
#define INTFB 0x0F
#define INTCAPA 0x10
#define INTCAPB 0x11
#define GPIOA 0x12
#define GPIOB 0x13
#define OLATA 0x14
#define OLATB 0x15
' here below i declare the address of our mcp
#define MCP_0_write 0b01000010 ' used for input
#define MCP_0_read 0b01000011 ' used for input
#define MCP_1_write 0b01001000 ' used for input
#define MCP_1_read 0b01001001 ' used for input
#define MCP_2_write 0b01000000 ' used for led output
#define MCP_3_write 0b01001110 ' used for led output
Dim Device0 As Byte ' i declare the device variable tha i use
Dim Device1 As Byte
Dim Device2 As Byte
Dim Device3 As Byte
' Set up MCP23017 I/O direction
I2cstart
Device2 = I2cwrite(MCP_2_write)
Device2 = I2cwrite(IODIRA)
Device2 = I2cwrite(0b00000000)'set output led
I2cstop
I2cstart
Device2 = I2cwrite(MCP_2_write)
Device2 = I2cwrite(IODIRB)
Device2 = I2cwrite(0b00000000)'set output led
I2cstop
I2cstart
Device3 = I2cwrite(MCP_3_write)
Device3 = I2cwrite(IODIRA)
Device3 = I2cwrite(0b00000000)'set output led
I2cstop
I2cstart
Device3 = I2cwrite(MCP_3_write)
Device3 = I2cwrite(IODIRB)
Device3 = I2cwrite(0b00000000)'set output led
I2cstop
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' here i set the register for input device0 , you must set to 11111111 these register
'GPPUA IOPOLA Or your project will Not work
I2cstart
Device0 = I2cwrite(MCP_0_write)
Device0 = I2cwrite(IODIRA)
Device0 = I2cwrite(0b11111111)'set input
I2cstop
I2cstart
Device0 = I2cwrite(MCP_0_write)
Device0 = I2cwrite(GPPUA)
Device0 = I2cwrite(0b11111111)'set /Pull-up enable
I2cstop
I2cstart
Device0 = I2cwrite(IOPOLA)
Device0 = I2cwrite(0b11111111)'set Invert polarity
I2cstop
I2cstart
Device0 = I2cwrite(MCP_0_write)
Device0 = I2cwrite(IODIRB)
Device0 = I2cwrite(0b11111111)'set input
I2cstop
I2cstart
Device0 = I2cwrite(MCP_0_write)
Device0 = I2cwrite(GPPUB)
Device0 = I2cwrite(0b11111111)'set /Pull-up enable
I2cstop
I2cstart
Device0 = I2cwrite(IOPOLB)
Device0 = I2cwrite(0b11111111)'set set Invert polarity
I2cstop
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ' here i set the register for input device1 , you must set to 11111111 these register
'GPPUA IOPOLA Or your project will Not work
I2cstart
Device1 = I2cwrite(MCP_1_write)
Device1 = I2cwrite(IODIRA)
Device1 = I2cwrite(0b11111111)'set input
I2cstop
I2cstart
Device1 = I2cwrite(MCP_1_write)
Device1 = I2cwrite(GPPUA)
Device1 = I2cwrite(0b11111111)'set /Pull-up enable
I2cstop
I2cstart
Device1 = I2cwrite(IOPOLA)
Device1 = I2cwrite(0b11111111)'set Invert polarity
I2cstop
I2cstart
Device1 = I2cwrite(MCP_1_write)
Device1 = I2cwrite(IODIRB)
Device1 = I2cwrite(0b11111111)'set input
I2cstop
I2cstart
Device1 = I2cwrite(MCP_1_write)
Device1 = I2cwrite(GPPUB)
Device1 = I2cwrite(0b11111111)' set Invert polarity
I2cstop
I2cstart
Device1 = I2cwrite(IOPOLB)
Device1 = I2cwrite(0b11111111)' set Invert polarity
I2cstop
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Do
' Read GPIOA register
' I2cstart
' Device0 = I2cwrite(MCP_0_write)
' Device0 = I2cwrite(GPIOA)
' I2cstart
' Device0 = I2cwrite(MCP_0_read)
' Device0 = I2cread(0)
' I2cstop
' Read GPIOB register
' I2cstart
' Device0 = I2cwrite(MCP_0_write)
' Device0 = I2cwrite(GPIOB)
' I2cstart
' Device0 = I2cwrite(MCP_0_read)
' Device0 = I2cread(0)
' I2cstop
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Read GPIOA register
'here i read the register GPIOA and see the effect on the debug to test...
I2cstart
Device1 = I2cwrite(MCP_1_write)
Device1 = I2cwrite(GPIOA)
I2cstart
Device1 = I2cwrite(MCP_1_read)
Device1 = I2cread(0)
I2cstop
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Read GPIOB register
' I2cstart
' Device1 = I2cwrite(MCP_1_write)
' Device1 = I2cwrite(GPIOB)
' I2cstart
' Device1 = I2cwrite(MCP_1_read)
'' Device1 = I2cread(0)
' I2cstop
''''''''''''''''''''''''''''''''''''''''''''''''''
'' HERE i turn on the leds and then i turn off
If Device1 = 253 Then
' Write GPIOA register
I2cstart
Device2 = I2cwrite(MCP_2_write)
Device2 = I2cwrite(GPIOA)
Device2 = I2cwrite(0b11111111)
I2cstop
Delay 500
' Write GPIOA register
I2cstart
Device2 = I2cwrite(MCP_2_write)
Device2 = I2cwrite(GPIOA)
Device2 = I2cwrite(0b00000000)
I2cstop
Endif
Delay 50
Debug Dec Device1,Cr
Loop