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.

Master and multi Slaves problem I2C

Status
Not open for further replies.

khanh my

Junior Member level 2
Junior Member level 2
Joined
Mar 19, 2013
Messages
23
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,412
Hello,

I' using CCS C example of code for I2C communication between one Master and one Slave. It's work well.
However, i'm trying now with two Slaves, One Master and i have problem. 2 slave data transmission on the master was wrong (LCD of Master 255)

code master
Code:
#include <16f887.h>
#fuses   hs,nolvp,nowdt
#use     delay(clock=20000000)
#include <lcd_lib_4bit.c>
#use     i2c(master,sda=pin_c4,scl=pin_c3 )

#define  slave_add 0x10
#define  slave_add2 0x40
unsigned int doc,doc2;

void hienthi_lcd(void);
unsigned int read_slave(void);
unsigned int read_slave2(void);
void main() {
   set_tris_d(0x00);
   lcd_init();
   while(true){
      doc=read_slave();
      doc2=read_slave2();
      hienthi_lcd();
   }
}

unsigned int read_slave(void) {
   unsigned int dulieu;
   i2c_start(1); 
   i2c_write(0x10);
   dulieu=i2c_read();
   i2c_stop();
   return dulieu;
}
unsigned int read_slave2(void) {
   unsigned int dulieu2;
   i2c_start(1); 
   i2c_write(0x40);
   dulieu2=i2c_read(); 
   i2c_stop();
   return dulieu2;
}


void hienthi_lcd() {
   lcd_putcmd(line_1);
   lcd_putchar("MASTER");
   lcd_putcmd(line_2);
   printf(lcd_putchar,"H0=%3u H1=%3u",doc,doc2);
}

code slave1:

Code:
#include <16f887.h>
#fuses   hs,nolvp,nowdt
#device  adc=10
#use     delay(clock=20000000)
#use     i2c(slave,sda=pin_c4,scl=pin_c3,address=0x10)
#include <lcd_lib_4bit.c>

float doc;
unsigned int16 a;

void doc_adc(void);
void xuly_adc(void);
void hienthi_lcd(void);

#int_ssp
void ssp_interupt () {  
   if(i2c_isr_state()==0x80) {
      i2c_write((unsigned int)doc);
   }
}

void main (void) {
   set_tris_a(0xff);
   set_tris_d(0x00);
   
   lcd_init();
   
   setup_adc_ports(san0);
   setup_adc(adc_clock_internal); 
   
   enable_interrupts(int_ssp);
   clear_interrupt(int_ssp);
   enable_interrupts(global);
   
   while (true) {
      doc_adc();
      xuly_adc();
      hienthi_lcd ();
   }
}

void doc_adc () {
   set_adc_channel(0);
   delay_us(10);
   a=read_adc ();
}

void xuly_adc () {
   doc=((float)a*4.88)/10;
}

void hienthi_lcd () {
   lcd_putcmd(line_1);
   lcd_putchar("SLAVE 1");
   lcd_putcmd(line_2);
   printf(lcd_putchar,"H0=%3.0f",doc);
}

Code Slave 2:

Code:
#include <16f887.h>
#fuses   hs,nolvp,nowdt
#device  adc=10
#use     delay(clock=20000000)
#use     i2c(slave,sda=pin_c4,scl=pin_c3,address=0x40)
#include <lcd_lib_4bit.c>

float doc2;
unsigned int16 a2;

void doc_adc(void);
void xuly_adc(void);
void hienthi_lcd(void);

#int_ssp
void ssp_interupt () {  
   if(i2c_isr_state()==0x80) {
      i2c_write((unsigned int)doc2);
   }
}

void main (void) {
   set_tris_a(0xff);
   set_tris_d(0x00);
   
   lcd_init();
   
   setup_adc_ports(san0);
   setup_adc(adc_clock_internal); 
   
   enable_interrupts(int_ssp);
   clear_interrupt(int_ssp);
   enable_interrupts(global);
   
   while (true) {
      doc_adc();
      xuly_adc();
      hienthi_lcd ();
   }
}

void doc_adc () {
   set_adc_channel(0);
   delay_us(10);
   a2=read_adc ();
}

void xuly_adc () {
   doc2=((float)a2*4.88)/10;
}

void hienthi_lcd () {
   lcd_putcmd(line_1);
   lcd_putchar("SLAVE 2");
   lcd_putcmd(line_2);
   printf(lcd_putchar,"H1=%3.0f",doc2);
}

tks 4all

- - - Updated - - -

Here is the simulation file and code master, slave1,2
https://www.mediafire.com/?l6m73e5v4uny4bn
 

Check Slave 2 individually.
And see if it is working or not.

It will be better if you tell us what slave devices you are using.
 
Check Slave 2 individually.
And see if it is working or not.

It will be better if you tell us what slave devices you are using.
Hi! arunsharma0731
Thank you for replying
You can download the file to simulate of me, apparently slave2 a still active, it reads the temperature from LM35
 
Last edited:

Check Slave 2 individually.
And see if it is working or not.

It will be better if you tell us what slave devices you are using.
Could you check the mistake for me?
thanks a lot
 
Last edited:

no one know what this error?????
 

Is your I2C code working properly?

Connect master RB0 to slave1 RA5
Connect master RB1 to slave2 RA5

when sending data from slave1 to master then make RB0 of master high and RB1 of master low.
when sending data from slave2 to master then make RB0 of master low and RB1 of master high.

You can only select one device at a time for I2C Communication.
 
Last edited:

    Kwaku

    Points: 2
    Helpful Answer Positive Rating
You can only select one device at a time for I2C Communication.

I believe this is incorrect. As long as the 2 slave devices have different addresses, and it looks like they do from the I2C setup call in the slave code, they can both be on the same bus. I've used multiple slave devices on a single bus but they were "dumb" peripherals. There may be some additional setup required for the slave PIC16F887 parts.
 
@spudboy488

addresses 0x10 and 0x40 is defined for slaves 1 and 2. In 2C write routine 0x10 and 0x40 address are used but how does I2C know which address is for which slave. Doesn't it write 0x10 to both slaves and then 0x40 to both slaves?
 

The first byte sent by the master after a start condition is the address byte. All slaves listen to the address and only the slave that has the matching address will send and ACK. The other slaves ignore all bus traffic until the bus goes to a stop condition. Then they all wait for a start condition again. At least that is the way it is supposed to work.
 

The first byte sent by the master after a start condition is the address byte. All slaves listen to the address and only the slave that has the matching address will send and ACK. The other slaves ignore all bus traffic until the bus goes to a stop condition. Then they all wait for a start condition again. At least that is the way it is supposed to work.
@spudboy488:
There may be some additional setup required for the slave PIC16F887 parts
you can tell the device here? I've tried jayanth.devarayanadurga but failed
tks 4all
 

Where is the device address for the slaves set in this project. The slaves are PIC MCUs. Do PIC MCU comes with different addresses?
you can download my file and fix the error help me?
thanks you very much
P/s: I'm from vietnam =>> I'm not good English
 

Read page 185 and 186 of datasheet. You have to use SS pin to select slave i.e., You have to make SS pin of slave high. Remove one slave and use just one slave and see if I2C is working between master and one slave. You are writing some slave address from master i.e., 0x10 and 0x40. Where did you get those values from?

url]http://www.mikroe.com/forum/viewtopic.php?t=8106&highlight=i2c+slave[/url]
http://www.mikroe.com/forum/viewtopic.php?t=20543

CCS C code
**broken link removed**

Hope these helps. Try with one master and one slave. See if it works.
 
Last edited:
Read page 185 and 186 of datasheet. You have to use SS pin to select slave i.e., You have to make SS pin of slave high. Remove one slave and use just one slave and see if I2C is working between master and one slave. You are writing some slave address from master i.e., 0x10 and 0x40. Where did you get those values from?

url]http://www.mikroe.com/forum/viewtopic.php?t=8106&highlight=i2c+slave[/url]
http://www.mikroe.com/forum/viewtopic.php?t=20543

CCS C code
**broken link removed**

Hope these helps. Try with one master and one slave. See if it works.

?????? I said in the topic
Hello,

I' using CCS C example of code for I2C communication between one Master and one Slave. It's work well.
However, i'm trying now with two Slaves, One Master and i have problem. 2 slave data transmission on the master was wrong (LCD of Master 255)

code master
 

Ofcourse it will be wrong. Only the slave addressed by master does the communication. Read the datasheet and the examples in the link I have given. see what address they are using for the slave. I don't know how to set the address of the slave when slave is a PIC mcu.
 
If you look at the slave code in the original post, you can see the type of MCU from "#include <16f887.h>". I'm asssuming this is correct.

I don't use CCS but, again, I assume that slave1 is set to address 0x10 by this line: #use i2c(slave,sda=pin_c4,scl=pin_c3,address=0x10)

Slave2 is set to 0x40 by this line: #use i2c(slave,sda=pin_c4,scl=pin_c3,address=0x40)

For the slave I2C setup, you may want to try the FORCE_HW option. The slaves may require it.

#use i2c(slave,sda=pin_c4,scl=pin_c3,address=0x10, FORCE_HW)
#use i2c(slave,sda=pin_c4,scl=pin_c3,address=0x40, FORCE_HW)

You say you can get one slave to work on its own. Can you get the second slave to work on its own using the code that you loaded into the first slave? This will check the hardware of the second slave. Once you verify that it works, reprogram it but change the slave address. The two slave modules can not have the same address. Then connect only slave 2 (with the changed address) and test. If it works, connect slave 1 (with the original address) and test.

Also, the SS pin is for SPI mode, not I2C. It should have nothing to do with I2C communication.
 
Last edited:
@spudboy Are you saying that if 0x10 and 0x40 is written then it will become addresses of PIC slave 1 and PIC Slave 2? When both slaves PICs are connected to the same I2C bus then when you write i2c address 0x10 or 0x40, to which slave will it get written?

Ok. I see address being set for the slaves in the slave codes.

See this

In Slave mode, the SCL and SDA pins must be
configured as inputs (TRISC<4:3> set). The MSSP
module will override the input state with the output data
when required (slave-transmitter).

You might set tris of port c pins 3 and 4 as input pins.

use set_tris_c(0x18); in the slaves.
 
Last edited:
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top