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 CCS Technical Support

IO-Expander MCP23017 I2C read problem

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



Joined: 06 Dec 2005
Posts: 59

View user's profile Send private message

IO-Expander MCP23017 I2C read problem
PostPosted: Sun Jun 24, 2007 12:23 pm     Reply with quote

Compiler PCWH v. 4.014
PIC18F2520
MCP23017 (Microchip), Datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf

Dear,

I have a problem in reading via I2C from the IO-Expander MCP23017. I have the following #use and #fuses commands in the 18F2520.

Code:

#use I2C(MASTER, SDA=PIN_C4, SCL=PIN_C3, RESTART_WDT, SLOW, FORCE_HW)

#fuses HS,WDT256,PUT,PROTECT,NOMCLR,CPD,BROWNOUT,NOIESO,NOFCMEN,NODEBUG,INTRC_IO,NOLVP


the read function is as follows:

Code:

int8 i2creadbyte(int8 reg)
{
   int8 num;

   i2c_start();
   i2c_write(0x40);     // address MCP23017 and set to write operation
   i2c_write(reg);       // write number of register to read
   i2c_start();            // restart
   i2c_write(0x41);    // address MCP23017 and set to read operation
   num = i2c_read(); // read register
   i2c_stop();
   return(num);

}


The program is hanging while running the read function and there is no more clock and data signal on the I2C bus (Scope) and I have no idea why.

The following write function is working without problems:

Code:
void i2cwritebyte(int8 reg, int8 data)
{
   i2c_start();
   i2c_write(0x40);
   i2c_write(reg);
   i2c_write(data);
   i2c_stop();
}


Are there any ideas why there are problems with the read?

Many thanks for helping.

Best Regards
Nilsener
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 24, 2007 12:58 pm     Reply with quote

Quote:
The program is hanging while running the read function.

On i2c devices, you have to do a "NAK" on the last read.
If there is only one read operation, it's the "last" one.
The default operation of the i2c_read() function to do an "ACK".
It will do that if no parameter is present. To force it to do
a NAK, you need to give it a zero parameter. Example:
Code:
num = i2c_read(0);
nilsener



Joined: 06 Dec 2005
Posts: 59

View user's profile Send private message

PostPosted: Mon Jun 25, 2007 1:56 am     Reply with quote

Dear PCM programmer,

thank you very much, the read function is working now by implementing the NAK. You saved me from desperation.

Now I have an other problem. If I induce some disturbances on the I2C clock line using the probe from the scope (disturbance comes from the little capacitors in the probe), the communication is hanging again after a while. In real life this disturbance may come from a burst or a surge e.g. on the main power lines or something like that. However, an error in I2C communication must not result in hanging. Is there any error flag (18F2520) that can be polled? Then I can reset the MCP23017 by Microcontroller and/or induce a reset of the microcontroller. It can be done by an ISR of a timer because the ISRs are still running during hanging.

Any other ideas to avoid I2C hanging?

Thank you very much for helping

Best Regards
Nilsener
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 25, 2007 11:49 am     Reply with quote

There are some articles in the forum on this.
http://www.ccsinfo.com/forum/viewtopic.php?t=30318
http://www.ccsinfo.com/forum/viewtopic.php?t=30297
http://www.ccsinfo.com/forum/viewtopic.php?t=30153
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