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

I2C with 3 PICs
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Tres
Guest







I2C with 3 PICs
PostPosted: Fri Nov 10, 2006 11:51 am     Reply with quote

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);
         }

}
Tres
Guest







One more file
PostPosted: Fri Nov 10, 2006 11:53 am     Reply with quote

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]
Ttelmah
Guest







PostPosted: Fri Nov 10, 2006 4:08 pm     Reply with quote

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
Tres
Guest







Still not working
PostPosted: Sat Nov 11, 2006 1:21 am     Reply with quote

it's still not working. i'm still receiving FF.
pasini



Joined: 12 Dec 2005
Posts: 50
Location: Curitiba - Brazil

View user's profile Send private message Send e-mail

PostPosted: Sun Nov 12, 2006 6:05 pm     Reply with quote

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
pasini



Joined: 12 Dec 2005
Posts: 50
Location: Curitiba - Brazil

View user's profile Send private message Send e-mail

PostPosted: Sun Nov 12, 2006 6:08 pm     Reply with quote

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
Tres
Guest







Still not working
PostPosted: Sun Nov 12, 2006 7:53 pm     Reply with quote

Thanks for the reply

but it's still not working
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Nov 13, 2006 7:34 am     Reply with quote

Maybe its your hardware.
DonWare



Joined: 18 Jan 2006
Posts: 43

View user's profile Send private message

Hardware suggestions - i2C
PostPosted: Mon Nov 13, 2006 2:05 pm     Reply with quote

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
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

Re: Hardware suggestions - i2C
PostPosted: Mon Nov 13, 2006 2:30 pm     Reply with quote

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!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 13, 2006 2:46 pm     Reply with quote

He means the input series resistors for each i2c chip (not the pullups).
These are referred to as "Rs" on pages 20 and 21 of the spec:
http://www.standardics.nxp.com/literature/books/i2c/pdf/i2c.bus.specification.pdf
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Nov 13, 2006 2:50 pm     Reply with quote

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.
DonWare



Joined: 18 Jan 2006
Posts: 43

View user's profile Send private message

PostPosted: Mon Nov 13, 2006 4:49 pm     Reply with quote

Makes sense. Yeah I didn't mean 200 ohm pullups Doh !

Thanks.
Tres
Guest







PostPosted: Mon Nov 13, 2006 8:39 pm     Reply with quote

I already used 1k pullup already. Is it too low.
Tres
Guest







PostPosted: Mon Nov 13, 2006 8:43 pm     Reply with quote

I forget to mention earlier. But before master get all 0xFF . it did received correct data from slave A once.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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