• AVR Freaks

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

Page: << < ..6789 > Showing page 7 of 9
Author
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/14 09:46:48 (permalink)
+1 (1)

I2C_Master_Start(); //Start condition
I2C_Master_Write(0b10011010); //7 bit address + Write
I2C_Master_Write(0x00);
I2C1_Repeated_Start();
I2C_Master_Write(0b10011011);
temp=I2C_Master_Read(1);
I2C_Master_Stop();
PORTB=temp;
temp=0;

i wrote this code for TC74 and try it on simulator just to do something instead of waiting and burning time 
 first i send 0b10011010 it is ok with ACk signal on SDA at the SCL 9th bit  after that there is a weird  one bit while the SCL is stretched low it seems as if it is repeated start signal while as u see in the code the repeated start is after sending 0x00 not before it . then send 8 zeros with ack signal then repeated start  then replay with 0xff from the TC74 which is weird too 
 
HERE U ARE THE SCREENSHOT https://imgur.com/m9seCyL
 
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/14 09:52:36 (permalink)
0
HERE u are a BITMAP picture for the new schematic https://imgur.com/EmM0mpo
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/14 09:54:55 (permalink)
0
The complete master code for TC74 if u may are interested

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 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
Delay_ms(500);
TRISB=0;
PORTB=0;
while(1)
{
I2C_Master_Start(); //Start condition
I2C_Master_Write(0b10011010); //7 bit address + Write
I2C_Master_Write(0x00);
I2C1_Repeated_Start();
I2C_Master_Write(0b10011011);
temp=I2C_Master_Read(1);
I2C_Master_Stop();
PORTB=temp;
temp=0;
Delay_ms(5000);
}
}

 
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/14 09:55:11 (permalink)
+1 (1)
ubuntuman
const unsigned short int crystalOscillator =4000000;      // <<<<<< unsigned short int?


u are absolutely right Jerry . i changed it to unsigned long int 




I wrote the same, some time ago :)

GENOVA :D :D ! GODO
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/14 09:59:22 (permalink)
0
Good! we got to TC74 Smile
 
I'd try with real parts, though...

GENOVA :D :D ! GODO
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/14 10:00:53 (permalink)
+1 (1)
DarioG
ubuntuman
const unsigned short int crystalOscillator =4000000;      // <<<<<< unsigned short int?


u are absolutely right Jerry . i changed it to unsigned long int 




I wrote the same, some time ago :)


vote up and sorry if i missed ur post 
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/14 10:02:11 (permalink)
+1 (1)
DarioG
Good! we got to TC74 Smile
 
I'd try with real parts, though...


i will try to get the real one but until then i wont stay doing nothing i just try and see ur approach as u suggested 
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/14 10:03:11 (permalink)
0
wait u to check and tell me about the result of the code  above i post in code and in the screenshot of the oscilloscope ad the screenshot of the schematic 
 
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/14 10:04:30 (permalink)
0
Could you try SW I2C then, like here:
http://www.microchip.com/forums/FindPost/295741
?

GENOVA :D :D ! GODO
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/14 10:08:54 (permalink)
0
DarioG
Could you try SW I2C then, like here:

look to that quote please and u will understand why i dont use SW I2C

  • Important :
    This library implements time-based activities, so interrupts need to be disabled when using Software I²C.
    All I²C Library functions are blocking-call functions (they are waiting for I²C clock line to become logical one).
    The pins used for the Software I²C communication should be connected to the pull-up resistors. Turning off the LEDs connected to these pins may also be required.
    Every Software I²C library routine has its own counterpart in Hardware I²C library, except I2C_Repeated_Start. Soft_I2C_Start is used instead of I2C_Repeated_Start.
    Working clock frequency of the Software I²C is 20kHz.
     
i have a ready software I2C library in the compiler MikroC for pic but i have many interrupts that i will build later after i put my I2C code and i have timers to use , many other things 
post edited by ubuntuman - 2017/11/14 10:10: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/14 10:13:10 (permalink)
0
Hmmm, I2C is synchronous so it does not care about IRQs...
 
I never had issues with my code and I always have (short) IRQs active. Moreover, my code works at some 150KHz :)
 
I don't know about MikroC's libraries. I know I trust mine and I know what they do in detail.

GENOVA :D :D ! GODO
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/14 12:22:23 (permalink)
0
DarioG
Hmmm, I2C is synchronous so it does not care about IRQs...
 
I never had issues with my code and I always have (short) IRQs active. Moreover, my code works at some 150KHz :)
 
I don't know about MikroC's libraries. I know I trust mine and I know what they do in detail.


ok even if i agree with u about this . rewriting the whole code of a complete software I2C is very hard thing to do and consuming lots of time because i wont take ur code copy and paste i need to read it bit by bit then rewrite whole of it according to my compiler syntax so in this case i will surrender and search for an alternative thing  it will take at least a week to do what u are saying and this is not a solution . i want to use the hardware I2C if the matter is like that . why they put a hardware I2C in the devices .. beside day after day i can see a progress we discover things and modify 
 
i guess if someone will tell me about this weird one bit after each ack signal in the digram above it will be a good thing  i post the code the schematic and the screenshot of wave form for the master and the TC74 thermal sensor 
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/14 12:24:19 (permalink)
+1 (1)
I2C_Master_Start(); //Start condition
I2C_Master_Write(0b10011010); //7 bit address + Write
I2C_Master_Write(0x00);
I2C1_Repeated_Start();
I2C_Master_Write(0b10011011);
temp=I2C_Master_Read(1);
I2C_Master_Stop();
PORTB=temp;
temp=0;

i wrote this code for TC74 and try it on simulator just to do something instead of waiting and burning time
first i send 0b10011010 it is ok with ACk signal on SDA at the SCL 9th bit after that there is a weird one bit while the SCL is stretched low it seems as if it is repeated start signal while as u see in the code the repeated start is after sending 0x00 not before it . then send 8 zeros with ack signal then repeated start then replay with 0xff from the TC74 which is weird too

HERE U ARE THE SCREENSHOT https://imgur.com/m9seCyL
HERE u are a BITMAP picture for the new schematic https://imgur.com/EmM0mpo
 
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/14 12:25:22 (permalink)
0
The complete master code for TC74 if u may are interested
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 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
Delay_ms(500);
TRISB=0;
PORTB=0;
while(1)
{
I2C_Master_Start(); //Start condition
I2C_Master_Write(0b10011010); //7 bit address + Write
I2C_Master_Write(0x00);
I2C1_Repeated_Start();
I2C_Master_Write(0b10011011);
temp=I2C_Master_Read(1);
I2C_Master_Stop();
PORTB=temp;
temp=0;
Delay_ms(5000);
}
}

 
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem 2017/11/14 12:31:23 (permalink)
+1 (1)
ubuntuman
...
i guess if someone will tell me about this weird one bit after each ack signal in the digram above it will be a good thing  i post the code the schematic and the screenshot of wave form for the master and the TC74 thermal sensor 

Totally normal.
At that instant, neither the Master or the slave is driving the SDA pin, so it is pulled high.
Changes to SDA when SCL is low are irrelevant.
In  a REAL device it wouldn't float for so long, so you are still just chasing anomalies in the simulator.
 
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/14 12:47:26 (permalink)
0
qhb
ubuntuman
...
i guess if someone will tell me about this weird one bit after each ack signal in the digram above it will be a good thing  i post the code the schematic and the screenshot of wave form for the master and the TC74 thermal sensor 

Totally normal.
At that instant, neither the Master or the slave is driving the SDA pin, so it is pulled high.
Changes to SDA when SCL is low are irrelevant.
In  a REAL device it wouldn't float for so long, so you are still just chasing anomalies in the simulator.

i just do my best with what is possible for me with tools right now 
so u think that is ok but look and this quote of datasheet 

A Repeated Start condition occurs when the RSEN bit
(SSPCON2<1>) is programmed high and the I2C logic
module is in the Idle state. When the RSEN bit is set,
the SCL pin is asserted low. When the SCL pin is
sampled low, the Baud Rate Generator is loaded with
the contents of SSPADD<5:0> and begins counting.
The SDA pin is released (brought high) for one Baud
Rate Generator count (TBRG). When the Baud Rate
Generator times out, if SDA is sampled high, the SCL
pin will be deasserted (brought high).

 
so will SCL is low not every change in the SDA is irrelevant because this one bit whil SCL is low is eactly a repeated start signal . wrong
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/14 13:16:22 (permalink)
0
it wasnt normal 
i discoverted that i mixed between the library of the I2C of the compiler MikroC for PIC and the new library that i make in the repeated start signal 

I2C_Master_Start(); //Start condition
I2C_Master_Write(0b10011010); //7 bit address + Write
I2C_Master_Write(0x00);
I2C1_Repeated_Start();
I2C_Master_Write(0b10011011);
temp=I2C_Master_Read(1);
I2C_Master_Stop();
PORTB=temp;
temp=0;

i correct it to be this 

I2C_Master_Start(); //Start condition
I2C_Master_Write(0b10011010); //7 bit address + Write
I2C_Master_Write(0x00);
I2C_Master_RepeatedStart();
I2C_Master_Write(0b10011011);
temp=I2C_Master_Read(1);
I2C_Master_Stop();

 
check it out a complete difference  https://imgur.com/ekYFHNN
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/15 03:27:15 (permalink)
0
so i finally get ACK signal from the T74 sensor 
WaveFrom of the signal https://imgur.com/r4MzEWY
 
The code of master


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(33000); //Initialize I2C Master with 100KHz clock
//I2C1_Init(50000);
Delay_ms(500);
TRISB=0;
PORTB=0;
while(1)
{
I2C_Master_Start(); //Start condition
I2C_Master_Write(0b10011010); //7 bit address + Write
I2C_Master_Write(0x00);
I2C_Master_RepeatedStart();
I2C_Master_Write(0b10011011);
temp=I2C_Master_Read(1);
I2C_Master_Stop();
PORTB=temp;
temp=0;//Stop condition
Delay_ms(5000);
}
}

 
so after  testing the master code alone with another slave device and here u are the result 
do u think the master code is working fine now ?
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/15 03:32:13 (permalink)
0
Wow! well, good!
 
Then you can try working on the Slave, maybe using the same address as TC74

GENOVA :D :D ! GODO
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/15 03:41:55 (permalink)
+1 (1)
i just did right now & a got this https://imgur.com/QQ2Byyz
Page: << < ..6789 > Showing page 7 of 9
Jump to:
© 2019 APG vNext Commercial Version 4.5