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 doesn't work

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



Joined: 20 Dec 2005
Posts: 16

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

I2c doesn't work
PostPosted: Sun Mar 18, 2007 10:35 am     Reply with quote

I tried i2c communication between 18F2420 / Master/ and 18F4420 / Slave/. It doesn't work. I used ex_slave.c code for slave and standard master code. When I used on slave side 16F877A, communication worked properly.

Tried anyone i2c with these new procesors? /18F4420, 18F2420/.

Master code:

Code:
...
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_start();
i2c_write(0xA1);
data = i2c_read(0);
i2c_stop();
...


Slave code:

1. ex_slave.c

2. I tried slave code from Ttelmah.
It doesn't work too.

Code:

#ifdef __PCM___
 #byte SSPCON=0x14
 #byte SSPSTAT=0x94
 #byte SSPBUF=0x13
#else
 #byte SSPCON=0xFC6
 #byte SSPSTAT=0xFC7
 #bit SSPBUF=0xFC9
#endif
#bit P=SSPSTAT.4
#bit DA=SSPSTAT.5
#bit S=SSPSTAT.3
#bit RW=SSPSTAT.2
#bit BF=SSPSTAT.0
#bit WCOL=SSPCON.7
#bit SSPOV=SSPCON.6
#bit SSPEN=SSPCON.5

//My I2C buffer
#define BSIZE 32;
//Note buffer size must be binary multiple
int8 buffer[BSIZE];

#define read_i2c() (SSPBUF)
#define write_i2c(x) SSPBUF=(x)
#define MWLBA ((S==1)&&(RW==0)&&(DA==0)&&(BF==1))
#define MWLBD ((S==1)&&(RW==0)&&(DA==1)&&(BF==1))
#define MRLBA ((S==1)&&(RW==1)&&(DA==0)&&(BF==0))
#define MRLBD ((S==1)&&(RW==1)&&(DA==1)&&(BF==0))
#define MNACK ((S==1)&&(RW==0)&&(DA==1)&&(BF==0))
//Note that for the 18 chips, you could add GCEN as another bit to test

#int_SSP
void SSP_int_handler(void) {
 static int8 ctr;
 int8 tmp;
 if(MWLBA) {
  //Here I have an address match for a write operation
  ctr=0;
  tmp=read_i2c();
 }
 else if(MWLBD) {
  //Here data arriving from master
  buffer[ctr++]=read_i2c();
  ctr &= (BSIZE-1);
  //Limit buffer counter to buffer size
 }
 else if(MRLBA) {
  //Here an address match for a read command
  ctr=0;
  write_i2c(buffer[ctr++]);
 }
 else if(MRLBD) {
  //Here sending data to master
  write_i2c(buffer[ctr++]);
  ctr &= (BSIZE-1);
 }
 else if(MNACK) {
  //Here NACK from master
  SSPEN=0;
  SSPEN=1;
  //Reset I2C module
 }
 if (SSPOV) SSPOV=0;
 if (WCOL) WCOL=0;
}

[/code]
Ttelmah
Guest







PostPosted: Sun Mar 18, 2007 1:00 pm     Reply with quote

Look at initialisation.
On the latter chips, the I2C hardware, is often multiplexed with other peripherals, and/or, may require specific fuses to set which bits are used. This initialisation needs to be right if the peripheral is going to work.

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