Hardware Reference
In-Depth Information
187 puts("Monitoring for MCP23017 input changes :\n");
188 post_outputs(); /
Copy inputs to outputs
/
∗
∗
189
190 do {
191 gpio_poll(fd); /
Pause until an interrupt
/
∗
∗
192
193 int_flags = mcp23017_interrupts();
194 if ( int_flags ) {
195 v = mcp23017_captured();
196 printf(" Input change: flags %04X values
%04X\n",
197 int_flags,v);
198 post_outputs();
199 }
200 } while ( !is_signaled ); /
Quit if ^C' d
/
∗
∗
201
202 fputc('\n', stdout);
203
204 i2c_close(); /
Close I2C driver
/
∗
∗
205 close(fd); /
Close gpio17/value
/
∗
∗
206 gpio_close(gpio_inta); /
Unexport gpio17
/
∗
∗
207 return 0;
208 }
209
210 /
End mcp23017.c
/
∗
∗
Module i2c_funcs.c
To compile code when making use of I2C, you will need to install the
libi2c
development library:
$ sudo apt-get install libi2c-dev
The
i2c_funcs.c
is a small module that wraps the
ioctl(2)
calls into neat little I/O
functions:
i2c_write8()
: Writes 8-bit value to MCP23017 register
i2c_write16()
: Writes 16-bit value to MCP23017 register
i2c_read8()
: Reads 8-bit value from MCP23017 register
i2c_read16()
: Reads 16-bit value from MCP23017 register
Additionally, the open and close routines are provided:
i2c_init()
: Opens the bus driver for
/dev/i2c-x
i2c_close()
: Closes the opened I2C driver