CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Two PIC 18f4620, 1 as master & 2nd is slave I2C problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ubuntuman



Joined: 15 Nov 2017
Posts: 2

View user's profile Send private message

Two PIC 18f4620, 1 as master & 2nd is slave I2C problem
PostPosted: Wed Nov 15, 2017 5:54 am     Reply with quote

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 https://imgur.com/2VGF4Gk


Last edited by ubuntuman on Wed Nov 15, 2017 7:07 am; edited 1 time in total
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Nov 15, 2017 6:34 am     Reply with quote

quick comments

1. Doesn't look like CCS C code. CCS supply easy to use I2C functions
2. No PIC device header as 1st line of code, can't cut/paste, no fuses?
3. With ANY I2C program, run I2C Scanner program from code library
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 15, 2017 6:43 am     Reply with quote

It's for the MikroC compiler. Not CCS.
ubuntuman



Joined: 15 Nov 2017
Posts: 2

View user's profile Send private message

PostPosted: Wed Nov 15, 2017 7:21 am     Reply with quote

PCM programmer wrote:
It's for the MikroC compiler. Not CCS.


Right but the problem is not in compilers. It is in the pic 18f4620 settings and in the MSSP hardware register settings so if you have any working code for 18f4620 master/slave written in any language, pass it pls to me.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 15, 2017 7:45 am     Reply with quote

We won't have anything you can use. CCS has built-in functions to do
all the i2c setup and bus transactions. We don't have to write to PIC
registers to make something work.

For example, you've got this as your setup code in MikroC:
Code:

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
}


We would write something like:
Code:
#use i2c(Master, I2C1)

and that would handle all the setup.


Another example. This is how you do a start in MikroC.
Code:
void I2C_Master_Start()
{
 I2C_Master_Wait();
 SSPCON2.SEN = 1;
}

This is how we do it in CCS.
Code:
i2c_start();


See, our way of coding won't help you. You need to go to a MikroC forum.
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Nov 15, 2017 8:55 am     Reply with quote

re:
Quote:
See, our way of coding won't help you. You need to go to a MikroC forum.

It'd be better to just buy the CCS compiler !! Razz

His project could be up and running in an hour or so....
AND

then we could help him...

Jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group