View unanswered posts | View active topics It is currently Sun Nov 23, 2014 5:08 am






Reply to topic  [ 3 posts ] 
help understanding code for PCF8574AP mindsensors interface 
Author Message
Rookie

Joined: Tue Mar 11, 2008 7:12 pm
Posts: 5
Post help understanding code for PCF8574AP mindsensors interface
HELLO

Could anyone tell me what these code below are suppose to mean and what they do plz

my programming skills are rather poor


while (nI2CStatus[S1] == STAT_COMM_PENDING)

if (nI2CStatus[S1] == NO_ERR)


thanks

_________________
Ghat


Thu Mar 13, 2008 6:18 am
Profile
Rookie

Joined: Fri Apr 20, 2007 3:22 am
Posts: 36
Post 
When you are send or getting data to or from an i2c device, it takes time for the data to come across. You have to wait untill the comuninication is done before reading the information into your return buffer.

" while (nI2CStatus[S1] == STAT_COMM_PENDING) " waits until that communication is done.



"if (nI2CStatus[S1] == NO_ERR)"
will only execute the the next pice of code if there is no error on the i2c bus.


Thu Mar 13, 2008 2:37 pm
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post 
Instead of
Code:
while (nI2CStatus[S1] == STAT_COMM_PENDING)

you should be using
Code:
while (nI2CStatus[S1] == STAT_COMM_PENDING)
{}

The "while" in the first clause applies to the next statement. The second version adds an empty statement (or block). An aternative for the "{}" is to use a ";" as empty statement as in
Code:
while (nI2CStatus[S1] == STAT_COMM_PENDING);

// same code but on two lines for better visibility

while (nI2CStatus[S1] == STAT_COMM_PENDING)
  ;

Leaving out the ":" or "{}" can be a difficult bug to detect!

If you are writing a multi-tasking program (i.e. doing two tasks at once), then you want to use the following which puts in a short delay and prevents the code from sucking up to much CPU time
Code:
while (nI2CStatus[S1] == STAT_COMM_PENDING)
{
  wait1Msec(2); // short wait so don't waste CPU time that could
                // go to other tasks.
}


Sat Mar 15, 2008 11:11 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


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 post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.