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 communication using pic18f46j11

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 the code for master communication-
Code:
#include <p18f46J11.h>
#include <i2c.h>
#include <delays.h>
#define I2C_V5
void send(unsigned char data)
{
	while(!WriteI2C(data));
		
}
void main()
{
	unsigned char x[]="Hello",i;
	EnableIntI2C1;
	SetPriorityIntI2C1(1);
	OpenI2C(MASTER,SLEW_OFF);
	IdleI2C();
	StartI2C();
	while(!WriteI2C(0x10));
	for(i=0;i<5;i++)
	send(x[i]);
	while(1);	
}

Here is the slave code-
Code:
#include <p18f46j11.h>
#include <i2c.h>
#define I2C_V5
static unsigned char data[5],i=0;
void readbuffer(void);

void trx()
{
	unsigned char x;
	for (x=0;x<5;x++)
	{
	while(PIR1bits.TX1IF=1)	//check if the transmit register is empty or not
	TXREG1=data[x];
	}
	

}
void chk_isr(void)
{
	if(PIR1bits.SSP1IF==1)
	{
		if(SSP1STATbits.S==1)
		{
		PORTD=~PORTD;
		readbuffer();
		}
		else if (SSP1STATbits.P==1)
		{
		_asm
		GOTO trx
		_endasm
		}
	}
}

#pragma code My_HiPrio_Int=0x08
void My_HiPrio_Int(void)
{
_asm
GOTO chk_isr
_endasm
}
#pragma code

void main()
{
	TRISD=0;
	PORTD=0X55;
	EnableIntI2C1;
	SetPriorityIntI2C1(1);
	TXSTA1=0x20;				//enable low speed 8 bit transmission
	SPBRG1=0x15;				//set baud rate=19200
	BAUDCON1=0X00;
	SSP1ADD=0x10;
	
	OpenI2C(SLAVE_7_STSP_INT,SLEW_OFF);
	while(1);	
}
void readbuffer(void)
{
	data[i]=ReadI2C();
	i++;
}

There are 2 possibilities of error:
either my rs232 display code has an error or the error is caused as I haven't connected pullup resistors along the serial bus lines.

Kindly guide me on solving the problem.
My aim is to send data from master to slave and on receiving a stop bit the slave will communicate the data to rs232 port on my pc
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top