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

about pic i2c

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



Joined: 31 Oct 2003
Posts: 6

View user's profile Send private message

about pic i2c
PostPosted: Fri Oct 31, 2003 12:54 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Oct 31, 2003 1:14 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Oct 31, 2003 2:12 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Fri Oct 31, 2003 2:43 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Oct 31, 2003 2:54 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Fri Oct 31, 2003 3:09 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Oct 31, 2003 4:00 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Sat Nov 01, 2003 12:15 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Dec 17, 2003 2:36 am     Reply with quote

hi,

Did you define the i2c interrupt?

#use i2c(slave, sda=pin_c4, sdl=pin_p3, address=0x90)
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