Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

I2C master slave programming

Status
Not open for further replies.

DatsAbk

Member level 3
Joined
Jun 22, 2012
Messages
55
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,704
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:
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();
		
	}
}
 

I haven't checked every line of code, but in the slave, you shouldn't make the SDA line an output. SDA is bi-directional, and when "high" it should be an input (= idle state as well). I would recommend looking at the lines with an oscilloscope to see what is actually getting sent.

Also check out some generic troubleshooting instructions for I2C.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top