This library allows a suitable Arduino to set and read the state of the 16 I/O available on the MCP23017 I/O expander IC via its I2C interface.

You will need to download (please log in to download the library) and unzip this library to the Arduino development environments library area.

On Windows: My Documents\Arduino\libraries\

On Mac: Documents/Arduino/libraries/

Linux:Usually found within the users home area under /Arduino/libraries/

Using the HCMCP23017 library

To use the library just include the HCMCP23017.h header file and then create an instance of the library. E.g:

Code:

#include "HCMCP23017.h"HCMCP23017 HCMCP23017(I2CAdd);

Where I2CAdd is the I2C slave address of the device. The MCP23017 can be set to one of 8 different slave addresses depnding on the state of the 3 slave address pins (A0-A2).

To initialise the library add the following line to the setup() section of your sketch:

Code:

HCMCP23017.Init();

The following functions are available with this library:

Code:

HCMCP23017.pinDir(Pin, Dir);

Sets the direction (input/output) of one of the 16 I/O pins where:

Pin is the pin number to set the direction for. Valid values for Pin are 0 to 15 (0 = pin 0 on port A, 15 = pin 7 on port B)

Dir is the required pin direction. Valid values are PIN_INPUT (sets the specified pin to an input) PIN_OUTPUT (sets the specified pin to an output)

Code:

HCMCP23017.pinPullup(Pin, State);

Sets the state (enabled/disabled) of one of the 16 internal pullups where:

Pin is the pin number to set the pullup state for. Valid values for Pin are 0 to 15 (0 = pin 0 on port A, 15 = pin 7 on port B)

State is the required pullup state. Valid values are PULLUP_ENABLED (enables the pullup for the specified pin) PULLUP_DISABLED (disables the pullup for the specified pin)

Code:

HCMCP23017.pinWrite(Pin, State);

Sets the output state (high/low) of one of the 16 I/O pins where:

Pin is the pin number to set the output state for. Valid values for Pin are 0 to 15 (0 = pin 0 on port A, 15 = pin 7 on port B)

State is the required output state. Valid values for State are HIGH (pin is driven high) LOW (pin is driven low)

Code:

boolean State = HCMCP23017.pinRead(Pin);

Gets the output state (high/low) of one of the 16 I/O pins where:

Pin is the pin number to get the input state for. Valid values for Pin are 0 to 15 (0 = pin 0 on port A, 15 = pin 7 on port B)

Returns a boolean value representing the current state of the pin.

Code:

HCMCP23017.dirA(Dir);

Sets the direction (input/output) of all 8 pins assigned to port A where:

Dir is the 8 bit pattern which sets the direction of all 8 pins. Valid values for Dir are:

0x00 to 0xFF (setting any bit to zero will set the appropriate pin to an output, setting it to a 1 will set the pin to an input) ALL_INPUTS (sets all 8 pins of port A to inputs) ALL_OUTPUTS (sets all 8 pins of port A to outputs)

Code:

HCMCP23017.dirB(Dir);

Sets the direction (input/output) of all 8 pins assigned to port B where:

Dir is the 8 bit pattern which sets the direction of all 8 pins. Valid values for Dir are:

0x00 to 0xFF (setting any bit to zero will set the appropriate pin to an output, setting it to a 1 will set the pin to an input) ALL_INPUTS (sets all 8 pins of port B to inputs) ALL_OUTPUTS (sets all 8 pins of port B to outputs)

Code:

HCMCP23017.pullupA(byte State);

Sets the state (enabled/disabled) of all 8 internal pullups assigned to port A where:

State is the 8 bit pattern which sets the state of all 8 pullups. Valid values for State are:

0x00 to 0xFF (setting any bit to zero will disable the pullup for the corresponding pin, setting it to a 1 will will enable the pullup) ALL_PULLUPS_ENABLED (enables all 8 pullups of port A) ALL_PULLUPS_DISABLED (disables all 8 pullups of port A)

Code:

HCMCP23017.pullupB(byte State);

Sets the state (enabled/disabled) of all 8 internal pullups assigned to port A where:

State is the 8 bit pattern which sets the state of all 8 pullups. Valid values for State are:

0x00 to 0xFF (setting any bit to zero will disable the pullup for the corresponding pin, setting it to a 1 will will enable the pullup) ALL_PULLUPS_ENABLED (enables all 8 pullups of port B) ALL_PULLUPS_DISABLED (disables all 8 pullups of port B)

Code:

HCMCP23017.outA(Data);

Sets the output state (high/low) of all 8 pins assigned to port A where:

Data is the 8 bit pattern which sets the state of all 8 pins. Valid values for Data are:

0x00 to 0xFF (setting any bit to zero will set the appropriate pin output to a low state, setting it to a 1 will set the output to a high state).

Code:

HCMCP23017.outB(Data);

Sets the output state (high/low) of all 8 pins assigned to port B where:

Data is the 8 bit pattern which sets the state of all 8 pins. Valid values for Data are:

0x00 to 0xFF (setting any bit to zero will set the appropriate pin output to a low state, setting it to a 1 will set the output to a high state).

This example sketch demonstrates how to use the HCMCP2017 library to read the input state of one of the 16 I/O pins (GPA0/Pin 21) on the MCP23017. The sketch will continually read and output the state of pin 7 on port B.Currently supported products:

This sketch also assumes the slave 3 address pins (A0-A2) on the MCP23017 are pulled low.

You may copy, alter and reuse this code in any way you like, but please leavereference to HobbyComponents.com in your comments if you redistribute this code.This software may not be used directly for the purpose of selling products thatdirectly compete with Hobby Components Ltd's own range of products.

THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHEREXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OFMERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANYREASON WHATSOEVER.*/

/* Include the HCMCP23017 library */#include <HCMCP23017.h>

/* Create an instance of the library and set the I2C slave address */HCMCP23017 HCMCP23017(0x20);

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum