Menu

MCP23017 INCLUDE FILE

jackjames
2018-03-12
2018-03-26
  • jackjames

    jackjames - 2018-03-12

    I wrote this file include because I need to use the MCP23017.
    I have not tried it yet but checking it I think there should be no problems.
    I hope it can be useful ...

     
  • jackjames

    jackjames - 2018-03-12

    Here is the file:

     
  • Moto Geek

    Moto Geek - 2018-03-13

    Excellent! I will have to give it a go! I love the 23017, One of the first expanders I ever messed with. Your code looks very neat! Thanks @jackjames.

     
  • Moto Geek

    Moto Geek - 2018-03-13

    Excellent! I will have to give it a go! I love the 23017, One of the first expanders I ever messed with. Your code looks very neat and bilingual too! Thanks @jackjames.

     
  • jackjames

    jackjames - 2018-03-13

    In reality it was only commented in Italian, my language, then I added the translation in English and the part in Italian remained for my laziness ...

     
    • Chris Roper

      Chris Roper - 2018-03-13

      Well I second what Moto Geek said.
      I think that is the neatest code presentation I have ever seen.
      If that represents your Laziness It far exceeds my best documentation efforts :>)

      Great contribution.

      However, I should point out that one is already included with GCBASIC.

      C:\GCB@Syn\GreatCowBasic\Include\MCP23017.h, you may want to have a look through that folder for any other devices you may need, it could save you some time. But then again it could deprive you of the creative fun of rolling your own :)

      Cheers
      Chris

       

      Last edit: Chris Roper 2018-03-13
  • jackjames

    jackjames - 2018-03-13

    @Chris
    Yes, I had already seen the include file MCP23017.H.
    In fact the two routines
    MCP23017_sendbyte
    and
    MCP23017_readbyte
    were copied from that file.
    ... Then I added my ...
    I am used to working with complex programs that have tens of thousands of lines of code, so if they are not well commented and you read them over time it is difficult to understand them right away.

     

    Last edit: jackjames 2018-03-13
  • stan cartwright

    stan cartwright - 2018-03-13

    A quick look and nice one. I use these and like them. I only used 8 lines for out to stepper motors so far but was easy. 8 more to use. It seems i2c not hi2c. Anobium recomended change to detect hi2c or i2c. How, detect #define HI2C_DATA ? posted elsewhere.

     
    • Chris Roper

      Chris Roper - 2018-03-13

      How, detect #define HI2C_DATA ?

        #ifdef HI2C_DATA
          ;
          ; Hardware I2C code here
          ;
        #endif
      
        #ifdef I2C_DATA
          ;
          ; Software I2C code here
          ;
        #endif
      

      Try looking in the existing Libraries and the GCBASIC Help Files:
      http://gcbasic.sourceforge.net/help/__ifdef.html

       

      Last edit: Chris Roper 2018-03-13
  • stan cartwright

    stan cartwright - 2018-03-13

    @Chris. thanks for bringing to my attention #ifdef HI2C_DATA
    I'll change the expander incude to try.

     
  • jackjames

    jackjames - 2018-03-13

    I modified the file to use it with I2c and Hi2C.

     
  • Chris Roper

    Chris Roper - 2018-03-13

    Excellent.

     
  • stan cartwright

    stan cartwright - 2018-03-13

    I deleted the MCP23017 include and used yours in a program that worked but now doesn't. 2 errors i2c ports not defined. I dug the old one out the recycle bin and restored and program works again. The recycled one was i2c only.

     
  • Anobium

    Anobium - 2018-03-13

    @jackjames.   We now have a forked development for this library. When you have tested and got it working ... is the plan to   replace the existing library?

     
  • jackjames

    jackjames - 2018-03-13

    @Anobium:
    YES.

    @Stan:
    Seeing some example files I noticed that when using the hardware I2C you have to define:
    ; ----- Define Hardware settings - choose hardware or software I2C - NOT BOTH !!
    '' Define I2C settings
    '#define HI2C_BAUD_RATE 400
    'HI2CMode Master

    --------> '#define HI2C_DATA ' This is required when using I2C hardware <--------
    Therefore HI2C_DATA must be defined with the I2C hardware.

    I have not found the definition of I2C_DATA anywhere.

     

    Last edit: jackjames 2018-03-13
  • stan cartwright

    stan cartwright - 2018-03-14

    I just used your last mcp23017 and new errors. Old version works ok.
    Test for hi2c and if not must be i2c

     

    Last edit: stan cartwright 2018-03-14
  • stan cartwright

    stan cartwright - 2018-03-14

    I'm using anobium's version. Differrent define names?

    '    Created Evan R Venn - Oct 2013 - 2017
    '    Thanks to Perry'
    #define MCP23017_DEVICE_1 0x48
    
    #define MCP23017_IODIRA 0x00  ' default is INput  0= out 1= in
    #define MCP23017_IODIRB 0x01
    #define MCP23017_IPOLA  0x02  ' input polarity 0= normal, 1= inverted
    #define MCP23017_IPOLB  0x03
    #define MCP23017_GPINTENA 0x04  ' Allow interrupt on change
    #define MCP23017_GPINTENB 0x05
    #define MCP23017_DEFVALA  0x06  ' default value BEFORE interrupt [for easy compare]
    #define MCP23017_DEFVALB  0x07
    #define MCP23017_INTCONA  0x08 ' I/O EXPANDER CONFIGURATION .2 1= Open Drain [wire together interrupts]
    #define MCP23017_INTCONB  0x09
    #define MCP23017_IOCONA  0x0A ' 0= Interrupt on change , 1= Interrupt on Difference
    #define MCP23017_IOCONB  0x0B
    #define MCP23017_GPPUA  0x0C ' weak pull up resistors [~100kOhhm] very handy
    #define MCP23017_GPPUB  0x0D
    #define MCP23017_ITFA 0x0E ' who done it, the interrupt
    #define MCP23017_ITFB 0x0F
    #define MCP23017_INTCAPA  0x10 ' Saves the event that caused the interrupt
    #define MCP23017_INTCAPB  0x11
    #define MCP23017_GPIOA  0x12 ' the IO port
    #define MCP23017_GPIOB  0x13
    #define MCP23017_OLATA  0x14 ' the LATCH of the IO port see docs
    #define MCP23017_OLATB  0x15
    
    sub MCP23017_sendbyte( in MCP23017_device, in MCP23017_instr , in MCP23017_data )
    
        ' see the Data sheet for this!!! FIGURE 1-1: MCP23017 I2C DEVICE PROTOCOL
        I2CSTART
        I2CSEND( MCP23017_device )
        I2CSEND( MCP23017_instr )
        I2CSEND( MCP23017_data )
        'initiate Stop Condition
        I2CSTOP
    
    end sub
    
    sub MCP23017_readbyte( in MCP23017_device, in MCP23017_instr , Out MCP23017_data )
    
        ' see the Data sheet for this!!! FIGURE 1-1: MCP23017 I2C DEVICE PROTOCOL
        I2CSTART
        I2CSEND( MCP23017_device )
        I2CSEND( MCP23017_instr )
        I2CSTART
        I2CSEND( MCP23017_device + 1)
        I2CReceive( MCP23017_data , NACK )
        'initiate Stop Condition
        I2CSTOP
    end sub
    
     
  • jackjames

    jackjames - 2018-03-14

    @Stan:
    I have verified the error and it is due to the fact that the definition MCP23017IoDirA used in your file in the new include file is called IODIRA without the prefix MCP2317.
    I had written this file to use it with another compiler so the definitions were named before deciding to get it to GCBasic.
    Now, to keep them compatible with the old one I renamed all the definitions with the same name used in the old file.
    I think I have eliminated the problems. Let me know how the test goes ...***

     
  • stan cartwright

    stan cartwright - 2018-03-14

    @jackjames, your last mcp23017.h worked ok with the test program I was using.
    I only tested porta out.

     
  • jackjames

    jackjames - 2018-03-14

    OK- Thanks

     
  • stan cartwright

    stan cartwright - 2018-03-14

    And it detects hi2c or i2c. I changed this from i2c and it works.

    #define HI2C_DATA          ' This is required when using hardware I2C
       #define hI2C_MODE Master
       #define hI2C_DATA PORTC.4
       #define hI2C_CLOCK PORTC.5
    
     
  • stan cartwright

    stan cartwright - 2018-03-14

    I'll test the device more, I'll try interrupt on portb change next and get back only if a problem. regards @stan

     
  • jackjames

    jackjames - 2018-03-14

    OK- Thanks

     
  • jackjames

    jackjames - 2018-03-26

    @Stan:
    Did you do any other checks?
    Do you think it is valid to be included in the 'include' files of the GCB?

     
  • stan cartwright

    stan cartwright - 2018-03-26

    Sorry @jackjame, not tested interrupt..needs an interrupt to test the interrupts. been busy.
    Another forum user wanted to use spi version. Again I didn't get a round tuit.

     

Log in to post a comment.