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

while statement

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



Joined: 07 Jan 2008
Posts: 10

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

while statement
PostPosted: Mon Jan 14, 2008 8:22 am     Reply with quote

Hello,

I'm a beginner in programming PIC controllers in C, it's a little different the assembly language.

I made a loop with while statement
[code]
int8 count ;

while (count < 3 && PIR1,0 !=1) // no timer1 interrupt detected
{

count++ ;
}
//here the rest of the program//
When i simulate this loop, i'm stuck in this loop even when the condition is false count > 3 it still counts.
I've noticed that the green pointer of MPLAB SIM (when i the loop) never go next to the while statement and propably never check the condition.

Is this normal?
Shouldn't the condition been checked every loop within the statement?

Thanks
D-Kens



Joined: 09 May 2005
Posts: 35
Location: Toulouse (France)

View user's profile Send private message MSN Messenger

PostPosted: Mon Jan 14, 2008 8:45 am     Reply with quote

Try giving count an initial value at declaration, first ?
Then maybe put both of your conditions between brackets ?
Code:
int8 count=0 ;
while ((count<3) && (PIR1,0 !=1))
{
count++ ;
}

At first, that's what I'd try... Surprised
Ttelmah
Guest







PostPosted: Mon Jan 14, 2008 8:53 am     Reply with quote

The second part of the line doesn't make any sense...
You can't use
PIR1,0
To access bit 0 of the PIR register. You either have to use a #bit statement to declare this, or use the 'bit_test' function.
The compiler does not complain about this, but produces no code for the invalid part of the statement. I'd presume this 'null code', is destroying the logic of the statement...

Best Wishes
SET



Joined: 15 Nov 2005
Posts: 161
Location: Glasgow, UK

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Mon Jan 14, 2008 9:34 am     Reply with quote

Quote:
Code:
while (count < 3 && PIR1,0 !=1) //


I think this means:
1. evaluate count<3 && PIR1, and throw the result away
2. evaluate 0 != 1, result is True, so never exit the While loop

Comma operator Smile
Ttelmah
Guest







PostPosted: Mon Jan 14, 2008 10:16 am     Reply with quote

Yes.
The compiler is throwing away the second part of the line, since '0!=1', simply evaluates as true all the time. Hence it optimises it away, and makes the loop permanent.
Not what is meant at all.
Good 'spot'...

Best Wishes
karlo



Joined: 07 Jan 2008
Posts: 10

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

PostPosted: Wed Jan 16, 2008 5:19 am     Reply with quote

Hello,

You guy's are right.

The bit_test on the PIR1,0 !=1 ensures that the condition is checked and will stop the while loop if the conditiond are met.
This is what i did:


Code:
while((count < 3) && (bit_test(PIR1,0) != 1))

THx

Karlo
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