Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] I2C Communication between multiple pic

Status
Not open for further replies.

easy electronics

Member level 3
Joined
Mar 3, 2014
Messages
63
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
485
hi.
i need an example to communicate between 8 pics as slave and one as master.
Every slave have an led light and i want to make turn on or off from MASTER instruction.

i want to use pic16f877a microcontroller for all (master and slaves).
I have little bit practice on CCS compiler if any body have this code then it well be very help full for me .
otherwise any code will be help full for me. i will translate it.

Any guidance please.:-D :fight: :sad: :razz: :bang:
 

connect 'em all to a bus and have the master initiate all comms by giving the code of the slave it wants to talk to.........the slaves are only allowed to speak when told to, and only the one slave.
The bus will be single wire......and have pullups going to it from each pic.
I think you should be in the micro forum though.....
 

For the slave, you have to reserve pins of uC that are able to deal with I/O interrupt, otherwise will waste a lot of time at polling routine, or will need to downgrade communication speed.
 

thanks treez and thanks ander for your reply .
https://www.ccsinfo.com/forum/viewtopic.php?p=28651
i have tried these codes
MASTER
Code:
/* Standard Include for 16F877 Chip */ 
#include <16F877.h> 
#fuses XT,NOWDT,NOPROTECT,NOLVP,PUT 

/* Delay for 4 mhz crystal */ 
#use delay (clock=4000000) 

/* Setup RS232 */ 
#use rs232(baud=9600, xmit=PIN_D4,rcv=PIN_D5, INVERT) 

/* Setup I2C */ 
#use I2C(MASTER, sda=PIN_C4, scl=PIN_C3, SLOW) 

main() 
{ 
   int8 i2c_command = 66; 

   while (true) 
   { 
      delay_ms(1000); 
      printf("Outputting: %c", i2c_command); 
      
      /* Master */ 
      i2c_start();      // Start condition 
      i2c_write(0xa0);   // Device address 
      i2c_write(i2c_command);   // Write Command 
      i2c_stop();      // Stop condition 

   } 
}

Code:
/* Standard Include for 16F877 Chip */ 
#include <16F877.h> 
#fuses XT,NOWDT,NOPROTECT,NOLVP,PUT 

/* Delay for 4 mhz crystal */ 
#use delay (clock=4000000) 

/* Setup RS232 */ 
#use rs232(baud=9600, xmit=PIN_D4,rcv=PIN_D5, INVERT) 

/* Setup I2C */ 
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0xa0, SLOW, FORCE_HW) 

#INT_SSP 
void ssp_interupt () 
{ 
   byte incoming; 
   incoming = i2c_read(); 
   printf("\n\rRead byte 1: %x\n\r", incoming); 
   incoming = i2c_read(); 
   printf("Read byte 2: %x\n\r\n\r", incoming); 
} 


main() 
{ 
      delay_ms(1000); 
      printf("Running"); 

      enable_interrupts(GLOBAL); 
      enable_interrupts(INT_SSP); 
      while (true) 
   { 

   } 
}

i have tried these codes between 2 pics but when i try to add 3rd code with different address it behaves randomly.
 

Writing to serial output during I2C slave interrupt is completely wrong. It's likely to cause receiver overflow and data loss.
 

Problem is solved in only one way com
here is master code
Code:
/* Standard Include for 16F877 Chip */ 
#include <18F4550.h> 
#fuses MCLR,hs,NOWDT,NOPROTECT,NOLVP,PUT 

#use delay (clock=20000000) 

#use rs232(baud=9600, xmit=PIN_c6,rcv=PIN_c7,) 

#use I2C(MASTER, sda=PIN_B0, scl=PIN_B1,fast) 
#define add1 0XA0
#define add2 0XB0
  
void send_byte(int address,int data)
{ 
      i2c_start();      // Start condition 
      i2c_write(address);   // Device address  
      i2c_write(data);    // Write Command 
      i2c_stop();       // Stop condition 
delay_ms(100);
}
////////////////
void main()
{   
for(int i=0;i<50;i++) 
{
output_TOGGLE(pin_b7);
delay_ms(100);
}
      WHILE(1)  
      { 
      send_byte(add1,'1');delay_ms(500);      printf("AAAAAAA\\n\r");output_toggle(pin_b7);
      send_byte(add1,'0');delay_ms(500);      printf("AAAAAAA\\n\r");output_toggle(pin_b7);
      send_byte(add2,'1');delay_ms(500);      printf("BBBBBBB\\n\r");output_toggle(pin_b7);
      send_byte(add2,'0');delay_ms(500);      printf("BBBBBBB\\n\r");output_toggle(pin_b7);
      }
}

Code for slave 1

Code:
#include <16f877a.h>
#fuses hs,NOWDT,NOPROTECT,NOLVP,PUT
/* Delay for 4 mhz crystal */ 
#use delay (clock=20000000) 

#use rs232(baud=9600, xmit=PIN_c6,rcv=PIN_c7) 
#define  add 0xA0
/* Setup I2C */ 
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=add, FAST, FORCE_HW) 
int1 rx_flag=0;
char rx_data=0;
#INT_SSP 
void ssp_interupt () 
{ 
   int x;
   x= i2c_read();
   printf("%u\n\r"x);
   if(x== add)
   {
      rx_data = i2c_read();
      rx_flag=1;
   if(rx_data=='1')      output_high(pin_b7);
   else if(rx_data=='0') output_low(pin_b7);
   }
}
///////////////////////////////////////////////////////////////////////////////
void main() 
{ 
      delay_ms(100); 
      printf("Running"); 
      enable_interrupts(GLOBAL); 
      enable_interrupts(INT_SSP); 
      while (true) 
   { 
      if (rx_flag==1)
        {
          rx_flag=0;
          printf("%c\n\r"rx_data);
        }
   }
}
///////////////////////////////////////////////////////////////////////////////

code for slave 2

Code:
#include <16f877a.h>
#fuses hs,NOWDT,NOPROTECT,NOLVP,PUT
/* Delay for 4 mhz crystal */ 
#use delay (clock=20000000) 

#use rs232(baud=9600, xmit=PIN_c6,rcv=PIN_c7) 
#define  add 0xB0
/* Setup I2C */ 
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=add, FAST, FORCE_HW) 
int1 rx_flag=0;
char rx_data=0;
#INT_SSP 
void ssp_interupt () 
{ 
   int x;
   x= i2c_read();
   printf("%u\n\r"x);
   if(x== add)
   {
      rx_data = i2c_read();
      rx_flag=1;
   if(rx_data=='1')      output_high(pin_b7);
   else if(rx_data=='0') output_low(pin_b7);
   }
}
///////////////////////////////////////////////////////////////////////////////
void main() 
{ 
      delay_ms(100); 
      printf("Running"); 
      enable_interrupts(GLOBAL); 
      enable_interrupts(INT_SSP); 
      while (true) 
   { 
      if (rx_flag==1)
        {
          rx_flag=0;
          printf("%c\n\r"rx_data);
        }
   }
}
///////////////////////////////////////////////////////////////////////////////
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top