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 support@ccsinfo.com

I2C - The real thing

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



Joined: 18 Apr 2013
Posts: 3

View user's profile Send private message

I2C - The real thing
PostPosted: Thu Apr 18, 2013 8:14 pm     Reply with quote

Hi,

This is the code I've been using for a while, for creating a fully functional I2C slave, where I have placed up to 8 slaves like this one with no issues.

Recently, a partner told me that there should be an easier way, where a missmatch of slave address doesn't drive the interrupt, which I told him it isn't. What do you think?

Code:

int8 iBuf[2] = {0,0};
int8 oBuf[4] = {0,0,0,0};


#int_SSP
void  SSP_isr(void)
{
   int8 state;
   state = i2c_isr_state();

   if (~BIT_TEST(SSPCON,SSPOV)){
      if(state== 0) {
          i2c_read();  //Address ignored
      }
      if(state >= 0x80){
         if (!BIT_TEST(SSPCON,STOP))  i2c_write(oBuf[state - 0x80]);
      }
      else {
         if(state > 0){
            iBuf[state - 1] = i2c_read(); 
            if (state == 2) validCompleteSequence = 1;
         }
      }
   }
   else{
      i2c_read();   //Flush
   }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19451

View user's profile Send private message

PostPosted: Fri Apr 19, 2013 3:50 am     Reply with quote

This is down to how the hardware responds so 'read the data sheet'. You don't say 'what chip', so taking a chip at random, the 4520, the data sheet entry says:

"If the addresses match and the BF
and SSPOV bits are clear, the following events occur:
1. The SSPSR register value is loaded into the
SSPBUF register.
2. The Buffer Full bit, BF, is set.
3. An ACK pulse is generated.
4. MSSP Interrupt Flag bit, SSPIF (PIR1<3>), is
set (interrupt is generated, if enabled) on the
falling edge of the ninth SCL pulse."

Note the 'If the addresses match'.

Otherwise the chip does nothing, unless you have general call address enabled. If this is enabled, any of the addresses 0x0 to 0x10, will also be accepted.

Best Wishes
mbalderas



Joined: 18 Apr 2013
Posts: 3

View user's profile Send private message

PostPosted: Fri Apr 19, 2013 2:18 pm     Reply with quote

Thanks for your support.

The device is a 16f690. The datasheet says that the interrupt would fire only if the address matched, but in practice it fires no matter if you are writing this device or another. My slave address is 0xAA.

How do I check for the general call address to be disabled.
Ttelmah



Joined: 11 Mar 2010
Posts: 19451

View user's profile Send private message

PostPosted: Fri Apr 19, 2013 3:05 pm     Reply with quote

You need to read up on the difference between the SSP, and the MSSP.

Best Wishes
mbalderas



Joined: 18 Apr 2013
Posts: 3

View user's profile Send private message

PostPosted: Sat Apr 20, 2013 1:08 pm     Reply with quote

Hi,

I actually have read the differences, and seems to me that SSP is enought for me, since I'm only writing the slave device. The master is a computer based controller, which takes care of speed of clock and sequencing.

My slaves are the issue. I mean, with the code that I posted earlier, I have had no problems, but I wish the devices didn't care whenever another address is being addressed.

[EDIT:]

Maybe... The reason of the interrupt is that the master generates a NACK if no one responds to the address being called, is my guess correct?
Ttelmah



Joined: 11 Mar 2010
Posts: 19451

View user's profile Send private message

PostPosted: Sat Apr 20, 2013 2:55 pm     Reply with quote

The SSP, has a _firmware only_ implementation. It interrupts on every byte, requiring the code to check the flags to see what has happened.
The MSSP, implements interrupt only on address match.

Best Wishes
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