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

has anyone used the new mcp23008 port expander?

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



Joined: 02 Feb 2004
Posts: 17

View user's profile Send private message

has anyone used the new mcp23008 port expander?
PostPosted: Fri Apr 22, 2005 5:56 pm     Reply with quote

I cannot get this chip to respond. I am using a pic18f2445 and the newest ccs. It never does anything, but if i put a similiar chip (pcf8547) on the i2c bus with a different address, the new chip works fine. Also if i read any port on the chip, i get back a value of 0xFF, which should not be the case.

So I think my problems are not i2c bus related, maybe bad software?
Here is the code I am using:

mcp23008.c:
Code:

#define MCP23008_I2C_WRITE 0x40
#define MCP23008_I2C_READ 0x41

// 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 write_MCP(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_MCP(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);
   
   ldata = i2c_read();       // Data from LSB or MSB of register
   delay_us(20);
   
   i2c_stop();             // stop condition

   return(data);
}

// ----------------------------------------------------------------------------
void init_mcp()
{
   output_float(PIN_B0);
   output_float(PIN_B1);

   // Wait for MCP23016 Expander Device to power-up.
   delay_ms(250);

   // Set-up selected I/O expander unit
   write_MCP(MCP23008_I2C_WRITE, _IPOL, 0x00); // NonInvert all input polarities if active low
   write_MCP(MCP23008_I2C_WRITE, _IOCON, 0x00); // IARES(1) to max. activity detection time.
   write_MCP(MCP23008_I2C_WRITE, _IODIR, 0x00); // Direction of all data is output.
//   write_MCP(MCP23008_I2C_WRITE, _OLAT, 0xFF); // Update o/p latch that controls the output.
//   write_MCP(MCP23008_I2C_WRITE, _GPPU, 0xFF);  // Enable pullups
}


And here is my test code:
Code:

#include <18f2455.h>

#FUSES HS,MCLR,PUT,NOWDT,NOBROWNOUT,NODEBUG,NOIESO,NOLVP,NOWRT,NOWRTD,NOFCMEN
#use delay(clock=16000000)
#use i2c(MASTER, SDA=PIN_B0, SCL=PIN_B1, FAST, FORCE_HW)
// #use i2c(MASTER, SDA=PIN_B0, SCL=PIN_B1)

#include "mcp23008.c"

void main(){
   
   init_mcp();

   while(1){
      write_mcp(0x40, _OLAT, 0xF0);
   }
   
}


If anyone has any ideas, i would be greatful for some advice.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 22, 2005 8:09 pm     Reply with quote

I didn't check your entire program, but I scanned it and spotted one
error. Remember that in i2c, you always have to do a "NAK" on the
last read. With CCS, you do this by giving a 0 parameter to the
i2c_read() function. You need to change your code, as shown in
bold below.

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);

ldata = i2c_read(0); // Do a NAK on the last i2c read

delay_us(20);

i2c_stop(); // stop condition

return(data);
}
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