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

MCP23008

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
aaronik19



Joined: 25 Apr 2011
Posts: 296

View user's profile Send private message

MCP23008
PostPosted: Mon Oct 10, 2011 3:12 pm     Reply with quote

Dear All,

I am using about 4 IO Expander on one single I2C bus. The configuration is below:

Code:

// MCP008 REGISTERS
#define _IODIR       0x00
#define _IPOL        0x01
#define _GPINTEN    0x02
#define _DEFVAL    0x03
#define _INTCON    0x04
#define _IOCON       0x05
#define _GPPU      0x06
#define _INTF      0x07
#define _INTCAP      0x08
#define _GPIO      0x09
#define _OLAT      0x0A

void init_MCP23008() {
   output_float(PIN_B1);
   output_float(PIN_B0);
   delay_ms(250);
}

void write_MCP23008(unsigned char WriteAddress, unsigned char cmdByte, unsigned char data)
{

   i2c_start();                   // start condition
   delay_us(20);
   
   i2c_write(WriteAddress);    // Send slave address and clear (R/W_)
   delay_us(20);

   i2c_write(cmdByte);       // Command byte and register to be written.
   delay_us(20);

   i2c_write(data);          // First data byte pair as per command byte(cmd)
   delay_us(20);

   i2c_stop();             // stop condition   
   delay_us(50);
}


long int read_MCP23008(unsigned int WriteAddress, unsigned int cmdByte, unsigned int ReadAddress)
{
   unsigned int data;

   i2c_start();             // start condition
   delay_us(20);
   
   i2c_write(WriteAddress);    // Send slave address and clear (R/W_)
   delay_us(20);
   
   i2c_write(cmdByte);       // Command byte and register to be written.
   delay_us(50);
   
   i2c_start();
   delay_us(20);             // restart condition
   
   i2c_write(ReadAddress);      // Send slave address and clear (R_/W)
   delay_us(20);
   
   data = i2c_read(0);       // Data from LSB or MSB of register
   delay_us(20);
   
   i2c_stop();             // stop condition

   return(data);
}


and the main program is this:

Code:

#include "i2c.c"

#define MCP23008_write_add 0x40
#define MCP23008_read_add 0x41


void main()
{
   write_MCP23008(0x01, _IODIR, 0x00);
   write_MCP23008(0x02, _IODIR, 0xFF);
   write_MCP23008(MCP23008_write_add, _IPOL, 0x00);
   write_MCP23008(MCP23008_write_add, _IOCON, 0x00);
   init_mcp23008();

   while(1)
{

write_MCP23008(0x01,0x09, 0x01);
delay_ms(3000);
write_MCP23008(0x02,0x09, 0x02);
delay_ms(3000);
write_MCP23008(0x03,0x09, 0x03);
delay_ms(3000);
write_MCP23008(0x04,0x09, 0x04);

}

}


now I have the 4 IO Expander from address 0x01 to 0x04. When I am reading I am getting always 0xff. I am making something wrong? Thanks for your help to solve this issue. I am using PIC18F4550 with 20MHz crystal.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 10, 2011 4:26 pm     Reply with quote

Quote:
#define MCP23008_write_add 0x40
#define MCP23008_read_add 0x41

now I have the 4 IO Expander from address 0x01 to 0x04

The addresses of 0x40 and 0x41 are for your first MCP23008.

The sub-address of each separate MCP23008 chip is contained in the
control byte. It's defined by 3 bits (A0, A1, A2).
Download the MCP23008 data sheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/21919d.pdf
Look at Figure 1-2 at the top right of page 8. You can see how if
the chip had a sub-address of 0 (A0, A1, A2 all connected to ground)
then the chip would have an i2c address of 0x40 (and 0x41 for reading).

So, you need to #define the control bytes for the other 3 MCP23008
chips. Look at the bit diagram and decide how to do it. Then write
the new #define statements. Then use those in your code to talk
to the specified chip.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion 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