ubuntuman
Super Member
- Total Posts : 278
- Reward points : 0
- Joined: 2016/05/02 15:28:40
- Location: 0
- Status: offline
TWo PIC 18f4620 , one as master & the second is slave I2C communication problem
I use "Mikroc for PIC pro 6.0" and i write a program to communicate between two 18f4620 one is master and the other is slave MASTER CODE unsigned short int sendData=0; //###################################################################################################################################### void main() { OSCCON.SCS1=1; OSCCON.SCS0=0; // external oscillator HS oscillator option 4MHZ //from "edit project" option in mikroc program CMCON = 0b00000111; TRISC.B3 = 1; TRISC.B4 = 1; I2C1_Init(100000); //Inicializar Bus I2C a 100kHz delay_ms(500); while(1) { // initialize I2C communication I2C1_Start(); // issue I2C start signal I2C1_Wr(0x18); // send byte via I2C (device address + W) I2C1_Wr(4); // send byte (address of EEPROM location) I2C1_Wr(0xAA); // send data (data to be written) I2C1_Stop(); delay_ms(2000); } } Slave code int counter=0,address=0,rxbuffer=0,buff=0, new=0;
void interrupt() { if (PIR1.SSPIF) { new=1; SSPCON1.CKP = 0; if(!SSPSTAT.B2 & SSPSTAT.B5 & SSPSTAT.B0 ) { counter++; if (counter==1) { address=SSPBUF; }
if (counter==2) { rxbuffer=SSPBUF; counter=0; } PIR1.SSPIF = 0; SSPCON1.CKP=1; new=1; } if( !SSPSTAT.B2 & !SSPSTAT.B5 & SSPSTAT.B0 ) { buff = SSPBUF; PIR1.SSPIF = 0; SSPCON1.CKP=1; new=1; } PIR1.SSPIF = 0; } } void main() { ADCON1=0b00001111; CMCON=0b00000111; TRISC.B3=1; TRISC.B4=1; INTCON=0b00000000; SSPCON1=0b00110110; SSPCON2=0b00000001; SSPADD=0x18; SSPSTAT=0x00; SSPCON1.SSPEN=1; SSPSTAT.SMP=1; SSPSTAT.CKE=0; PIR1.SSPIF=0; PIR2.BCLIF=0; PIE1.SSPIE=1; INTCON.PEIE=1; INTCON.GIE=1; TRISA.B0=0; while(1) { if(new==1) { PORTA.B0=1; new=0; } }
} i use this combination in proteus for testing so i should fin d the led on portA pin0 is lighting which is never happen so the PIR1.SSPIF never gets one so we can say that the slave doesnt give a match for her calling address from the master . The one million dollar question is " Why doesnt she generate an address matching interrupt to make "PIR1.SSPIF" equal 1?
|
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 10:08:14
(permalink)
Use && in here if(!SSPSTAT.B2 & SSPSTAT.B5 & SSPSTAT.B0 )
And also make the variables that are shared between Interrupt and Main "volatile"
|
ubuntuman
Super Member
- Total Posts : 278
- 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 10:25:04
(permalink)
|
ubuntuman
Super Member
- Total Posts : 278
- 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 10:32:57
(permalink)
i did what u say and i try but zero changes thank u for ur effort
|
ubuntuman
Super Member
- Total Posts : 278
- 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 10:39:10
(permalink)
HERE U ARE ANOTHER PICTURES ABOUT THE COMBINATION OF THE TWO PICS THAT MAY HELP U TO HELP ME . fULL WAVE ON THE OSCILLOSCOPE link for first image https://imgur.com/yBWFD9x
post edited by ubuntuman - 2017/11/11 10:44:22
|
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 10:55:51
(permalink)
It could (could) be that slave's SSPADD wants the value not-shifted, i.e. in your case 0x0c - to match the incoming 0x18 (I almost never used HW I2C so am never sure about that...)
|
rodims
Super Member
- Total Posts : 1558
- Reward points : 0
- Joined: 2009/02/10 11:08:59
- Location: 51.9627, 7.6262
- Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem
2017/11/11 11:12:16
(permalink)
+1 or the other way round. If the slave defines SSPADD=0x18; then try I2C1_Wr(0x18<<1); // send byte via I2C (device address + W) for the master to add the least significant bit for R/W [edit: as it turns out two days later, here I was wrong unfortunately. An i2c slave on a PIC24 and a PIC18 use a different address scheme. My explanations are valid for the master and a PIC24 slave. But the PIC18 expects the fully address byte and simply ignores the least significant bit (R/W). E.g. here it would for an i2c address 0x18, the master sends 0x30 for a WRITE, the slaves sets 0x30 in SPADD and will simply not use the LSB. ]
post edited by rodims - 2017/11/14 07:57:59
|
ubuntuman
Super Member
- Total Posts : 278
- 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 11:22:38
(permalink)
DarioG It could (could) be that slave's SSPADD wants the value not-shifted, i.e. in your case 0x0c - to match the incoming 0x18 (I almost never used HW I2C so am never sure about that...)
i dont understand what u mean by not shifted . could u explain more pls ?
|
ubuntuman
Super Member
- Total Posts : 278
- 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 11:23:51
(permalink)
rodims +1 or the other way round. If the slave defines
SSPADD=0x18; then try
I2C1_Wr(0x18<<1); // send byte via I2C (device address + W) for the master to add the least significant bit for R/W
Why should i shift to the left ? the address the 8th BIT is already zero for write
post edited by ubuntuman - 2017/11/11 12:42:32
|
rodims
Super Member
- Total Posts : 1558
- Reward points : 0
- Joined: 2009/02/10 11:08:59
- Location: 51.9627, 7.6262
- Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem
2017/11/11 11:41:26
(permalink)
The shift left just makes clear that you make room for the R/W bit. I would not code it like that of course. The 7 MSB are the device address, the LSB is the R/W. Thus your device master currently uses a device address of 0x0c. And a LSB '0' for WRITE. If the addresses don't match, there will be no interrupt. Why don't you just try ? https://support.saleae.co...ter-Integrated-Circuit
|
ubuntuman
Super Member
- Total Posts : 278
- 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 12:36:54
(permalink)
rodims The shift left just makes clear that you make room for the R/W bit. I would not code it like that of course. The 7 MSB are the device address, the LSB is the R/W. Thus your device master currently uses a device address of 0x0c. And a LSB '0' for WRITE. If the addresses don't match, there will be no interrupt. Why don't you just try ? https://support.saleae.co...ter-Integrated-Circuit
I dont understand what u want me exactly to write and where in slave code or in master code beside i dont get what u say about shifted and un-shifteted so i will be grateful if u enlighten me more pls
|
ubuntuman
Super Member
- Total Posts : 278
- 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 13:03:17
(permalink)
rodims The shift left just makes clear that you make room for the R/W bit. I would not code it like that of course. The 7 MSB are the device address, the LSB is the R/W. Thus your device master currently uses a device address of 0x0c. And a LSB '0' for WRITE. If the addresses don't match, there will be no interrupt. Why don't you just try ? https://support.saleae.co...ter-Integrated-Circuit
if u want me to write in master code I2C1_Wr(0x18<<1); // send byte via I2C (device address + W) and to write in slave code SSPADD=0x18; i did it and i dont understand why but it didnt work the same no address match . thank u for ur trial with me and i will be glad if u explain why u wanted to do this
|
ubuntuman
Super Member
- Total Posts : 278
- 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 13:09:20
(permalink)
rodims The shift left just makes clear that you make room for the R/W bit. I would not code it like that of course. The 7 MSB are the device address, the LSB is the R/W. Thus your device master currently uses a device address of 0x0c. And a LSB '0' for WRITE. If the addresses don't match, there will be no interrupt. Why don't you just try ? https://support.saleae.co...ter-Integrated-Circuit
BUT if u want me to write in master code I2C1_Wr(0x18<<1); // send byte via I2C (device address + W) and to write in slave code SSPADD=0x0C; i did it and i dont understand why but it didnt work the same no address match
|
rodims
Super Member
- Total Posts : 1558
- Reward points : 0
- Joined: 2009/02/10 11:08:59
- Location: 51.9627, 7.6262
- Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem
2017/11/11 13:17:02
(permalink)
No, I did not want you to do that. Now you mangled both sides. (writing 0x30 in slave master, using ADDR 0x0c in slave) Please use 0x18 in your slave (as you originally did). And use 0x30 in the master for WRITE and 0x31 for READ. [edit: I did not see that you already added multiple posts. If you already tested my above proposal, then you need to search for additional problems]
post edited by rodims - 2017/11/13 04:06:29
|
ubuntuman
Super Member
- Total Posts : 278
- 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 13:26:28
(permalink)
rodims No, I did not want you to do that. Now you mangled both sides. (writing 0x30 in slave, using ADDR 0x0c in slave) Please use 0x18 in your slave (as you originally did). And use 0x30 in the master for WRITE and 0x31 for READ. [edit: I did not see that you already added multiple posts. If you already tested my above proposal, then you need to search for additional problems]
Master while(1) { // initialize I2C communication I2C1_Start(); // issue I2C start signal I2C1_Wr(0x30); // send byte via I2C (device address + W) I2C1_Wr(4); // send byte (address of EEPROM location) I2C1_Wr(0xAA); // send data (data to be written) I2C1_Stop(); delay_ms(2000); //WriteToSlavePic(4,sendData); sendData++; // Delay_1sec(); } }
slave
INTCON=0b00000000; SSPCON1=0b00110110; // Modo esclavo 7 bits de direccion. SSPCON2=0b00000001; SSPADD=0x18; //Dirección del dispositivo SSPSTAT=0x00; SSPCON1.SSPEN=1; SSPSTAT.SMP=1; SSPSTAT.CKE=0; PIR1.SSPIF=0; PIR2.BCLIF=0; PIE1.SSPIE=1; // Habilita la interrupcion del modulo SSP INTCON.PEIE=1; // Habilita las interrupciones de los perifericos INTCON.GIE=1;
I tried as u see but no change i want ur help to fin d out what is the "more problem" do i have because it seems for me that there is no but i dont know what to do more to make it work
post edited by ubuntuman - 2017/11/11 13:29:54
|
rodims
Super Member
- Total Posts : 1558
- Reward points : 0
- Joined: 2009/02/10 11:08:59
- Location: 51.9627, 7.6262
- Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem
2017/11/11 13:44:56
(permalink)
I'm confident that you now removed at least the address problem. For the other problems someone else might have an idea, since I leave now ... I have no experience with Proteus and no special experience with 18f4620 Your schematics https://imgur.com/1pYF0d6 is hardly to read, I cannot not verify the connections with it.
|
rodims
Super Member
- Total Posts : 1558
- Reward points : 0
- Joined: 2009/02/10 11:08:59
- Location: 51.9627, 7.6262
- Status: offline
Re: TWo PIC 18f4620 , one as master & the second is slave I2C communication problem
2017/11/11 13:47:55
(permalink)
And of course you should apply Darios changes from #2 (&&), and then place a corrected version of the complete master and slave code here ...
|
ubuntuman
Super Member
- Total Posts : 278
- 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:02:25
(permalink)
Master code unsigned short int sendData=0; //###################################################################################################################################### void main() { OSCCON.SCS1=1; OSCCON.SCS0=0; CMCON = 0b00000111; TRISC.B3 = 1; TRISC.B4 = 1; I2C1_Init(100000); //Inicializar Bus I2C a 100kHz delay_ms(500); while(1) { I2C1_Start(); // issue I2C start signal I2C1_Wr(0x30); // send byte via I2C (device address + W) I2C1_Wr(4); // send byte (address of EEPROM location) I2C1_Wr(0xAA); // send data (data to be written) I2C1_Stop(); delay_ms(2000); sendData++; } }
salve code //############################################################################## volatile int counter=0,address=0,rxbuffer=0,buff=0, new=0; void interrupt() { if (PIR1.SSPIF) { new=1; //Comprueba si la interrupción es por I2C SSPCON1.CKP = 0; if(!SSPSTAT.B2==1 && SSPSTAT.B5==1 && SSPSTAT.B0==1 ) { counter++; if (counter==1) { address=SSPBUF; } if (counter==2) { rxbuffer=SSPBUF; counter=0; } PIR1.SSPIF = 0; SSPCON1.CKP=1; new=1; } if( !SSPSTAT.B2==1 && !SSPSTAT.B5==1 && SSPSTAT.B0==1 ) { buff = SSPBUF; PIR1.SSPIF = 0; SSPCON1.CKP=1; new=1; } PIR1.SSPIF = 0; } } void main() { ADCON1=0b00001111; CMCON=0b00000111; TRISC.B3=1; TRISC.B4=1; //Configuracion de interrupciones INTCON=0b00000000; SSPCON1=0b00110110; SSPCON2=0b00000001; SSPADD=0x18; SSPSTAT=0x00; SSPCON1.SSPEN=1; SSPSTAT.SMP=1; SSPSTAT.CKE=0; PIR1.SSPIF=0; PIR2.BCLIF=0; PIE1.SSPIE=1; INTCON.PEIE=1; INTCON.GIE=1; TRISA.B0=0; while(1) { if(new==1) { PORTA.B0=1; new=0; } } }
better schematic screenshot https://imgur.com/HrZSffE
post edited by ubuntuman - 2017/11/11 14:27:07
|
ubuntuman
Super Member
- Total Posts : 278
- 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:09:13
(permalink)
I think there is something with the clock and data sent and received . i just feel that because simply the new =0; is out of the !SSPSTAT.B2==1 && SSPSTAT.B5==1 && SSPSTAT.B0==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 anyway the new variable is out of the if condition so once the interrupt happens , portA.B0 should light the LED up
post edited by ubuntuman - 2017/11/11 14:16:25
|
ubuntuman
Super Member
- Total Posts : 278
- 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:13:33
(permalink)
Can anyone have a look on this screenshot please for the data and clock transmission https://imgur.com/a/UpyZY i feel it is not good sequence from the master according to the I2C usual sequence
|