View previous topic :: View next topic |
Author |
Message |
Tres Guest
|
I2C with 3 PICs |
Posted: Fri Nov 10, 2006 11:51 am |
|
|
I have problem connecting 3 pics with I2C I can connect two pics fine but when i connect the second slave Master just keep receiving 0xFF
Here's the code
Master
Code: |
#include "18F458.inc"
#include <stdio.h>
#include <math.h>
#use i2c(Master,fast,sda=PIN_C4,scl=PIN_C3,force_hw)
char mode;
int8 DataA1,DataA2,DataA3,DataB1,DataB2,DataB3;
void main(void) {
delay_ms(250);
printf("Master start \n\r");
while(1)
{
//Slave A
i2c_start();
i2c_write(0xa0 + 1); // read
DataA1=i2c_read(1);
DataA2=i2c_read(1);
DataA3=i2c_read(0);
i2c_stop();
delay_ms(100);
//Slave B
i2c_start();
i2c_write(0xb0 + 1); // read
DataB1=i2c_read(1);
DataB2=i2c_read(1);
DataB3=i2c_read(0);
i2c_stop();
printf("DataA1=%d DataA2=%d DataA3=%d DataB1=%d DataB2=%d DataB3=%d\n\r",DataA1,DataA2,DataA3,DataB1,DataB2,DataB3);
delay_ms(100);
}
}
|
Slave A
Code: |
#include "18F458.inc"
#include <stdio.h>
#include <math.h>
#use i2c(Slave,Fast,sda=PIN_C4,scl=PIN_C3,address=0xa0,force_hw)
enum {address,data};
int mode,i;
#int_SSP
SSP_isr()
{
BYTE incoming, state;
state = i2c_isr_state();
if(state < 0x80) //Master is sending data
{
incoming = i2c_read(1);
printf("Function=%X\r\n",incoming);
}
state = i2c_isr_state();
if(state == 0x80) //Master is requesting data
{
i2c_write(0x11);
i2c_write(0x22);
i2c_write(0x33);
}
}
void main()
{
enable_interrupts(INT_SSP);
enable_interrupts(global);
mode=address;
printf("Slave B Start\n\r");
while(1)
{
printf("Running\n\r");
delay_ms(200);
}
}
|
Slave B
Code: |
#include "18F458.inc"
#include <stdio.h>
#include <math.h>
#use i2c(Slave,Fast,sda=PIN_C4,scl=PIN_C3,address=0xb0,force_hw)
enum {address,data};
int mode,i;
#int_SSP
SSP_isr()
{
BYTE incoming, state;
state = i2c_isr_state();
if(state < 0x80) //Master is sending data
{
incoming = i2c_read(1);
printf("Function=%X\r\n",incoming);
}
state = i2c_isr_state();
if(state == 0x80) //Master is requesting data
{
i2c_write(0x44);
i2c_write(0x55);
i2c_write(0x66);
}
}
void main()
{
enable_interrupts(INT_SSP);
enable_interrupts(global);
mode=address;
printf("Slave B Start\n\r");
while(1)
{
printf("Running\n\r");
delay_ms(200);
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
Tres Guest
|
One more file |
Posted: Fri Nov 10, 2006 11:53 am |
|
|
18F458.inc
Code: |
#include <18F458.h>
#fuses H4,NOWDT,WDT1,WRT,NOLVP
#use delay (clock=40000000)
#use rs232(baud=9600 ,parity=N, xmit=PIN_C6,rcv=PIN_C7,bits=8)
| [/code] |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Fri Nov 10, 2006 4:08 pm |
|
|
Try:
Code: |
#int_SSP
SSP_isr()
{
BYTE incoming, state;
state = i2c_isr_state();
if(state < 0x80) //Master is sending data
{
//Not for state zero,
//Otherwise this message will be printed for the address,
//This will result in a long delay, and probably cause problems.
if (state!=0) {
incoming = i2c_read(0);
//Assuming this is the last byte of a read transaction, it must be
//handled with a NACK.
printf("Function=%X\r\n",incoming);
}
else
I2C_READ(1); //for the address
}
else {
switch (state) {
case 0x80 :
i2c_write(0x11);
break;
case 0x81 :
i2c_write(0x22);
break;
case 0x82 :
i2c_write(0x33);
break;
default :
//should never get here
i2c_write(0);
}
}
|
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Tres Guest
|
Still not working |
Posted: Sat Nov 11, 2006 1:21 am |
|
|
it's still not working. i'm still receiving FF. |
|
![](templates/subSilver/images/spacer.gif) |
pasini
Joined: 12 Dec 2005 Posts: 50 Location: Curitiba - Brazil
|
|
Posted: Sun Nov 12, 2006 6:05 pm |
|
|
Hi,
I have faced some problems with multiple devices in the I2C bus (1 MCU + 1 I/O expander + 1 LCD driver). It worked fine with 1 device but not 2. Try removing the FORCE_HW from the #use I2C statment, it might work, altough it is not guaranteed because I didn't look the datasheet or errata.
Pasini |
|
![](templates/subSilver/images/spacer.gif) |
pasini
Joined: 12 Dec 2005 Posts: 50 Location: Curitiba - Brazil
|
|
Posted: Sun Nov 12, 2006 6:08 pm |
|
|
Hi,
I have faced some problems with multiple devices in the I2C bus (1 MCU + 1 I/O expander + 1 LCD driver). It worked fine with 1 device but not 2. Try removing the FORCE_HW from the #use I2C statment, it might work, altough it is not guaranteed because I didn't look the datasheet or errata.
Pasini |
|
![](templates/subSilver/images/spacer.gif) |
Tres Guest
|
Still not working |
Posted: Sun Nov 12, 2006 7:53 pm |
|
|
Thanks for the reply
but it's still not working |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Nov 13, 2006 7:34 am |
|
|
Maybe its your hardware. |
|
![](templates/subSilver/images/spacer.gif) |
DonWare
Joined: 18 Jan 2006 Posts: 43
|
Hardware suggestions - i2C |
Posted: Mon Nov 13, 2006 2:05 pm |
|
|
I'm just getting started on a similar project but with 6 processors.
Look into connecting each processor to the two bus lines using resistors and also pull each bus line to +5V using resistors.
Sorry I don't have any values. I think they are <200 ohms each.
I think I got the basic information from the Microchip site.
Good luck.
Don |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
Re: Hardware suggestions - i2C |
Posted: Mon Nov 13, 2006 2:30 pm |
|
|
DonWare wrote: | I'm just getting started on a similar project but with 6 processors.
Look into connecting each processor to the two bus lines using resistors and also pull each bus line to +5V using resistors.
Sorry I don't have any values. I think they are <200 ohms each.
I think I got the basic information from the Microchip site.
Good luck.
Don |
200 ohms is way too low. That's 25ma sink per pullup! |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Nov 13, 2006 2:50 pm |
|
|
We use 47 ohm series resistors with 47K pullups on each device. We also have a 6ms active pullup on the lines. On a large system we typically have more than 18 devices on the I2C bus. |
|
![](templates/subSilver/images/spacer.gif) |
DonWare
Joined: 18 Jan 2006 Posts: 43
|
|
Posted: Mon Nov 13, 2006 4:49 pm |
|
|
Makes sense. Yeah I didn't mean 200 ohm pullups Doh !
Thanks. |
|
![](templates/subSilver/images/spacer.gif) |
Tres Guest
|
|
Posted: Mon Nov 13, 2006 8:39 pm |
|
|
I already used 1k pullup already. Is it too low. |
|
![](templates/subSilver/images/spacer.gif) |
Tres Guest
|
|
Posted: Mon Nov 13, 2006 8:43 pm |
|
|
I forget to mention earlier. But before master get all 0xFF . it did received correct data from slave A once. |
|
![](templates/subSilver/images/spacer.gif) |
|