Menu

MCP23017 demo

2017-12-14
2017-12-14
  • stan cartwright

    stan cartwright - 2017-12-14

    As there isn't a demo for mcp23017.h, I thought I'd share using porta as out.
    First setting the i2c address by tieing pins 15,16,17 - a0,a1,a2 hi lo. I used gcb i2c finder and it gave address as 0x40 with a0,1,2 grounded which contradicts with info but that was arduino and maybe 7bit address??
    This line is important, you can't #define MCP23017_GPIOA 0
    MCP23017_sendbyte( MCP23017_addr,MCP23017_GPIOA,0) ;set port a to out
    The interrupt probably wrong,should be 100 Hz.
    https://youtu.be/PKG3Z9pjrAU

    #chip mega328p,16
    #include <MCP23017.h>
    #option Explicit
       #define I2C_MODE Master
       #define I2C_DATA PORTC.4
       #define I2C_CLOCK PORTC.5
       #define I2C_DISABLE_INTERRUPTS ON
       'Optionally, you can reduce the I2C timings.
       #define I2C_BIT_DELAY 0 us
       #define I2C_CLOCK_DELAY 1 us
       #define I2C_END_DELAY 0 us
    
    #define MCP23017_addr 0x40
    ;#define MCP23017_IODIRA 0; make porta output
    MCP23017_sendbyte (MCP23017_addr,MCP23017_IODIRA,0)
    dim lmotforward,lmotor,rmotforward,rmotor,lmotval,rmotval as byte
    lmotforward=1:lmotor=1:rmotforward=1:rmotor=1
    
    ;interrupt drives motors
        On Interrupt Timer1Overflow Call InterruptHandler
        ' Initialise the timer - this is required
        ' Set prescaler to 1 and then start the timer
        InitTimer1 Osc, PS_1_1
        ' Start the timer - this is required
        StartTimer 1
        'Set here to initialise but you need to set in the Interrupt routine handler
        ' Use the Timer Calc' application that is part of Mister E PIC Mutl-calc tookset to calculate the value.
        ' Ensure you set the Prescaler in the InitTimer1 shown above
        SetTimer 1, 25538
    ;
    MCP23017_sendbyte( MCP23017_addr,MCP23017_GPIOA,0) ;set port a to out
    ;
    do
    ;your code
    loop
    ;----- end 
    'This will be called when the Timer overflows
    Sub InterruptHandler
      ' Set the register to 6 to give us the 10ms Interrupt.
      ' Use the Timer Calc' application that is part of Mister E PIC Mutl-calc tookset to calculate the value.
      ' Ensure you set the Prescaler in the InitTimer1 shown above
      SetTimer 1, 25538 ;reset timer
    ;left stepper motor
      if lmotforward=1 Then
        lmotor++
        if lmotor=9 then lmotor=1
      else
        lmotor---
        if lmotor=0 then lmotor=8
      end if
    ;
      select case lmotor
        case 1
        lmotval=128
        case 2
        lmotval=192
        case 3
        lmotval=64
        case 4
        lmotval=96
        case 5
        lmotval=32
        case 6
        lmotval=48
        case 7
        lmotval=16
        case 8
        lmotval=144
      End Select
    
    ;right stepper motor
      if rmotforward=1 Then
        rmotor++
        if rmotor=9 then rmotor=1
      else
        rmotor---
        if rmotor=0 then rmotor=8
      end if
    ;
      select case rmotor
        case 1
        rmotval=1
        case 2
        rmotval=3
        case 3
        rmotval=2
        case 4
        rmotval=6
        case 5
        rmotval=4
        case 6
        rmotval=12
        case 7
        rmotval=8
        case 8
        rmotval=9
      End Select
    
    MCP23017_sendbyte (MCP23017_addr,MCP23017_GPIOA,lmotval+rmotval) ;sets porta to lmotval+rmotval
    
    End Sub
    
     
  • stan cartwright

    stan cartwright - 2017-12-14

    This gives 98Hz interrupt but not from the calculator..trial error

    ;interrupt drives motors
        ' Add the handler for the interrupt
        On Interrupt Timer0Match1 Call InterruptHandler
    
        Dim OCR0  AS byte alias OCR0A
        Dim TCCR0 AS  byte alias TCCR0B
        WGM01 = 1                'Timer in CTC mode - required
    
        ' Set the correct regsisters.  These registers give us the correct timed Interrupt.
        ' Use the TimerCalculator from MikroElectronika to obtain the correct values for these registers.
        ' When using the TimerCalculator from MikroElectronika paste the any registers with *_Bit registers
        ' Simple cut and paste from the BASIC tab
    
        'Timer0 Prescaler = 0; Preload = 159; Actual Interrupt Time = 10 us
        OCR0 = 19
        TCCR0 = 0x28
        TCCR0 = TCCR0 or 0x05
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.