|
|
View previous topic :: View next topic |
Author |
Message |
Sejo
Joined: 09 Nov 2006 Posts: 2
|
I2C_STOP Problem |
Posted: Thu Nov 09, 2006 1:27 pm |
|
|
Hi,
I would like to comunicate 2 PICs using I2C, I used the following code.
As you can see in the code, I try to read a number that send the slave when the master ask for the data. if the number that you sent is >=128(10000000), the program works, NOT PROBLEM AT ALL. But if you sent a number is <=127 it does not work. I have a logic analizer to check the bus, if i send 128 i see in the bus the start, address+read-bit, ACK,data,ack, stop. But if the number if i send <=127(01111111) i just can see start,address+read-bit, ACK,data,ack) I lost the stop condition.
I think:
1) Due to that is the master the one that have to generate the stop condition, it looks like if the slave keep the SDA pin LOW, and the master is no able to set the SDA to high
2)if the number that i sent in binary mode start with a "1" it works, but if start with a "0" it does not work
3)if after the read comand in the master code you send any data to the bus for example "i2c_write(0Xb1);" it works, you can see the stop condition
4) it looks like if the slave keep the control of the SDA line
5) The real problem it hapen when the data that i have read form the Slave have to be sent to a LCD display using the I2C bus, due to that we do not have a stop condition i can not use the bus, to use it i have to reset the slave pic
ANY IDEAS????
MASTER:
#include <16F876.h>
#use delay(clock=4000000)
#use i2c(master,sda=PIN_C4,scl=PIN_C3)
#fuses NOPROTECT,NOCPD,NOLVP,NOWDT,XT
void main(void)
{
int16 datos;
i2c_start();
i2c_write(0Xa1);
datos=i2c_read();
i2c_stop();
while(1)
{
}
}
SLAVE:
#include <16F876.h>
#use delay(clock=4000000)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0xa0,FORCE_HW)
#fuses NOPROTECT,NOCPD,NOLVP,NOWDT,XT
#INT_SSP
void ssp_interupt ()
{
i2c_write(127);
}
void main()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
while(1)
{
}
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 09, 2006 1:40 pm |
|
|
When you read from an i2c chip, you need to do a NAK on the last read
operation. This is done by calling i2c_read with the 0 parameter.
Code: | data = i2c_read(0); |
I suggest that you start by using the CCS example file, Ex_Slave.c,
instead of inventing your own method. Here is thread with more
information on this:
http://www.ccsinfo.com/forum/viewtopic.php?t=28473&start=1 |
|
|
Sejo
Joined: 09 Nov 2006 Posts: 2
|
It Works |
Posted: Fri Nov 10, 2006 4:25 am |
|
|
THANKS IT WORKS you are right, I forgot the NAK.
But why if I do not use "data = i2c_read(0);" and I use the "data = i2c_read();" if a send a 128 it works and if I send 127 it does not work, Why? |
|
|
agrj
Joined: 26 Sep 2003 Posts: 48
|
|
Posted: Mon Nov 13, 2006 12:09 pm |
|
|
Now I'm lost.
let me understand. I have the following routine in my master:
{
int q;
I2C_start( );
I2C_write( endereco_uC_le );
for(q = 0; q <= tamanho; q++)
{
I2C_buffer_RX[ q ] = I2C_read( );
}
I2C_stop( );
}
and, I think, some times I have some problems with I2C comm.
should I change it for:
{
int q;
I2C_start( );
I2C_write( endereco_uC_le );
for(q = 0; q <= tamanho-1; q++)
{
I2C_buffer_RX[ q ] = I2C_read( );
}
I2C_buffer_RX[ tamanho ] = I2C_read(0);
I2C_stop( );
}
thanks all.
Dinho |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Nov 13, 2006 12:18 pm |
|
|
Yes, you must do a NAK on the last i2c read operation.
Look in the the CCS driver file directory. There are at least 25 drivers
that show you must do a NAK on the last read. I ran a text search on
that directory. Here are just a few of the hits:
Code: | c:\program files\picc\drivers\2402.c
data=i2c_read(0);
c:\program files\picc\drivers\2404.c
data=i2c_read(0);
c:\program files\picc\drivers\24128.c
data=i2c_read(0); |
|
|
|
agrj
Joined: 26 Sep 2003 Posts: 48
|
|
Posted: Mon Nov 13, 2006 12:21 pm |
|
|
Thank you, I will test ir right now.
it could solve a lot of problems!!!! I do have several 0xFF answers during the run time.
could it be the problem? (I hope so)
thanks again
Dinho |
|
|
agrj
Joined: 26 Sep 2003 Posts: 48
|
|
Posted: Mon Nov 13, 2006 1:45 pm |
|
|
just an update.
perfect!!!! no more 0xFF.
that's why everyone should read the manual several times!!!!!
thanks
Dinho |
|
|
|
|
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
|