• AVR Freaks

Hot!TWo PIC 18f4620 , one as master & the second is slave I2C communication problem

Page: < 12345.. > >> Showing page 2 of 9
Author
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/11 14:18:08 (permalink)
+1 (1)
You have again 18 in both places Smile

GENOVA :D :D ! GODO
#21
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/11 14:25:44 (permalink)
+1 (1)
Indeed your photo show that only 2 bytes are transmitted, plus "something" in between.
 
Where is the code for I2C1_Wr() ?

GENOVA :D :D ! GODO
#22
ubuntuman
Super Member
  • Total Posts : 232
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/11 14:35:13 (permalink)
+1 (1)
DarioG
Indeed your photo show that only 2 bytes are transmitted, plus "something" in between.
 
Where is the code for I2C1_Wr() ?


i put 0x30 in master and 0x18 in slave 
 
i use the I2c liberary of the "Mikroc for pic pro 6.0" so the code of write function is a builtin one 
#23
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/11 14:43:13 (permalink)
0
Maybe you have to wait for an ACK in between - I don't know those libraries...

GENOVA :D :D ! GODO
#24
ubuntuman
Super Member
  • Total Posts : 232
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/11 14:52:39 (permalink)
0
DarioG
Maybe you have to wait for an ACK in between - I don't know those libraries...


the acknowledge signal come from the slave by putting the data line to low 0 voltage after the 9th bit of the clock as i understand not coming from the master if i am not mistaken 
#25
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/11 15:13:00 (permalink)
0
Yes, the ACK comes from the slave: but sometimes you need to call a separate function in order to check for it (at least, this is how Microchip did in its libraries)

GENOVA :D :D ! GODO
#26
ubuntuman
Super Member
  • Total Posts : 232
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/11 15:48:32 (permalink)
0
DarioG
Yes, the ACK comes from the slave: but sometimes you need to call a separate function in order to check for it (at least, this is how Microchip did in its libraries)


master code

// PIC16F877A Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
#include <pic16f877a.h>

#define _XTAL_FREQ 8000000

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage In-Circuit Serial Programming Enable bit
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit
#pragma config WRT = OFF // Flash Program Memory Write Enable bits
#pragma config CP = OFF // Flash Program Memory Code Protection bit

void I2C_Master_Init(const unsigned long c)
{
SSPCON = 0b00101000;
SSPCON2 = 0;
SSPADD = (_XTAL_FREQ/(4*c))-1;
SSPSTAT = 0;
TRISC3 = 1; //Setting as input as given in datasheet
TRISC4 = 1; //Setting as input as given in datasheet
}

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

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

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

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

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

unsigned short I2C_Master_Read(unsigned short a)
{
unsigned short temp;
I2C_Master_Wait();
RCEN = 1;
I2C_Master_Wait();
temp = SSPBUF;
I2C_Master_Wait();
ACKDT = (a)?0:1;
ACKEN = 1;
return temp;
}

void main()
{
nRBPU = 0; //Enable PORTB internal pull up resistor
TRISB = 0xFF; //PORTB as input
TRISD = 0x00; //PORTD as output
PORTD = 0x00; //All LEDs OFF
I2C_Master_Init(100000); //Initialize I2C Master with 100KHz clock
while(1)
{
I2C_Master_Start(); //Start condition
I2C_Master_Write(0x30); //7 bit address + Write
I2C_Master_Write(PORTB); //Write data
I2C_Master_Stop(); //Stop condition
__delay_ms(200);
I2C_Master_Start(); //Start condition
I2C_Master_Write(0x31); //7 bit address + Read
PORTD = I2C_Master_Read(0); //Read + Acknowledge
I2C_Master_Stop(); //Stop condition
__delay_ms(200);
}
}

Slave code

// PIC16F877A Configuration Bit Settings
// 'C' source line config statements

#include <xc.h>
#include <pic16f877a.h>
#define _XTAL_FREQ 8000000

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage In-Circuit Serial Programming Enable bit
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit
#pragma config WRT = OFF // Flash Program Memory Write Enable bits
#pragma config CP = OFF // Flash Program Memory Code Protection bit
short z;
void interrupt I2C_Slave_Read()
{
if(SSPIF == 1)
{
SSPCONbits.CKP = 0;

if ((SSPCONbits.SSPOV) || (SSPCONbits.WCOL))
{
z = SSPBUF; // Read the previous value to clear the buffer
SSPCONbits.SSPOV = 0; // Clear the overflow flag
SSPCONbits.WCOL = 0; // Clear the collision bit
SSPCONbits.CKP = 1;
}

if(!SSPSTATbits.D_nA && !SSPSTATbits.R_nW)
{
z = SSPBUF;
while(!BF);
PORTD = SSPBUF;
SSPCONbits.CKP = 1;
}
else if(!SSPSTATbits.D_nA && SSPSTATbits.R_nW)
{
z = SSPBUF;
BF = 0;
SSPBUF = PORTB ;
SSPCONbits.CKP = 1;
while(SSPSTATbits.BF);
}

SSPIF = 0;
}
}

void I2C_Slave_Init(short address)
{
SSPSTAT = 0x80;
SSPADD = address;
SSPCON = 0x36;
SSPCON2 = 0x01;
TRISC3 = 1; //Setting as input as given in datasheet
TRISC4 = 1; //Setting as input as given in datasheet
GIE = 1;
PEIE = 1;
SSPIF = 0;
SSPIE = 1;
}

void main()
{
nRBPU = 0; //Enables PORTB internal pull up resistors
TRISB = 0xFF; //PORTB as input
TRISD = 0x00; //PORTD as output
PORTD = 0x00; //All LEDs OFF
I2C_Slave_Init(0x30); //Initialize as a I2C Slave with address 0x30
while(1);
}


i found this above complete code in some project not mine and i dont test it yet , could u check it out and give me ur opinion ?
post edited by ubuntuman - 2017/11/11 15:51:27
#27
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/11 16:07:49 (permalink)
+1 (1)
So this is a new code? I am not sure... try it

GENOVA :D :D ! GODO
#28
rodims
Super Member
  • Total Posts : 1490
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: online
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/12 06:01:15 (permalink)
+1 (1)
regardless is it & or && as i guess in the above version of the code it should be & not && be cause it is logically ANDIng the three bits if the result is 1 so the if condition is true

It's ANDing in both cases. While both variants probably result in the same effect for your if statement, the more logical one is &&  (literally).
 

i found this above complete code in some project not mine and i dont test it yet , could u check it out and give me ur opinion ?

That's no fun, if you change your complete code in the middle of the thread, but do not even test it yourself. Why not ? And how does the code relate to your schematics ? (e.g. PORTD, LED ? )
 
Looking at your schematics, I don't know how the software (Proteus?) draws the schematics. For me it looks as if you have connected the SCL lines, but you have not connected the SDA lines (Pin 23 of both CPUs)
 
a) However: may be the software does not distinguish when displaying 'crossings', i.e. either a bridge (no connection) or a connection.
   At least there are other crossings, which definitely must not have a connection.
 
b) But: from your comments and code concerning the LED, I must conclude that the PIC at the right side in the schematics is the slave, because only that one has a LED attached.  That means that the left one must be the master.
 
However then, since your Scope Logic Diagram shows both SCL and SDA activity, the master must have a connection with the scope.
Which finally means that the 'crossing' in this case is a 'connection', and then also both SDA pins are connected.
As said I have no experience with Proteus, but this kind of indifferent display for a bridge or a connection is weird .
 
Just double check yourself.
#29
ubuntuman
Super Member
  • Total Posts : 232
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/12 06:34:28 (permalink)
0
rodims
regardless is it & or && as i guess in the above version of the code it should be & not && be cause it is logically ANDIng the three bits if the result is 1 so the if condition is true

It's ANDing in both cases. While both variants probably result in the same effect for your if statement, the more logical one is &&  (literally).
 

i found this above complete code in some project not mine and i dont test it yet , could u check it out and give me ur opinion ?

That's no fun, if you change your complete code in the middle of the thread, but do not even test it yourself. Why not ? And how does the code relate to your schematics ? (e.g. PORTD, LED ? )
 
Looking at your schematics, I don't know how the software (Proteus?) draws the schematics. For me it looks as if you have connected the SCL lines, but you have not connected the SDA lines (Pin 23 of both CPUs)
 
a) However: may be the software does not distinguish when displaying 'crossings', i.e. either a bridge (no connection) or a connection.
  At least there are other crossings, which definitely must not have a connection.
 
b) But: from your comments and code concerning the LED, I must conclude that the PIC at the right side in the schematics is the slave, because only that one has a LED attached.  That means that the left one must be the master.
However then, since your Scope Logic Diagram shows both SCL and SDA activity, the master must have a connection with the scope.
Which finally means that the 'crossing' in this case is a 'connection', and then also both SDA pins are connected.
As said I have no experience with Proteus, but this kind of indifferent display for a bridge or a connection is weird .
 
Just double check yourself.


First Thank u 
second u are right the one on  the right is the slave and the one on the left is the master 
and dont worry both SCL  an d SDA are connected 
 
i post this other code just as hopeless try an d i mentioned that it is not mine 
the difference between that code and mine  is that he wrote the master library himself not using a previous made library like i use in the Mikroc for PIC program 
#30
ubuntuman
Super Member
  • Total Posts : 232
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/12 06:43:14 (permalink)
0
but can u look in the slave code 

if(!SSPSTATbits.D_nA && !SSPSTATbits.R_nW)
{
z = SSPBUF;
while(!BF);
PORTD = SSPBUF;
SSPCONbits.CKP = 1;
}

 
i dont understand why he is reading the  buffer in variable z then wait until BF to be 1  and read the buffer again to PORTD
i think once he read the buffer to variable z so there is no data to read again from the buffer it will be unloaded and BF bit will be clear and it will enter in infinite loop in this while loop .Am i mistaken? 
 
#31
rodims
Super Member
  • Total Posts : 1490
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: online
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/12 07:02:56 (permalink)
+1 (1)
could u check it out and give me ur opinion ?

Depending on the source it's very likely that the original code has worked for someone (the writer) with some (real) hardware configuration, if he published it.
Did you modify the code ?
Again the master code uses 0x30 / 0x31 and the slave the address 0x30.
Anyway, you have to test yourself whether it works for you or not and of course you have to review whether it fits your hardware (simulation).
 
#32
ubuntuman
Super Member
  • Total Posts : 232
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/12 09:43:27 (permalink)
0
slave new code after reformation 


//###########################SETTING LCD MODULE###################################################
// LCD module connections
sbit LCD_RS at RB5_bit;
sbit LCD_EN at RB4_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB5_bit;
sbit LCD_EN_Direction at TRISB4_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
//##############################################################################
unsigned short int zed=0;
volatile unsigned short int new =0 ,dataRead=0 , dataWrite;
//##############################################################################
void interrupt()
{
if(PIR1.SSPIF == 1)
{
SSPCON1.CKP = 0;
new=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.D_A) && !SSPSTAT.R_W)
{
while(!SSPSTAT.BF){};
while(SSPSTAT.BF){dataRead = SSPBUF;};
SSPCON1.CKP = 1;
}
else if((!SSPSTAT.D_A||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 = 0b10000000;
SSPADD = address;
SSPCON1 = 0x36;
SSPCON2 = 0x01;
TRISC.B3 = 1; //Setting as input as given in datasheet
TRISC.B4 = 1; //Setting as input as given in datasheet
INTCON = 0b00000000;
INTCON.PEIE = 1;
INTCON.GIE = 1;
PIE1.SSPIE = 1;
PIR1.SSPIF = 0;
}
//##############################################################################
void main()
{
TRISA.B0 = 0; //PORTD as output
PORTA.B0=0;
I2C_Slave_Init(0x30); //Initialize as a I2C Slave with address 0x30
while(1)
{
if(new==1)
{
PORTA.B0=1;
//Delay_1sec();
new=0;
}
}
}

 
master new code after reformation
 


const unsigned short int crystalOscillator =4000000;
void I2C_Master_Init(const unsigned long c)
{
SSPCON1 = 0b00101000;
SSPCON2 = 0;
SSPADD = (crystalOscillator/(4*c))-1;
SSPSTAT = 0;
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 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
while(1)
{
I2C_Master_Start(); //Start condition
I2C_Master_Write(0x30); //7 bit address + Write
I2C_Master_Write(0x04); //Write data
I2C_Master_Stop(); //Stop condition
Delay_ms(2000);
}
}

 
Just want u to check it our and tell me if there is any comments or notes u may want me to modify 
Thank u in advance 
#33
ubuntuman
Super Member
  • Total Posts : 232
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/12 09:54:50 (permalink)
0
A screenShot of the wave form after i modify the master code and slave code library . do u think we has a progress . i think yes in comparison with the last wave form screenShot of the previously builtin master code library of the compiler Mikroc For PIC 
 
check it out PLS https://imgur.com/qpc2Y9D
 
#34
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/12 09:56:21 (permalink)
+1 (1)
That address thing is hard to get, huh? grin
 
I'd also use 100000L where a number larger than 65535 is used, but this is possibly not an issue now.

GENOVA :D :D ! GODO
#35
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/12 09:57:08 (permalink)
+1 (1)
ubuntuman
A screenShot of the wave form after i modify the master code and slave code library . do u think we has a progress . i think yes in comparison with the last wave form screenShot of the previously builtin master code library of the compiler Mikroc For PIC 
 
check it out PLS https://imgur.com/qpc2Y9D
 




Hmmm not much, still not enough pulses for 3 bytes...

GENOVA :D :D ! GODO
#36
ubuntuman
Super Member
  • Total Posts : 232
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/12 10:05:16 (permalink)
+1 (1)
i send two bytes in the code i will add a third one and tell u the result 
#37
ubuntuman
Super Member
  • Total Posts : 232
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/12 10:14:09 (permalink)
+1 (1)
Here u are the three bytes sending wave form according to the new code
https://imgur.com/lNIp4It
 
#38
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/12 10:15:14 (permalink)
0
Ok, now it looks better!

GENOVA :D :D ! GODO
#39
ubuntuman
Super Member
  • Total Posts : 232
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/12 10:18:05 (permalink)
0

but can u look in the slave code 

if(!SSPSTATbits.D_nA && !SSPSTATbits.R_nW)
{
z = SSPBUF;
while(!BF);
PORTD = SSPBUF;
SSPCONbits.CKP = 1;
}

 
i dont understand why he is reading the  buffer in variable z then wait until BF to be 1  and read the buffer again to PORTD
i think once he read the buffer to variable z so there is no data to read again from the buffer it will be unloaded and BF bit will be clear and it will enter in infinite loop in this while loop .Am i mistaken? 


Can u reply to me for this one pls ?
post edited by ubuntuman - 2017/11/12 10:22:02
#40
Page: < 12345.. > >> Showing page 2 of 9
Jump to:
© 2019 APG vNext Commercial Version 4.5