View previous topic :: View next topic |
Author |
Message |
JaSoN
Joined: 05 Jul 2006 Posts: 15
|
I2C network for multi-byte sending and receiving |
Posted: Wed Jul 05, 2006 3:33 am |
|
|
Hi, I am working with 3 PIC16F877 microcontrollers and I want to setup a I2C network between three boards. The network should be able to communicate continuously. So, the master PIC should do multi-byte I2C sending and reading to/from two slave PICs.
I have already developed the multi-byte master to slave sending program. But, when I try to do the multi-byte master to slave reading, it just can read one byte and the slave PIC lock the I2C network until I reset the PIC.
Please tell me how can I program the I2C network for multi-byte master to slave reading?
THANKS |
|
|
Guest
|
|
Posted: Wed Jul 05, 2006 8:22 am |
|
|
Well, like with so many other requests that have been made here, it's very difficult to tell you what's wrong with your program without seeing what's in it. It's obvious that something, in your Slave code, that's holding the bus captive. One thing that I've done is, in the Slave code, have the Status register ouput on one of the ports and connect LED's to display the word. Have it step through the code and see what's going on with the Slave and that might tell you where it's hanging. First, make the code as simple as possible. Get it working with the very Basics and then add on to that.
Ronald |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Jul 05, 2006 9:15 am |
|
|
When reading data from the slave, it will "stretch" the clock. That is hold the clock line low until you release the line in code. This is to ensure that you have loaded the data into the SSPBUF register. Read the section in the PIC datasheet. It talks about this. |
|
|
JaSoN
Joined: 05 Jul 2006 Posts: 15
|
|
Posted: Thu Jul 06, 2006 5:52 am |
|
|
Thanks!!
The problem have been solved by using i2c_read(0) in master PIC.
The program can read and write continuously
but, if I want to read 2-3 bytes from slave continuously, the master code can just read the first byte correctly, and the second byte is the same as the first one. I have checked the slave program, they have sent different value on different byte.
Why does the master program receive same value on different byte??
Since my code is a bit long
so I just show the I2C part in my code
MASTER:
I=0;
I2Creceive(slave_address);
delay_us(50);
I=1;
I2Creceive(slave_address);
void I2Creceive(int slave_address)
{
i2c_start();
i2c_write(slave_address+1);
I2C_receive[I]=i2c_read(0);
i2c_stop();
}
SLAVE I2C interrupt:
#INT_SSP
void I2C_TxRx()
{
i2c_write(I2C_send);
if(U==0)
{
I2C_send=distance;
U++;
}
else if(U==1)
{
I2C_send=SPEED;
U=0;
}
} |
|
|
Ttelmah Guest
|
|
Posted: Thu Jul 06, 2006 6:58 am |
|
|
There are a couple of comments.
First, the master must not start clocking back the reply, till the slave has had enough time to load it. It takes a significant time for the slave to enter the interrupt code...
Second, the slave, needs to distinguish the 'address' call, from the data calls. Look at the system function I2C_ISR_STATE, and the example 'ex_slave.c', to see how this can be used to tell why the ISR has been called.
I'd say your unchanging value, is probably time, with the slave not yet having loaded the reply, but you should also handle the call types, if the system is to stand any hope of remaining 'in sync' between the devices.
Best Wishes |
|
|
JaSoN
Joined: 05 Jul 2006 Posts: 15
|
|
Posted: Thu Jul 06, 2006 8:26 am |
|
|
THANKS!!
It works now.
The interrupt in the slave will be activated twice and the master just read the value at the first activation. |
|
|
domdom
Joined: 06 Sep 2006 Posts: 29
|
|
Posted: Wed Sep 06, 2006 7:28 pm |
|
|
JaSoN wrote: | THANKS!!
It works now.
The interrupt in the slave will be activated twice and the master just read the value at the first activation. |
Jason, can you teach me how you program your slave?
i am using 16f877a to be my master and send and receive data from other two 167877a...
i got no idea how to write the code !]
thanks! |
|
|
|