PDA

View Full Version : Code for I2C Slave



Balachandar
- 21st November 2006, 06:39
I have been using PICs & PBP for the past 5 years and I consider this forum to be the best when it comes to providing useful tips, guidance and help.

My current issue : I am writing code in which the PIC should receive data through RS232 from one device and send it out through I2C to another device.

First I wrote the code in such a way that the PIC is the master. After having received the data (64 bytes) through RS232, it sends it to the other device through I2C. In this configuration, the PIC is the master and initiates I2C communication. The other device is the slave and keeps waiting for the data. This worked satisfactorily.

But I need to reverse the roles. For the I2C communication, the other device will be the master and whenever it queries, the PIC should send data to the master. How do I write I2C code in such a way that the PIC waits and sends data to the master whenever it asks for it? The PIC used is 16F688. In the software implementation of I2C in PBP, there are only two commands: I2CWRITE and I2CREAD and both seem to be those used by the master. In the code shown below, if the acknowledement signal is not received, it tries again and again. Will it work? (I cannot at the moment connect to the other device and check my code.)

GOTO START

NO_ACK:

START:
FOR Count = 0 to 63
I2CWRITE SDA,SCL,%10100000,0,DataOut[Count],NO_ACK
NEXT Count

END

Any help / guidance is highly appreciated.

Regards,
Bala

Melanie
- 21st November 2006, 08:14
Please do a forum SEARCH on 'I2CSlave' and you will eventually find links to code and examples to play with...

Balachandar
- 21st November 2006, 16:25
Thanks, Melanie.

But no luck. What I am looking for is a software implemtation of I2C for a slave that can be used in PICs like 16F688 that don't have a Synchronous Serial Port. All the links that I could find pertain to those PICs that have an SSP.

Regards,
Bala

BigWumpus
- 21st November 2006, 19:14
All my work with I2C shows me:

I2C-master could be done by hardware or software and speed doen't matter.

I2C-slave could only be done by hardware. The slave must react very fast on every bit....