Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 11th August 2007, 06:57 PM   (permalink)
Default Pic I2C SSP Code

Have been playing with some Pic I2c Master to Pic Slave code. When the Pic master sends a read to the slave, a peripheral interrupt flag is set and an ISR is started to feed the slave SSPBUF with data. Meanwhile, the slave main code is constantly updating the registers at a regular interval.

The problem arises when eventually the master and slave become out of sync, with new data corrupting a previous array of data to be sent. I am new to interrupts, so not sure how to go about some code that might keep the the master and slave in sync, or avoiding a slave write while the master is reading.

Maybe the answer is very careful delays being set forth, in both the master and slave units, so collision is avoided? Thanks for any ideas.

P.S. The Pic slave is sharing the master Vdd, Vss, and have seperate, but equal 20mhz oscillators. Also, the slave is attached by about a foot of I2C wire.
nickelflippr is online now   Reply With Quote
Old 11th August 2007, 08:16 PM   (permalink)
3v0
Default

In this case it is a bad idea to read and write from the same data array.

There are a few ways you can fix it.

1. Do not update during a read request.

2. When the slave gets a read request from the master, stop the update process after the current update finishes for just long enough to make a copy of the data to be sent. Let the update run and send the requested data from the copy.

3. Go with a double buffer setup. You have to figure out when it is best/safe to swap the buffers.

Depends on the application. More specifically the nature of the data.
3v0 is offline   Reply With Quote
Old 12th August 2007, 12:02 AM   (permalink)
Default

I appreciate the good ideas there on ways to stop collision.

Tried the double buffer idea with table data change right after first slave write, while holding off the master read long enough to get this done. Went along pretty good for awhile, but still ended up with some corruption, hopefully this isn't a hardware problem creeping in.

Looks like I,m going to have set and clear a register upon entry into and out of the interrupt, then have the slave check that, so that a write won't occur while reading. Also in the same way, will have to check against the slave writing data, to make sure a read won't occur while writing. Tried this and when a deliberate collision course was set, the code locked up, so more work required on this angle.

Another thought would be to call for the data upon initiation of the interrupt, although I have seen many reminders on this forum to keep intterupt routines short. So how short is short?

Right now, the data from the slave is strictly an excersise in using and viewing the Pic I2C Master to Pic Slave relationship/code.
nickelflippr is online now   Reply With Quote
Old 17th August 2007, 03:01 PM   (permalink)
Default

Just a quick update. Taking the sizeable delay out of Main for the slave was a big help. Also buffering the interrupt value just prior to the slave read (in the case of a master write anyways) proved a winning combination.
nickelflippr is online now   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
need help modifying some pic ASM code justDIY Micro Controllers 17 16th July 2007 11:41 AM
I have error with Pic Code & Need help admain Micro Controllers 0 15th April 2007 06:06 AM
Newcomers, please read! (PIC regarded) Upd. 0xD Jay.slovak Micro Controllers 0 17th April 2005 01:04 PM
Tough assembly program for the PIC16F84 asmpic Micro Controllers 34 3rd December 2004 06:50 PM
An error in pic16f84a, why? Zener_Diode Micro Controllers 6 11th April 2004 02:55 AM



All times are GMT. The time now is 04:45 AM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.