View unanswered posts | View active topics It is currently Thu Apr 17, 2014 11:45 am

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

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

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)



Thu Mar 13, 2008 6:18 am

Joined: Fri Apr 20, 2007 3:22 am
Posts: 36
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

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

you should be using
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
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
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
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.