|
|
View previous topic :: View next topic |
Author |
Message |
nilsener
Joined: 06 Dec 2005 Posts: 59
|
IO-Expander MCP23017 I2C read problem |
Posted: Sun Jun 24, 2007 12:23 pm |
|
|
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
|
|
Posted: Sun Jun 24, 2007 12:58 pm |
|
|
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:
|
|
|
nilsener
Joined: 06 Dec 2005 Posts: 59
|
|
Posted: Mon Jun 25, 2007 1:56 am |
|
|
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
|
|
|
|
|
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
|