Arduino library for the Microchip Technology MCP23017 (I2C) serial 16 bit I/O expander IC. Currently supported products:
MCP23017 Serial 16bit Expander IC (HCCOIC0048) available from hobbycomponents.com
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: Select all
#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: Select all
HCMCP23017.Init();
The following functions are available with this library:
Code: Select all
HCMCP23017.pinDir(Pin, Dir);
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: Select all
HCMCP23017.pinPullup(Pin, State);
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: Select all
HCMCP23017.pinWrite(Pin, State);
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: Select all
boolean State = HCMCP23017.pinRead(Pin);
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: Select all
HCMCP23017.dirA(Dir);
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: Select all
HCMCP23017.dirB(Dir);
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: Select all
HCMCP23017.pullupA(byte State);
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: Select all
HCMCP23017.pullupB(byte State);
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: Select all
HCMCP23017.outA(Data);
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: Select all
HCMCP23017.outB(Data);
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: Select all
HCMCP23017.out(Data);
Data is a 16 bit pattern representing the state off all 16 pins (bit 0 = port
A pin 0, bit 15 = port b pin 7). Valid values for Data are:
0x0000 to 0xFFFF
Code: Select all
byte Result = HCMCP23017.inA();
Returns an 8 bit byte containing the state of the pins (bit 0 = port A pin 0,
bit 7 = port A pin 7)
Code: Select all
byte Result = HCMCP23017.inB();
Returns an 8 bit byte containing the state of the pins (bit 0 = port B pin 0,
bit 7 = port B pin 7)
Code: Select all
unsigned int Result = HCMCP23017.in();
Returns a 16 bit unsigned integer containing the state of the pins (bit 0 = port A pin 0, bit 15 = port B pin 7)
Code: Select all
HCMCP23017.I2CWriteByte(Register, Data);
Register is the address of the register to write to. See the HCMCP23017.h
header file for a list of valid addressed
Data is the 8 bit value to be written
Code: Select all
HCMCP23017.I2CWriteInt(Register, Data);
Register is the first address of the registers to write to.
See the HCMCP23017.h header file for a list of valid addressed
Data is the 16 bit value to be written
Code: Select all
byte Data = HCMCP23017.I2CReadByte(Register);
Register is the address of the register to read from. See the HCMCP23017.h
header file for a list of valid addressed
Returns a byte value containing the current state of the register.
Code: Select all
unsigned int Data = HCMCP23017.I2CReadInt(Register);
Reads a 16 bit value from two consecutive register pairs where:
Register is the first address of the registers to read from.
See the HCMCP23017.h header file for a list of valid addressed
Returns a 16 bit unsigned integer containing the current state of the
registers.
- /* FILE: HCMCP23017_Pin_Read_Example
- DATE: 24/06/16
- VERSION: 0.1
- AUTHOR: Andrew Davies
- Sketch created by Hobby Components Ltd (HOBBYCOMPONENTS.COM)
- 24/06/16 version 0.1: Original version
- 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:
- MCP23017 Serial 16 bit Expander IC (HCCOIC0048)
- Arduino Connections:
- Uno/Nano............MCP23017 (DIP)
- +5V.................VDD (Pin 9)
- GND.................VSS (Pin 10)
- A4..................SDA (Pin 13)
- A5..................SCL (Pin 13)
- 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 leave
- reference to HobbyComponents.com in your comments if you redistribute this code.
- This software may not be used directly for the purpose of selling products that
- directly compete with Hobby Components Ltd's own range of products.
- THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
- EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
- MERCHANTABILITY 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 ANY
- REASON WHATSOEVER.
- */
- /* Include the HCMCP23017 library */
- #include <HCMCP23017.h>
- /* Create an instance of the library and set the I2C slave address */
- HCMCP23017 HCMCP23017(0x20);
- void setup()
- {
- /* Initialise the library and serial interface*/
- HCMCP23017.Init();
- Serial.begin(9600);
- /* Set the pin to an input (default state) */
- HCMCP23017.pinDir(GPB7, PIN_INPUT);
- /* Enable its internal pullup */
- HCMCP23017.pinPullup(GPB7, PULLUP_ENABLED);
- }
- void loop()
- {
- Serial.print("Port B pin 7: ");
- /* Read the state of the pin */
- if(HCMCP23017.pinRead(GPB7))
- {
- Serial.println("HIGH");
- }else
- {
- Serial.println("LOW");
- }
- delay(500);
- }