Reply to topic  [ 1 post ] 
HCMCP23017 - Library for MCP23017 Serial 16bit Expander IC 
Author Message
Site Admin

Joined: Sun Aug 05, 2012 4:02 pm
Posts: 633
Post HCMCP23017 - Library for MCP23017 Serial 16bit Expander IC
Image



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:
#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).



Code:
HCMCP23017.out(Data);


Sets the output pin state for all 16 pins (port A & B) where:

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:
byte Result = HCMCP23017.inA();


Gets the input state (high/low) of all 8 pins assigned to port A.

Returns an 8 bit byte containing the state of the pins (bit 0 = port A pin 0,
bit 7 = port A pin 7)




Code:
byte Result = HCMCP23017.inB();


Gets the input state (high/low) of all 8 pins assigned to port B.

Returns an 8 bit byte containing the state of the pins (bit 0 = port B pin 0,
bit 7 = port B pin 7)



Code:
unsigned int Result = HCMCP23017.in();


Gets the input state (high/low) of all 16 pins (port A & B).

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:
HCMCP23017.I2CWriteByte(Register,  Data);


Writes a single byte of data to one of the devices I2C registers where:

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:
HCMCP23017.I2CWriteInt(Register, Data);


Writes a 16 bit value to two consecutive register pairs where:

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:
byte Data = HCMCP23017.I2CReadByte(Register);


Reads a single byte of data from one of the devices I2C registers where:

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:
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.


Image


Code:
/* 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);
}




Image

Attachment:
HCMCP23017.zip


You do not have the required permissions to view the files attached to this post.


Fri Jun 24, 2016 1:57 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 1 guest


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

Search for:
Jump to:  
cron
Thank you to phpBB.com & phpBB.ro.
Design created by Florea Cosmin Ionut.
© 2011 All contents of this site are © Hobby Components LTD