CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

MCP23016 simple driver

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
PICoHolic



Joined: 04 Jan 2005
Posts: 224

View user's profile Send private message

MCP23016 simple driver
PostPosted: Thu Nov 30, 2006 3:54 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jan 24, 2007 10:43 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jan 24, 2007 10:51 am     Reply with quote

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 Laughing

Good Luck
&
Keep me posted!
BoydNielsen



Joined: 20 Aug 2006
Posts: 12

View user's profile Send private message

PostPosted: Fri Aug 15, 2008 6:15 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Aug 18, 2008 1:10 am     Reply with quote

No you dont have to duplicate anything...
It works perfect for both ports.

Regards
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group