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 CCS Technical Support

I2C network for multi-byte sending and receiving

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
JaSoN



Joined: 05 Jul 2006
Posts: 15

View user's profile Send private message

I2C network for multi-byte sending and receiving
PostPosted: Wed Jul 05, 2006 3:33 am     Reply with quote

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








PostPosted: Wed Jul 05, 2006 8:22 am     Reply with quote

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

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

PostPosted: Wed Jul 05, 2006 9:15 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jul 06, 2006 5:52 am     Reply with quote

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







PostPosted: Thu Jul 06, 2006 6:58 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jul 06, 2006 8:26 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Sep 06, 2006 7:28 pm     Reply with quote

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!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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