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.
What do you mean? you think you can't use multiple slaves?
 

What do you mean? you think you can't use multiple slaves?
My current problem is two salve but do not have a solution,
you can fix the error for me??
 

Well I didn't follow all the code and I'm not familiar with I2C, but what I see is the following : you have 2 slaves, with 2 adresses. How did you get these adresses?
 

Well I didn't follow all the code and I'm not familiar with I2C, but what I see is the following : you have 2 slaves, with 2 adresses. How did you get these adresses?

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

Okay, now that you have the adresses, how does the communication work?
In OneWire, the master had to tell ALL the slaves that he is going to call ONLY ONE and the slave had to pay attention as the adress was being "spelled" by the master.

How does it work with your system? Does the master also tell the slaves to "listen" to the bus while he calls someone?
 
I don't know CCS C. Does this code in slave #use i2c(slave,sda=pin_c4,scl=pin_c3,address=0x10) sets the address for that slave PIC? If not, then it will not work.

Zip and post your latest project files and proteus files. I will check it later.
 

Okay, now that you have the adresses, how does the communication work?
In OneWire, the master had to tell ALL the slaves that he is going to call ONLY ONE and the slave had to pay attention as the adress was being "spelled" by the master.

How does it work with your system? Does the master also tell the slaves to "listen" to the bus while he calls someone?
that is, now I have to processing with the master???
how to call only one slave
 

You don't have a special command for that? a command which would allow you to tell the slaves that you are going to send an adress and not a command.

If you don't have that, have you got commands to put a slave to sleep or wake him up?
 

Slaves should have adresses. How are you setting the address of the slave? How does the master know which PIC has what address? What code are you using in slave to set its address?

The general call address is recognized (enabled) when
the General Call Enable (GCEN) bit is set (SSPCON2
register). Following a Start bit detect, eight bits are
shifted into the SSPSR and the address is compared
against the SSPADD. It is also compared to the general
call address and fixed in hardware.

Try SSPADD register of slaves with 0x10 and 0x40. It is the address register. Then you can address slaves from master using those address.
 
Last edited:

From what he told me, it seems that he can set the adresses himself (which is weird I must admit). The piece of code doing that is :

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

I2C write doesn't set the address of device. It writes to the I2C bus and if a I2C device exists on the bus then that address is checked with that slave device address.

I might be wrong. See this link. https://www.edaboard.com/threads/166753/

Try using 7 bit address for one slave and 10 bit address for another slave.

I think this will solve your problem. https://www.microchip.com/forums/m113057-print.aspx

It says between PICs you can use only Multi Master I2C Communication.
 
Last edited:

okay that's what I thought, then he must have a command to get the address from a slave right?

Connect only one slave, get that address, write it down, connect the other slave and do the same.
 

@Pictou: Please find the definition of the I2C protocol and read it. This is not 1-wire. There are no commands to go look for the addresses. The addresses are known by the programmer. For "dumb" parts (i.e. memory, A/D, etc.), the addresses are set in the chip and are available in the spec sheet. What Khanh is trying to do is program a PIC micro to become an I2C slave. The addresses should be set in the code which is listed in the first post:

#use i2c(slave,sda=pin_c4,scl=pin_c3,address=0x10) // Slave 1 code
#use i2c(slave,sda=pin_c4,scl=pin_c3,address=0x40) // Slave 2 code

The master code uses these addresses to comunicate to the slaves.

@Khanh: Have you tried the things I mentioned in post 18? Please go through the items in post 18 and let me know the results.

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.
 
spudboy see the link in my last post. It says only Multi master I2C communication is possible between PICs.

Try this method **broken link removed**

use

#use i2c(SLAVE, SDA=PIN_B4, SCL=PIN_B6, address=0xA0, FORCE_HW)
and
#use i2c(SLAVE, SDA=PIN_B4, SCL=PIN_B6, address=0xB0, FORCE_HW)

for the slaves

Try using these addresses from master while communicating with slaves. SCL and SDA Pin names should be changed accordingly.

See this link also http://www.best-microcontroller-projects.com/i2c-tutorial.html

I checked your simulation in LAN11.rar. None of the 2 slaves are sending data to master. It displays H0 = 255 and H1 = 255. I tried using both slaves and 1 slave at a time.


@khanh my Don't worry. I have found a solution to your problem. I will write a working code and post it tomorrow.
 
Last edited:
I didn't see anything in the previous links saying that only Multi-master I2C communications is possible between PICs. Multi-master would seem to be even more difficult to setup due to bus arbitration.

One thing I noticed in the master code in the first post is that the read_slave() routine was using the write address to read. Bit 0 should be low when writing and should be high when reading.

unsigned int read_slave(void) {
unsigned int dulieu;
i2c_start(1);
i2c_write(0x11); // <---- Slave address with bit 0 set for reading.
dulieu=i2c_read();
i2c_stop();
return dulieu;
}
 
@Khanh: Have you tried the things I mentioned in post 18? Please go through the items in post 18 and let me know the results.

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)
I tried but not, master dislay 255 @@
 

I have done the project and it is working. I am fine tuning the project (code). I wrote the code using CCS C. You can see the screenshots of the project working. When I test the working thoroughly I will post the complete project here.
 

Attachments

  • i2c_working.jpg
    i2c_working.jpg
    388.7 KB · Views: 118
  • i2c_w1.jpg
    i2c_w1.jpg
    320.5 KB · Views: 130
  • i2c_hang.jpg
    i2c_hang.jpg
    385 KB · Views: 109
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top