View previous topic :: View next topic |
Author |
Message |
PICoHolic
Joined: 04 Jan 2005 Posts: 224
|
MCP23016 simple driver |
Posted: Thu Nov 30, 2006 3:54 am |
|
|
Code: |
#ifndef MCP23016
#define MCP23016
#define MCP_write 0B01000000
#define MCP_read 0B01000001
#define GP0 0x00
#define GP1 0x01
#define OLAT0 0x02
#define OLAT1 0x03
#define IPOL0 0x04 // INPUT POLARITY PORT REGISTER 0
#define IPOL1 0x05 // INPUT POLARITY PORT REGISTER 1
#define IODIR0 0x06 // I/O DIRECTION REGISTER 0
#define IODIR1 0x07 // I/O DIRECTION REGISTER 1
#define INTCAP0 0x08 // INTERRUPT CAPTURE REGISTER 0
#define INTCAP1 0x09 // INTERRUPT CAPTURE REGISTER 1
#define IOCON0 0x0A // I/O EXPANDER CONTROL REGISTER 0
#define IOCON1 0x0B // I/O EXPANDER CONTROL REGISTER 1
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
void InitMCP23016(int8 AddressSelect, int8 P0, int8 P1)
{
i2c_start();
delay_us(20);
i2c_write(MCP_write | (AddressSelect << 1));
delay_us(20);
i2c_write(IODIR0);
delay_us(20);
i2c_write(P0);
delay_us(20);
i2c_write(P1);
delay_us(20);
i2c_stop();
delay_us(20);
i2c_start();
delay_us(20);
i2c_write(MCP_write | (AddressSelect << 1));
delay_us(20);
i2c_write(IOCON0);
delay_us(20);
i2c_write(0x01);
delay_us(20);
i2c_stop();
delay_us(50);
}
//////////////////////////////////////////////////////////////////////////////
void GetGPx(int8 AddressSelect, int8 *P0, int8 *P1)
{
i2c_start();
delay_us(20);
i2c_write(MCP_write | (AddressSelect << 1));
delay_us(20);
i2c_write(GP0);
delay_us(20);
i2c_stop();
delay_us(50);
i2c_start();
delay_us(20);
i2c_write(MCP_read | (AddressSelect << 1));
delay_us(20);
*P0 = i2c_read();
delay_us(20);
*P1 = i2c_read(0);
delay_us(20);
i2c_stop();
delay_us(50);
}
//////////////////////////////////////////////////////////////////////////////
void SetGPx(int8 AddressSelect, int8 P0, int8 P1)
{
i2c_start();
delay_us(20);
i2c_write(MCP_write | (AddressSelect << 1));
delay_us(20);
i2c_write(OLAT0);
delay_us(20);
i2c_write(P0);
delay_us(20);
i2c_write(P1);
delay_us(20);
i2c_stop();
delay_us(50);
}
//////////////////////////////////////////////////////////////////////////////
void GetINTGPx(int8 AddressSelect, int8 *P0, int8 *P1)
{
i2c_start();
delay_us(20);
i2c_write(MCP_write | (AddressSelect << 1));
delay_us(20);
i2c_write(INTCAP0);
delay_us(20);
i2c_stop();
delay_us(50);
i2c_start();
delay_us(20);
i2c_write(MCP_read | (AddressSelect << 1));
delay_us(20);
*P0 = i2c_read();
delay_us(20);
*P1 = i2c_read(0);
delay_us(20);
i2c_stop();
delay_us(50);
}
//////////////////////////////////////////////////////////////////////////////
void SetGPREG(int8 AddressSelect, int8 REG, int8 Data)
{
i2c_start();
delay_us(20);
i2c_write(MCP_write | (AddressSelect << 1));
delay_us(20);
i2c_write(REG);
delay_us(20);
i2c_write(Data);
delay_us(20);
i2c_stop();
delay_us(50);
}
#endif
|
|
|
|
DonWare
Joined: 18 Jan 2006 Posts: 43
|
|
Posted: Wed Jan 24, 2007 10:43 am |
|
|
I borrowed some of this code for a hot job I'm working on.
Thanks very much !!!
I'm using a PIC16F877 and MCP23016. I was wondering how important the delays are in this code ?
The reason I ask is that I'm using this device to control 2 step motors and the delays slow down my maximum possible speed.
In retrospect it was probably a bad idea using the 23016 for this - but I'm stuck right now. Does anybody have any thoughts on this ?
Thanks in advance, Don |
|
|
PICoHolic
Joined: 04 Jan 2005 Posts: 224
|
|
Posted: Wed Jan 24, 2007 10:51 am |
|
|
At most you can reduce the delay_us(20) to delay_us(12)
and the 50 us to probably 25!
(Go back to the datasheet)
I dont know. Try to go down with the delay bit by bit
Good Luck
&
Keep me posted! |
|
|
BoydNielsen
Joined: 20 Aug 2006 Posts: 12
|
|
Posted: Fri Aug 15, 2008 6:15 am |
|
|
I have never used the MCP23016 I/O Expander and have a presssing project where this will help greatly. Your "MCP23016 simple driver" appears to be well written. However, I have a question: Going through the code, it looks like the driver only addresses Port 0 (GP0, OLAT0, IPOL0, IODIR0, INTCAP0, and IOCON0). Do I need to duplicate the functions for Port 1? Or, am I missing something?
Thanks,
Boyd |
|
|
PICoHolic
Joined: 04 Jan 2005 Posts: 224
|
|
Posted: Mon Aug 18, 2008 1:10 am |
|
|
No you dont have to duplicate anything...
It works perfect for both ports.
Regards |
|
|
|