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/.
//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
Posted: Sun Mar 18, 2007 1:00 pm
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.
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