Netcruzer Library API  V2.03
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
nz_mcp23017.h
Go to the documentation of this file.
1 
30  /*********************************************************************
31  * @section nz_mcp23017_lic Software License Agreement
32  *
33  * The software supplied herewith is owned by Modtronix Engineering, and is
34  * protected under applicable copyright laws. The software supplied herewith is
35  * intended and supplied to you, the Company customer, for use solely and
36  * exclusively on products manufactured by Modtronix Engineering. The code may
37  * be modified and can be used free of charge for commercial and non commercial
38  * applications. All rights are reserved. Any use in violation of the foregoing
39  * restrictions may subject the user to criminal sanctions under applicable laws,
40  * as well as to civil liability for the breach of the terms and conditions of this license.
41  *
42  * THIS SOFTWARE IS PROVIDED IN AN 'AS IS' CONDITION. NO WARRANTIES, WHETHER EXPRESS,
43  * IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
44  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE
45  * COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
46  * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
47  **********************************************************************
48  * File History
49  *
50  * 2011-06-03, David Hosken (DH):
51  * - Initial version
52  *********************************************************************/
53 #ifndef _MCP23017_H_
54 #define _MCP23017_H_
55 
56 #define MCP23017_REG_IODIRA 0x00
57 #define MCP23017_REG_IODIRB 0x01
58 #define MCP23017_REG_IPOLA 0x02
59 #define MCP23017_REG_IPOLB 0x03
60 #define MCP23017_REG_GPINTENA 0x04
61 #define MCP23017_REG_GPINTENB 0x05
62 #define MCP23017_REG_DEFVALA 0x06
63 #define MCP23017_REG_DEFVALB 0x07
64 #define MCP23017_REG_INTCONA 0x08
65 #define MCP23017_REG_INTCONB 0x09
66 #define MCP23017_REG_IOCON 0x0A
67 #define MCP23017_REG_GPPUA 0x0C
68 #define MCP23017_REG_GPPUB 0x0D
69 #define MCP23017_REG_INTFA 0x0E
70 #define MCP23017_REG_INTFB 0x0F
71 #define MCP23017_REG_INTCAPA 0x10
72 #define MCP23017_REG_INTCAPB 0x11
73 #define MCP23017_REG_GPIOA 0x12
74 #define MCP23017_REG_GPIOB 0x13
75 #define MCP23017_REG_OLATA 0x14
76 #define MCP23017_REG_OLATB 0x15
77 
78 /* Not used
79 #define MCP23017_REG_IODIRA_B1 0x00
80 #define MCP23017_REG_IODIRB_B1 0x10
81 #define MCP23017_REG_IPOLA_B1 0x01
82 #define MCP23017_REG_IPOLB_B1 0x11
83 #define MCP23017_REG_GPINTENA_B1 0x02
84 #define MCP23017_REG_GPINTENB_B1 0x12
85 #define MCP23017_REG_DEFVALA_B1 0x03
86 #define MCP23017_REG_DEFVALB_B1 0x13
87 #define MCP23017_REG_INTCONA_B1 0x04
88 #define MCP23017_REG_INTCONB_B1 0x14
89 #define MCP23017_REG_IOCON_B1 0x05
90 #define MCP23017_REG_GPPUA_B1 0x06
91 #define MCP23017_REG_GPPUB_B1 0x16
92 #define MCP23017_REG_INTFA_B1 0x07
93 #define MCP23017_REG_INTFB_B1 0x17
94 #define MCP23017_REG_INTCAPA_B1 0x08
95 #define MCP23017_REG_INTCAPB_B1 0x18
96 #define MCP23017_REG_GPIOA_B1 0x09
97 #define MCP23017_REG_GPIOB_B1 0x19
98 #define MCP23017_REG_OLATA_B1 0x0A
99 #define MCP23017_REG_OLATB_B1 0x1A
100 */
101 
102 #define MCP23017_ADDRESS 0x40 /*Base address if A0 to A3 are all 0 */
103 
104 //Bit address of port pins. A value from 0 - 15 are allocated for port pins GPA0-GPA7 and GPB0-GPB7
105 #define MCP23017_GPA0 0
106 #define MCP23017_GPA1 1
107 #define MCP23017_GPA2 2
108 #define MCP23017_GPA3 3
109 #define MCP23017_GPA4 4
110 #define MCP23017_GPA5 5
111 #define MCP23017_GPA6 6
112 #define MCP23017_GPA7 7
113 #define MCP23017_GPB0 8
114 #define MCP23017_GPB1 9
115 #define MCP23017_GPB2 10
116 #define MCP23017_GPB3 11
117 #define MCP23017_GPB4 12
118 #define MCP23017_GPB5 13
119 #define MCP23017_GPB6 14
120 #define MCP23017_GPB7 15
121 
122 #define MCP23017_SETPIN 0x80
123 #define MCP23017_CLEARPIN 0
124 
125 
129 typedef struct __attribute__((aligned(2), packed)) _MCP23017
130 {
131  BYTE adr; //Slave I2C address. Depending on state of A0 to A2 lines (bits 1,2 and 3), can be 0x40 to 0x4E
132  BYTE fill; //Filler byte
133  WORD_VAL dir; //Direction of 16 ports, 0=output, 1=input
134  WORD_VAL pullUp; //Indicates if pull-ups are enabled for 16 ports
135  WORD_VAL latch; //Current setting of output latch for 16 ports
136  WORD_VAL polarity; //Input polarity for 16 ports
137  WORD_VAL pin; //Current pin value for 16 ports
138 } MCP23017;
139 
140 
151 INT8 mcp23017_Init(MCP23017* obj);
152 
163 INT8 mcp23017_ConfigDir(MCP23017* obj, UINT16 dir);
164 
165 
177 INT8 mcp23017_ConfigDirMask(MCP23017* obj, UINT16 dir, UINT16 mask);
178 
179 
190 INT8 mcp23017_ConfigPullups(MCP23017* obj, UINT16 pullUps);
191 
192 
205 INT8 mcp23017_ConfigIntPins(MCP23017* obj, UINT16 intPins);
206 
207 
222 INT8 mcp23017_WritePin(MCP23017* obj, UINT8 pinVal);
223 
224 
238 INT8 mcp23017_WriteMask(MCP23017* obj, UINT16 val, UINT16 mask);
239 
240 
251 #define mcp23017_WritePin(obj, pin) mcp23017_WritePin(obj, pin | 0x80)
252 
253 
264 #define mcp23017_ClearPin(obj, pin) mcp23017_WritePin(obj, pin & 0x7f)
265 
266 
277 INT8 mcp23017_ReadPins(MCP23017* obj);
278 
279 
289 #define mcp23017_ReadPins(obj) ((obj)->pin.Val)
290 
291 
296 //void mcp23017(UINT16 );
297 
298 #endif