Netcruzer Library API  V2.03
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
nz_mcp23017.h File Reference

MCP23017 Input/Output functions. More...

Go to the source code of this file.

Data Structures

struct  _MCP23017
 

Macros

#define MCP23017_REG_IODIRA   0x00
 
#define MCP23017_REG_IODIRB   0x01
 
#define MCP23017_REG_IPOLA   0x02
 
#define MCP23017_REG_IPOLB   0x03
 
#define MCP23017_REG_GPINTENA   0x04
 
#define MCP23017_REG_GPINTENB   0x05
 
#define MCP23017_REG_DEFVALA   0x06
 
#define MCP23017_REG_DEFVALB   0x07
 
#define MCP23017_REG_INTCONA   0x08
 
#define MCP23017_REG_INTCONB   0x09
 
#define MCP23017_REG_IOCON   0x0A
 
#define MCP23017_REG_GPPUA   0x0C
 
#define MCP23017_REG_GPPUB   0x0D
 
#define MCP23017_REG_INTFA   0x0E
 
#define MCP23017_REG_INTFB   0x0F
 
#define MCP23017_REG_INTCAPA   0x10
 
#define MCP23017_REG_INTCAPB   0x11
 
#define MCP23017_REG_GPIOA   0x12
 
#define MCP23017_REG_GPIOB   0x13
 
#define MCP23017_REG_OLATA   0x14
 
#define MCP23017_REG_OLATB   0x15
 
#define MCP23017_ADDRESS   0x40 /*Base address if A0 to A3 are all 0 */
 
#define MCP23017_GPA0   0
 
#define MCP23017_GPA1   1
 
#define MCP23017_GPA2   2
 
#define MCP23017_GPA3   3
 
#define MCP23017_GPA4   4
 
#define MCP23017_GPA5   5
 
#define MCP23017_GPA6   6
 
#define MCP23017_GPA7   7
 
#define MCP23017_GPB0   8
 
#define MCP23017_GPB1   9
 
#define MCP23017_GPB2   10
 
#define MCP23017_GPB3   11
 
#define MCP23017_GPB4   12
 
#define MCP23017_GPB5   13
 
#define MCP23017_GPB6   14
 
#define MCP23017_GPB7   15
 
#define MCP23017_SETPIN   0x80
 
#define MCP23017_CLEARPIN   0
 
#define mcp23017_WritePin(obj, pin)   mcp23017_WritePin(obj, pin | 0x80)
 
#define mcp23017_ClearPin(obj, pin)   mcp23017_WritePin(obj, pin & 0x7f)
 
#define mcp23017_ReadPins(obj)   ((obj)->pin.Val)
 

Typedefs

typedef struct _MCP23017 MCP23017
 

Functions

INT8 mcp23017_Init (MCP23017 *obj)
 
INT8 mcp23017_ConfigDir (MCP23017 *obj, UINT16 dir)
 
INT8 mcp23017_ConfigDirMask (MCP23017 *obj, UINT16 dir, UINT16 mask)
 
INT8 mcp23017_ConfigPullups (MCP23017 *obj, UINT16 pullUps)
 
INT8 mcp23017_ConfigIntPins (MCP23017 *obj, UINT16 intPins)
 
INT8 mcp23017_WritePin (MCP23017 *obj, UINT8 pinVal)
 
INT8 mcp23017_WriteMask (MCP23017 *obj, UINT16 val, UINT16 mask)
 
INT8 mcp23017_ReadPins (MCP23017 *obj)
 

Detailed Description

MCP23017 Input/Output functions.

MCP23017 I/O Expander functions.

Author
Modtronix Engineering
Compiler:
MPLAB XC16 Compiler

Software License Agreement

The software supplied herewith is owned by Modtronix Engineering, and is protected under applicable copyright laws. The software supplied herewith is intended and supplied to you, the Company customer, for use solely and exclusively on products manufactured by Modtronix Engineering. The code may be modified and can be used free of charge for commercial and non commercial applications. All rights are reserved. Any use in violation of the foregoing restrictions may subject the user to criminal sanctions under applicable laws, as well as to civil liability for the breach of the terms and conditions of this license.

THIS SOFTWARE IS PROVIDED IN AN 'AS IS' CONDITION. NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.

File History

2011-06-03, David Hosken (DH):

  • Initial version
Author
Modtronix Engineering
Compiler:
MPLAB XC16 compiler

Description

Functions for using the MCP23017 I/O Expander

Configuration

The following defines are used to configure this module, and should be placed in projdefs.h. Note that all items marked [-DEFAULT-] are defaults, and do not have to be placed in projdefs.h if they contain desired configuration! For details, see Project Configuration.

// *********************************************************************
// -------------- MCP23017 Configuration --------------------
// *********************************************************************
//Uncomment one of the following, depending on what I2C bus should be used
#define MCP23017_USE_I2C1 //[-DEFAULT-] Use I2C Bus 1
//#define MCP23017_USE_I2C2 //Use I2C Bus 2
//#define MCP23017_USE_I2C3 //Use I2C Bus 3
//#define MCP23017_USE_I2C4 //Use I2C Bus 4

Macro Definition Documentation

#define mcp23017_ClearPin (   obj,
  pin 
)    mcp23017_WritePin(obj, pin & 0x7f)

Clear a single pin. For example, to set pin GPA5, use command: mcp23017_ClearPin(0x40, MCP23017_GPA5)

Parameters
objStructure with data for MCP23017 chip
pinAddress of pin to clear. Is a value from 0 to 15. 0=GPA0, 7=GPA7 8=GPB0 and 15=GPB7
#define mcp23017_ReadPins (   obj)    ((obj)->pin.Val)

Returns the current pin values saved in the MCP23017 object. The current pin values are NOT read from the MCP23017 chip. The mcp23017_ReadPins() will cause MCP23017 chip to be read. Use DINPTST1_IOX_MASK_XXX constants to test if pin is 0 or 1.

Parameters
objPointer to structure with data for MCP23017 chip
Returns
Word with pin values in bits 0 to 15
#define mcp23017_WritePin (   obj,
  pin 
)    mcp23017_WritePin(obj, pin | 0x80)

Set a single pin. For example, to set pin GPA5, use command: mcp23017_WritePin(0x40, MCP23017_GPA5)

Parameters
objStructure with data for MCP23017 chip
pinAddress of pin to set. Is a value from 0 to 15. 0=GPA0, 7=GPA7 8=GPB0 and 15=GPB7

Typedef Documentation

typedef struct _MCP23017 MCP23017

Structure containing information for a MCP23017 chip. Can be multiple MCP23017 on a bus.

Function Documentation

INT8 mcp23017_ConfigDir ( MCP23017 obj,
UINT16  dir 
)

Configure direction of pins.

Parameters
objStructure with data for MCP23017 chip
dirDirection bit for 16 port pins
Returns
Returns 0 if OK, else returns a non-zero value if error

Configure direction of pins.

Parameters
objStructure with data for MCP23017 chip
dirBits indicate direction of I/O pins. A 0 sets pin to output, and a 1 sets pin to input.
Returns
Returns 0 if OK, else returns a non-zero value if error
INT8 mcp23017_ConfigDirMask ( MCP23017 obj,
UINT16  dir,
UINT16  mask 
)

Configure direction of pins. A mask and value is supplied.

Parameters
objStructure with data for MCP23017 chip
dirDirection bit for 16 port pins
mask16-bit mask
Returns
Returns 0 if OK, else returns a non-zero value if error
INT8 mcp23017_ConfigIntPins ( MCP23017 obj,
UINT16  intPins 
)

Configures what pins to use for interrupt-on-change function. If the value of any of these pins change, a interrupt will be generated.

Parameters
objStructure with data for MCP23017 chip
intPinsBits indicate what pins to use for interrupt-on-change function
Returns
Returns 0 if OK, else returns a non-zero value if error
INT8 mcp23017_ConfigPullups ( MCP23017 obj,
UINT16  pullUps 
)

Configure pull-up resistors.

Parameters
objStructure with data for MCP23017 chip
pullUpsBits indicate if pull-up's are active for 16 port pins
Returns
Returns 0 if OK, else returns a non-zero value if error

Configure pull-up resistors.

Parameters
objStructure with data for MCP23017 chip
pullUpsBits indicate if pull-up is active.
Returns
Returns 0 if OK, else returns a non-zero value if error
INT8 mcp23017_Init ( MCP23017 obj)

Initializes the MCP23017 I/O Expander

Pre-Condition:
The I2C port must have been initialized for Master Mode operation before calling this function!
Parameters
objStructure with data for MCP23017 chip
Returns
Returns 0 if OK, else returns a non-zero value if error

Initializes the MCP23017 I/O Expander

Pre-Condition:
The I2C port must have been initialized for Master Mode operation before calling this function!
Parameters
objStructure with data for MCP23017 chip.
Returns
Returns 0 if OK, else returns a non-zero value if error
INT8 mcp23017_ReadPins ( MCP23017 obj)

Reads value of all pins, and updates obj->pin. This function does a read of the MCP23017 chip, and updates the pin value in the given MCP23017 object.

Parameters
objPointer to structure with data for MCP23017 chip
Returns
0 if all pins same as for last read, 1 if pins had different value -1 if error
INT8 mcp23017_WriteMask ( MCP23017 obj,
UINT16  val,
UINT16  mask 
)

Set/clear all pins by providing a 16 bit value and mask. For example, to set pin 0 and 7, and clear pins 4 and 5, use command: mcp23017_WriteMask(0x40, 0b0000000010000001, 0b0000000010110001);

Parameters
objStructure with data for MCP23017 chip
val16-bit pin values
mask16-bit mask
Returns
Returns 0 if OK, else returns a non-zero value if error
INT8 mcp23017_WritePin ( MCP23017 obj,
UINT8  pinVal 
)

Write value to pin. For example, to set pin GPA5, use command: mcp23017_WritePin(0x40, MCP23017_GPA5 | MCP23017_SETPIN) Or, to clear pin GPB7, use command: mcp23017_WritePin(0x40, MCP23017_GPB7 | MCP23017_CLEARPIN)

Parameters
objStructure with data for MCP23017 chip
pinValIndicates what pin to set/clear, and value. Bits 0-3 give pin's address, and bit 7 it's value
Returns
Returns 0 if OK, else returns a non-zero value if error