DatsAbk
Member level 3
Hello,Here is my I2C master slave code for PIC18f4550.
The problem with the code is not understandable by me.I checked the data on CRO.The master is sending the data constantly.But the problem is that the slave isn't raising interrupt at all.
Is there some mistake in addressing or some other mistake in coding?
I need a C-language code for the same if anyone has successfully implemented it.....
Master Code:
Slave Code:
The problem with the code is not understandable by me.I checked the data on CRO.The master is sending the data constantly.But the problem is that the slave isn't raising interrupt at all.
Is there some mistake in addressing or some other mistake in coding?
I need a C-language code for the same if anyone has successfully implemented it.....
Master Code:
Code:
#include <p18f4550.h>
#include <i2c.h>
#define I2C_V3
void send(unsigned char data)
{
while(!WriteI2C(data));
}
void main()
{
unsigned char x[]="Hello",i;
ADCON1=0X0F;
OpenI2C(MASTER,SLEW_OFF);
SSPADD=0x24;
IdleI2C();
StartI2C();
while(!WriteI2C(0x10));
for(i=0;i<6;i++)
{ send(x[i]); }
StopI2C();
while(1);
}
Slave Code:
Code:
#include <p18f4550.h>
#include <i2c.h>
#define I2C_V3
static unsigned char data[6],i=0;
void readbuffer(void);
void trx()
{
unsigned char x;
for (x=0;x<6;x++)
{
while(PIR1bits.TXIF==0); //check if transmit register is empty
TXREG=data[x]; //enter the data in transmit register after checking its empty
}
while(1);
}
void chk_isr(void)
{
PIR1bits.SSPIF=0;
if(SSPSTATbits.S==1)
{
readbuffer();
}
else if (SSPSTATbits.P==1)
{
_asm
GOTO trx
_endasm
}
}
#pragma code My_HighPrio_Int=0x08
void My_HighPrio_Int(void)
{
_asm
GOTO chk_isr
_endasm
}
void main()
{
RCONbits.IPEN=1;
INTCONbits.GIE=1;
PIE1bits.SSPIE=1;
SetPriorityIntI2C(1);
TXSTA=0x20; //low bud rate 8-bit transmission
SPBRG=0x19; //baud rate=9600 for 16MHz
TRISCbits.RC6=0; //make Tx as output
TXSTAbits.TXEN=1; //enable transmission and reception
RCSTAbits.SPEN=1;
SSPADD=0x08;
SSPCON2bits.SEN=1;
OpenI2C(SLAVE_7_STSP_INT,SLEW_OFF);
while(1);
}
void readbuffer(void)
{
while(!SSPSTATbits.P)
{
while(SSPSTATbits.BF==0);
data[i]=SSPBUF;
SSPCON1bits.CKP=1;
i++;
AckI2C();
}
}