I2C I/O Expanstion

Mark.R

Member
Hi all,

I've just got hold of a copy David Lincolin's book on the picaxe (second Edition) and attached is the file for the I2C I/O expansion test routine, it complies ok but the when I run the simulator I get the error shown in the photo. Any ideas?

Code:
'I2C I/O expantion MCP23017 - Test Routine

#picaxe 18X

#freq m4

#gosubs 256
 
#no_data



symbol MCP23017 = %01000000    'Address code for MCP23017, address 000

symbol IOCON0 = $0A        'I/O control register address for bank = 0 (PowerOn default)

symbol GPIOA = $09        'GP I/O port register

symbol GPPU = $06            'GP port pull-ups

symbol IPOL = $01            'Input port polarity

symbol GPIOB = $19        'Port B data register in 8-bit mode

symbol IODIRB = $10        'Port B direction register in 8-bit mode

symbol BANK1 = %10000000    'Bank bit for 8-bit mode

symbol i2c_data = b0        'Working register for I2C data

    i2cwrite IOCON0, (BANK1)    'Set the Bank bit in IOCON for 8-bit mode

    i2cwrite IODIRB, (0)        'Set port B to outputs

    i2cwrite GPPU, ($FF)        'Set port A pull-ups on

    i2cwrite IPOL, ($FF)        'Set port A polarity

    

    do                        'Read byte from Port A and copy to Port B

        i2cread GPIOA, (i2c_data)    'Read Port A

        i2cwrite GPIOB, (i2c_data)    'Write the data to port B

    loop
 

Attachments

Last edited:

Aries

New Member
You need to have defined your I2C regime first using hi2csetup - something like ...
Code:
    hi2csetup i2cmaster,%01010100,i2cslow,i2cbyte
 

Mark.R

Member
That's done it, that line is missing from that code in the book.

How do you find/know what the "$" numbers to use for specific operations or parameters, I've looked at the data sheet and I cant find a $ anywhere?

Code:
'I2C I/O expantion MCP23017 - Test Routine

#picaxe 18X

#freq m4

#gosubs 256
 
#no_data



symbol MCP23017 = %01000000    'Address code for MCP23017, address 000

symbol IOCON0 = $0A        'I/O control register address for bank = 0 (PowerOn default)

symbol GPIOA = $09        'GP I/O port register

symbol GPPU = $06            'GP port pull-ups

symbol IPOL = $01            'Input port polarity

symbol GPIOB = $19        'Port B data register in 8-bit mode

symbol IODIRB = $10        'Port B direction register in 8-bit mode

symbol BANK1 = %10000000    'Bank bit for 8-bit mode

symbol i2c_data = b0        'Working register for I2C data

    hi2csetup i2cmaster, MCP23017, i2cFAST_16, i2cbyte

    hi2cout IOCON0, (BANK1)    'Set the Bank bit in IOCON for 8-bit mode

    hi2cout IODIRB, (0)        'Set port B to outputs

    hi2cout GPPU, ($FF)        'Set port A pull-ups on

    hi2cout IPOL, ($FF)        'Set port A polarity

    

    do                        'Read byte from Port A and copy to Port B

        i2cread GPIOA, (i2c_data)    'Read Port A

        i2cwrite GPIOB, (i2c_data)    'Write the data to port B

    loop
 
Last edited:

lbenson

Senior Member
The Lincoln book is ancient. The HI2C commands didn't exist when it was written--you should use them. You shouldn't rely on that book for anything but suggestions.

There's a fair amount that can be found on the forum on using the MCP23017 and 23008, for instance, this for 3 mcp23017s:
https://picaxeforum.co.uk/threads/need-to-know-code-to-interface-mcp23017-i-o-exp-via-i2c.26267/#post-322368

Also, when posting code, please don't attach it as a file unless it is too large for the forum requirements. Instead, put the code in your post between the forum tags, "[ code]" and "[ /code]" (but omit the space after "[").
 

Aries

New Member
Just to answer your question about dollars etc

Code:
symbol XYZ = 10       ' sets XYZ to be decimal 10
symbol XYZ = $10      ' sets XYZ to be hexadecimal 10 - i.e. 16
symbol XYZ = %10      ' sets XYZ to be binary 10 - i.e. 2
 
Top