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 slave problem

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



Joined: 11 Mar 2010
Posts: 19326

View user's profile Send private message

PostPosted: Mon Jun 13, 2011 2:44 pm     Reply with quote

Yes, it is just a nomenclature thing.

I2C always sends 'bytes', and the address is a byte with the top seven bits being the 'I2C address', and the bottom bit the direction flag. The PIC always deals with the 'address' in this format pre-justified to 8bits. Most chip manufacturers also now use the format this way, stating the device address(s) as the byte that needs to be sent to select the device, rather than the seven bit
bus address number, which then needs to be left shifted.

It's rather 'historical'. The original I2C spec dealt with the 7bit address, but usage, has tended to move to using the 8bit format.

Best Wishes
kongfu1



Joined: 26 Apr 2010
Posts: 56

View user's profile Send private message

I2C Master can't read
PostPosted: Mon Jun 20, 2011 8:23 pm     Reply with quote

Hi,

Thanks for everyone's help. I2C has no problem as slave now.
But as master of I2C, it is still not working yet.
In attached code, PIC sends a slave address and asking slave to return one byte.
If slave is not connected, PIC will repeat same action and toggle PIN_B7 every 100 ms.
If salve is connected and sends back one byte, PIC will be stopped. (PIN_B7 will be a flat line all the time).

Anyway, PIC is OK to send data to slave as I2C master.

Please help.
Thanks.
Howard
Code:

#include <16F687.h>

#fuses NOWDT, BROWNOUT, NOPUT, MCLR,NOCPD,INTRC,IESO,FCMEN
#use delay(clock=4M)
//#use i2c(SLAVE, SDA=PIN_B4, SCL=PIN_B6, address=0x80,FORCE_HW)
#use i2c(MASTER, SDA=PIN_B4, SCL=PIN_B6, address=0x2f,fast=100000)  //,SMBUS)  //,FORCE_HW)

void main (void)
{
   output_high(PIN_C4); // to turn pwr on for testing board
   output_low(PIN_C1);  // to turn pwr on for testing board
   output_high(PIN_C3); // to turn pwr on for testing board

   while (TRUE)
   {
      //hello for ack
      i2c_start();
      i2c_write(0x20);
      i2c_stop();

      //read
      i2c_start();
      i2c_write(0x21);
      i2c_read(1);
      i2c_stop();     // Stop condition
     
       
      delay_ms(100);
      output_toggle(PIN_B7);
   }
}
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