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.

[PIC] Two PIC 18f4620 1 as master.2nd is slave I2C problem

Status
Not open for further replies.

ubuntuman

Newbie level 4
Joined
Nov 15, 2017
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
74
Two PIC 18f4620 , 1 as master.2nd is slave I2C problem

The complete Master code . HS oscillator mode .4MHZ
Code:
const long int crystalOscillator =4000000;
unsigned short int temp=0;
//#####################################################################################
void I2C_Master_Init(const unsigned long int c)
{
 SSPCON1 = 0b00101000;
 SSPCON2 = 0;
 SSPADD = (crystalOscillator/(4*c))-1;
 SSPSTAT = 0b11000000;
 TRISC.B3 = 1; //Setting as input as given in datasheet
 TRISC.B4 = 1; //Setting as input as given in datasheet
}

void I2C_Master_Wait()
{
 while ((SSPSTAT & 0x04) || (SSPCON2 & 0x1F));
}

void I2C_Master_Start()
{
 I2C_Master_Wait();
 SSPCON2.SEN = 1;
}

void I2C_Master_RepeatedStart()
{
 I2C_Master_Wait();
 SSPCON2.RSEN = 1;
}

void I2C_Master_Stop()
{
 I2C_Master_Wait();
 SSPCON2.PEN = 1;
}

void I2C_Master_Write(unsigned short int d)
{
 I2C_Master_Wait();
 SSPBUF = d;
}

unsigned short int I2C_Master_Read(unsigned short int a)
{
 unsigned short int temp;
 I2C_Master_Wait();
 SSPCON2.RCEN = 1;
 I2C_Master_Wait();
 temp = SSPBUF;
 I2C_Master_Wait();
 if(a==1){SSPCON2.ACKDT=1;}
 else{SSPCON2.ACKDT=0;}
 SSPCON2.ACKEN = 1;
 return temp;
}

void main()
{
 I2C_Master_Init(100000); //Initialize I2C Master with 100KHz clock
 //I2C1_Init(50000);
 OSCTUNE.PLLEN=0;
 Delay_ms(500);
 TRISB=0;
 PORTB=0;
 while(1)
 {

 I2C_Master_Start();
 I2C_Master_Write(0xAA); //Write data
 //I2C_Master_Write(0x00);
 I2C_Master_Stop();
 
 Delay_ms(5000);
 }
}

The complete Slave code . HS oscillator mode .4MHZ
Code:
//##############################################################################
unsigned short int  zed=0;
volatile unsigned short int  new =0 ,dataRead=0 , dataWrite=0;
unsigned char dataReadCh[7] , dataWriteCh[7];
//##############################################################################
void interrupt()
{
 if(PIR1.SSPIF == 1)
 {
  SSPCON1.CKP = 0;
  new=1;
  zed = SSPBUF;
  PIR1.SSPIF = 0;
  SSPCON1.CKP = 1;
  if ((SSPCON1.SSPOV) || (SSPCON1.WCOL))
  {
   zed = SSPBUF; // Read the previous value to clear the buffer
   SSPCON1.SSPOV = 0; // Clear the overflow flag
   SSPCON1.WCOL = 0; // Clear the collision bit
   SSPCON1.CKP = 1;
  }
  if(!SSPSTAT.D_A && !SSPSTAT.R_W)
  {
   while(!SSPSTAT.BF){};
   while(SSPSTAT.BF){dataRead = SSPBUF;}
   SSPCON1.CKP = 1;
  }
  else if(SSPSTAT.D_A && SSPSTAT.R_W)
  {
   while(SSPSTAT.BF){zed = SSPBUF;}
   SSPBUF = dataWrite ;
   SSPCON1.CKP = 1;
   while(SSPSTAT.BF){};
  }
  PIR1.SSPIF = 0;
 }
}
//##############################################################################
void I2C_Slave_Init(unsigned short int address)
{
 SSPSTAT = 0b11000000;
 SSPADD = address;
 SSPCON1 = 0b00110110;
 SSPCON2 = 0b00000001;
 //TRISC.B3 = 1; //Setting as input as given in datasheet
 //TRISC.B4 = 1; //Setting as input as given in datasheet
 TRISC=0b00011000;
 LATC= 0b00011000;
 INTCON = 0b00000000;
 INTCON.PEIE = 1;
 INTCON.GIE = 1;
 PIE1.SSPIE = 1;
 PIR1.SSPIF = 0;
 while(SSPSTAT.BF){zed = SSPBUF;SSPCON1.SSPOV = 0;SSPCON1.WCOL = 0;}
}
//##############################################################################

void main() 
{
  OSCTUNE.PLLEN=0;
  ADCON1=0b00001111;
  TRISA.B0 = 0; //PORTD as output
  PORTA.B0=0;
  I2C_Slave_Init(0xAA); //Initialize as a I2C Slave with address 0x30
  while(1)
  {
   if(new==1)
     {
      PORTA.B0=1;
      new=0;
     }
   }
}

The problem is each time I send 0xAA from the master to the slave after putting 0xAA to slave SSPADD, I get NOTACK signal. I don't know where is the thing who doesn't work.


Here you are, screenShot for the WaveForm i get 2VGF4Gk.png
 
Last edited by a moderator:

Re: Two PIC 18f4620 , 1 as master.2nd is slave I2C problem

just looking at this briefly, I'm confused by the statement:

SSPADD = (crystalOscillator/(4*c))-1;

That sure doesn't match the address declared in the slave module which is 0xaa,(but commented as 0x30.)
 

Re: Two PIC 18f4620 , 1 as master.2nd is slave I2C problem

just looking at this briefly, I'm confused by the statement:

SSPADD = (crystalOscillator/(4*c))-1;

That sure doesn't match the address declared in the slave module which is 0xaa,(but commented as 0x30.)

In master mode SSPADD of the master pic is filled not with address of the slave . It is filled with a value that is used in Baud rate counter to generate the clock pulses of the system over SCL pin
 

Re: Two PIC 18f4620 , 1 as master.2nd is slave I2C problem

Ah, my mistake. Have you looked at signals with an oscilloscope? That might help a lot. Or use the debugger.
 

Re: Two PIC 18f4620 , 1 as master.2nd is slave I2C problem

Ah, my mistake. Have you looked at signals with an oscilloscope? That might help a lot. Or use the debugger.

Could u please check the attaches in the main thread post above
 
Last edited:

Re: Two PIC 18f4620 , 1 as master.2nd is slave I2C problem

Could u please check the attaches in the main thread post above

AHA! Your problem is that 0xAA is an invalid address. I2C addresses are 7 bit, you've got an 8-bit address.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top