View previous topic :: View next topic |
Author |
Message |
karlo
Joined: 07 Jan 2008 Posts: 10
|
while statement |
Posted: Mon Jan 14, 2008 8:22 am |
|
|
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)
|
|
Posted: Mon Jan 14, 2008 8:45 am |
|
|
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... |
|
|
Ttelmah Guest
|
|
Posted: Mon Jan 14, 2008 8:53 am |
|
|
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
|
|
Posted: Mon Jan 14, 2008 9:34 am |
|
|
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 |
|
|
Ttelmah Guest
|
|
Posted: Mon Jan 14, 2008 10:16 am |
|
|
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
|
|
Posted: Wed Jan 16, 2008 5:19 am |
|
|
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 |
|
|
|
|