Menu

New MPC23017 library working.

jackjames
2018-08-08
2018-09-11
  • jackjames

    jackjames - 2018-08-08

    I rewrote the code to program the MCP2307 extender so that it works with both the hardwareI2C and the software I2C.
    Inside the file you will find many useful functions with instructions for use.
    Enjoy yourselves.

     

    Last edit: jackjames 2018-08-08
    • Anobium

      Anobium - 2018-08-11

      @jackjames. Great job.

      Questions:

      1. Does this replace the one that was shipped with version 0.98.02? The header is totally different and we have therefore lost the change log?
      2. Are there any demos that go with this library? If yes, can you put in GitHub please
      3. And, are there any older demos that we need to retest?

      Thank you,

      Anobium

       
      • jackjames

        jackjames - 2018-08-11

        The previous demos included in version 0.98.02 can be used.
        The library supports I2C and HI2C.
        I'm out of Rome (my city) and I'll be back around September 10th.
        Until that date I can not prepare demo files.

        In the title of the discussion I was wrong to write the name of the circuit, MPC instead of MCP.
        If someone is able to correct it .....

         

        Last edit: jackjames 2018-08-11
        • stan cartwright

          stan cartwright - 2018-08-11

          I have not had time to try your MCP2307.h
          In latest gcb release MCP23017.h uses i2c yet my servo robot demo I posted uses hi2c.
          I am also confused as to what include is which.
          I have this code snippet but was before latest gcb release.
          note it's hi2c..and useful servo motor example maybe.

          #chip mega328p,16
          #include <MCP23017.h>
          #option Explicit
          
          #define HI2C_DATA          ' This is required when using hardware I2C
          #define hI2C_MODE Master
          #define HI2C_BAUD_RATE 400
          #define hI2C_DATA PORTC.4
          #define hI2C_CLOCK PORTC.5
          
          #define MCP23017_addr 0x4e
          MCP23017_sendbyte (MCP23017_addr,MCP23017_IODIRA,0) ; make porta output
          #define forward 1
          #define reverse 255
          #define stop 0
          dim int_speed as word
          dim lmotdir,lmotor,rmotdir,rmotor,lmotval,rmotval as byte
          ;lmotdir=1:lmotor=1
          lmotor=1:rmotor=1
          #define slow 63036 ;50Hz
          #define fast 64286 ;100Hz
          int_speed=fast
          dir portb.0 out ;scope
          
          ;interrupt drives motors
              ' Add the handler for the interrupt
              On Interrupt Timer1Overflow Call motors
              InitTimer1 Osc, PS_1_64
              StartTimer 1
              SetTimer 1, int_speed
          ;
          ;-------------------------
          MCP23017_sendbyte( MCP23017_addr,MCP23017_GPIOA,0) ;set port a off
          wait 2  s
          ;
          
          do
          lmotdir=reverse:rmotdir=reverse
          wait 2  s
          
          lmotdir=stop:rmotdir=stop
          wait 2  s
          
          lmotdir=forward:rmotdir=forward
          wait 2  s
          
          lmotdir=stop:rmotdir=stop
          wait 2  s
          
          lmotdir=reverse:rmotdir=forward
          wait 2  s
          
          lmotdir=stop:rmotdir=stop
          wait 2  s
          
          lmotdir=forward:rmotdir=reverse
          wait 2  s
          
          lmotdir=stop:rmotdir=stop
          wait 2  s
          
          loop
          ;----- end
          
          'This will be called when the Timer overflows
          Sub motors
            SetTimer 1, int_speed ;reset timer
          ;  portb.0 = !portb.0 ;scope pin
          
          ;left stepper motor
            if lmotdir<>stop then
              if lmotdir=forward Then
                lmotor++
                if lmotor=5 then lmotor=1
              else ;reverse
                lmotor---
                if lmotor=0 then lmotor=4
              end if
          ;which phase left motor
              select case lmotor
                case 1
                lmotval=b'10010000'
                case 2
                lmotval=b'11000000'
                case 3
                lmotval=b'01100000'
                case 4
                lmotval=b'00110000'
              End Select
            Else
              lmotval=0 ;stop=off
            end if
          ;right stepper motor
            if rmotdir<>stop then
              if rmotdir=forward Then
                rmotor++
                if rmotor=5 then rmotor=1
              else ;reverse
                rmotor---
                if rmotor=0 then rmotor=4
              end if
          ;which phase right motor
              select case rmotor
                case 1
                rmotval=b'00001001'
                case 2
                rmotval=b'00000011'
                case 3
                rmotval=b'00000110'
                case 4
                rmotval=b'00001100'
              End Select
            else
              rmotval=0 ;stop=off
            end if
          
            MCP23017_sendbyte (MCP23017_addr,MCP23017_GPIOA,lmotval+rmotval) ;sets porta to lmotval+rmotval
          
          End Sub
          ;----------
          
           
  • stan cartwright

    stan cartwright - 2018-08-08

    Good work. I use the chip for driving 2 stepper motors using the gcb include.
    I'll check yours.
    seems the gcb version was i2c and I just used hi2c.
    jackjames,perry&venn
    nice chip, I have no prob driving 2 servos on interrupt with this chip. Really useful device.

     

    Last edit: stan cartwright 2018-08-08
  • jackjames

    jackjames - 2018-08-12

    No functional changes were made by the old version of MCP23017.h.
    The possibility of using HI2C has also been added.
    Library routines make calls to old and working (I2c and HI2C) MCP23017_SendByte and MCP23017_ReadByte.
    I think there are no compatibility issues with previously written software.

     
  • stan cartwright

    stan cartwright - 2018-08-12

    This is mcp23017.h latest gcb

    #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-08-12

    I noticed that the definition of the registers of the MCP23017 in my library is different from the include file.
    But there are no problems because these registers are called from the routines inside the library and are not necessary for the use of the routines by the program.

     
  • jackjames

    jackjames - 2018-09-10

    This is the final library file ready to be used and, if you want, combined with the next version of the compiler.
    I modified the header with the standard wording of the libraries.
    If something is incorrect please the administrators to make the appropriate changes.

    The test files used in the old version of the library can be used.
    Currently I am very busy for work and unfortunately I do not have time to write test programs of the various routines.
    I know for sure that they work because I have used them (written for another compiler) in other programs.

     
  • Anobium

    Anobium - 2018-09-11

    Cheers Jack. Added to the next release, as follows:

    544 Fix MCP23017 Revised library for general release.

     

Log in to post a comment.