View previous topic :: View next topic |
Author |
Message |
Michael88
Joined: 31 Oct 2003 Posts: 6
|
about pic i2c |
Posted: Fri Oct 31, 2003 12:54 pm |
|
|
Hi all,
I am newbie and have a question about pic i2c. I am using 16f876 as master and slave processor now. My ccsc is PCM 3.072. My code of slave are as follow:
code 1:
#INT_SSP
void ssp_interupt()
{
char incoming;
incoming=SSPBUF;
SSPIF=0;
CKP=1;
}
code 2:
#INT_SSP
void ssp_interupt()
{
char incoming;
incoming=i2c_read();
}
My question is:
If I use code 1 then I can get data correctly. if i use code 2 then nothing I can get from the master. Why?
Thanks.
Michael |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Fri Oct 31, 2003 1:14 pm |
|
|
I don't think that i2c_read(); was designed to work with an interupt. The same could be said about read_adc(); and read_SPI(); |
|
|
Michael88
Joined: 31 Oct 2003 Posts: 6
|
|
Posted: Fri Oct 31, 2003 2:12 pm |
|
|
Hi Neutone,
Thanks for your response. Is this mean if I put i2c_read() not in interrupt routine then it is ok? I look at ccsc manual and it said #int_ssp can used in spi and i2c. why?
Thanks.
Michael |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Oct 31, 2003 2:43 pm |
|
|
Note that an interrupt is generated on the Start and Stop conditions. You should call the i2c_poll() function to determine if a byte is present before using the i2c_read(). Even so, I think using the registers is a better approach since you know what is happening. The i2c_read() function from CCS hides the code so that you really don't know what is going on unless you look at the lst file. It is also subject to change without notice when they rev the compiler which could make perfectly working code no longer function. |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Fri Oct 31, 2003 2:54 pm |
|
|
If I understand correctly when you call i2c_read() it will clear the buffer and wait for a byte to arrive. If you are using this from an interupt you will clear the buffer you want to read and then get the following byte. Using interupts is a more advanced feature that the compiler supports for custom functions. Sometimes the builtin features of the compiler don't work well with interupts. I reccomend that you compile your code using both methods and then look at the list file and see how the two methods are different. This will let you see past the syntax at what is really happening. The list file name will be the same as the hex file with the extention of .LST |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Oct 31, 2003 3:09 pm |
|
|
In Slave Mode:
If there is a byte in the buffer, it will return that byte. If no byte is present, then it will wait for one.
In Master Mode:
It will read a byte from a Slave device supplying the clock signal. The parameter can be used to ACK or NACK the byte to signal the end of a read sequence. |
|
|
Michael88
Joined: 31 Oct 2003 Posts: 6
|
|
Posted: Fri Oct 31, 2003 4:00 pm |
|
|
to Mark: actually i called i2c_poll() before, it is the same result.
to Neutone: as you said, if the master sends 2 bytes to slave, the slave should get the second byte, right? but nothing. as your recommendation, i will compare the LST file of both code and see what is the difference.
anyways, thanks you guys again. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sat Nov 01, 2003 12:15 am |
|
|
I am not sure if you used the i2c_poll() properly. You should actually test the result
Code: |
if (i2c_poll())
data = i2c_read();
|
Regards,
Mark |
|
|
benri
Joined: 08 Sep 2003 Posts: 2
|
|
Posted: Wed Dec 17, 2003 2:36 am |
|
|
hi,
Did you define the i2c interrupt?
#use i2c(slave, sda=pin_c4, sdl=pin_p3, address=0x90) |
|
|
|