Re: IC MCP23017 I2C Bus I/0 - Erweiterung Kategorie: Programmierung C (von UlliS - 23.02.2009 10:04) | ||
Als Antwort auf IC MCP23017 I2C Bus I/0 - Erweiterung von Christian - 23.02.2009 0:25 | ||
| ||
> Hallo > > bräuchte dringend Hilfe. Ich probiere jetzt schon seit knapp 2 Wochen den MCP23017 IC > von Microchip anzusprechen. > Aber entweder stürzt das Programm ab oder es passiert nix. Die Ausgänge bleiben dennoch stumm. :-( > Als Stütze benutze ich die Beispielprogramme für den PCF 8574 von Ulli. > > > Hierher hab ich ihn bezogen:> > MCP23017 IC > > Vielen Dank > > Grü�e Christian > > Hallo Christian, der MCP23017 ist etwas umfangreicher von den Einstellungen wie der PCF8574... der MCP besitzt unter anderem ein richtiges Datenrichtungsregister und es kann zwichen Bytemode und Sequenziellmode ausgwählt werden. Das Beispiel zum PCF geht bei den Burschen daher nicht. Ich hab jedoch ein Beispiel für Bascom gefunden das den 23016 benutzt. Das sollte dir evtl. weiterhelfen... '********************************************************************** ' Application Note: Using the MCP23016 I/O expander '----------------------------------------------------- ' ' Program-ID.: AN245 MCP23016.bas. ' Date...... : 22 / 05 / 2005 ' Description: Example program for I2C MCP23016 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 MCP23016 shematic. ' ' This program provides an example for writing to and reading from ' an I2C MCP23016 device wired for device 0 (AO,A1 and A2 all pulled ' to Gnd). ' ' '********************************************************************** $regfile = "2313def.dat" ' Change for your AVR. $crystal = 4000000 ' 4 MHz Crystal. $baud = 19200 ' 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 = Portd.6 ' I2C Data. Config Scl = Portd.5 ' I2C Clock. '********************************************************************** '********************************************************************** ' '** Declare subroutines ** '********************************************************************** ' ' This subroutine writes data to the I2C MCP23016. ' Declare Sub I2c_mcp23016_write(byval Cmd As Byte , Byval Lsb As Byte , Byval Msb As Byte) ' ' This subroutine reads data from the I2C MCP23016. ' Declare Sub I2c_mcp23016_read '********************************************************************** '** 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 Mcp23016_adress_w As Byte Mcp23016_adress_w = &H40 'Write Adress: 0 1 0 0 A2 A1 A0 0 Dim Mcp23016_adress_r As Byte Mcp23016_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 '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 '********************************************************************** '** Actual work starts here. ** '********************************************************************** 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 Call I2c_mcp23016_write(&H02 , Lsb , Msb) 'Write Lsb & Msb to MCP23016 Rotate Lsb , Left , 1 'Rotate the bits Rotate Msb , Left , 1 Waitms 100 'Wait 100ms Counter_1 = Counter_1 + 1 ' Increase the counter 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_mcp23016_write(&H02 , Lsb , Msb) 'All leds off Call I2c_mcp23016_write(&H06 , &H00 , &HFF) 'Initiallize It So The Lsbs Outputs Are & Msbs Are Inputs Do 'Read Msb and display on Lsb Call I2c_mcp23016_read ' Read Msb Print "Msb: " ; Msb ' Send value of Msb to serial port Lsb = Msb ' Copy Msb to Lsb Call I2c_mcp23016_write(&H02 , Lsb , Msb) ' Write Lsb to MCP23016 Wait 1 Loop End 'end program '********************************************************************** '** 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 Hier noch der Link dazu: http://www.mcselec.com/index.php?option=com_content&task=view&id=107&Itemid=57 Ich habe leider keinen MCP zum spielen hier :-) Tschü Ulli | ||
Antwort schreiben Antworten: Re: IC MCP23017 I2C Bus I/0 - Erweiterung (von Christian - 23.02.2009 17:36) |
Zur Übersicht - INFO - Neueste 50 Beiträge - Neuer Beitrag - Suchen - Zum C-Control-I-Forum - Zum C-Control-II-Forum