|
|
View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
Problem with WHILE |
Posted: Fri Dec 16, 2011 8:52 am |
|
|
Hi, someone can tell me what is my mistake!!!, why not leave the conditional (do..while), when any of the two conditions is true ...
but now, just let this conditional only when two conditions are true ...
Code: | #include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,NOSTVREN
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, ERRORS)// RS232 Estándar
#define RESETX 0
char Estado = RESETX;
int8 k;
#define Program PIN_B4
void main()
{
while (TRUE) {
k = 1;
Estado = 1;
do{
if (Estado == 1) {
k++;
}
} while ((input(Program)!=0)||(k < 5));
}
} |
|
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1635 Location: Perth, Australia
|
Re: Problem with WHILE |
Posted: Fri Dec 16, 2011 1:37 pm |
|
|
pilar wrote: | Hi, someone can tell me what is my mistake!!!, why not leave the conditional (do..while), when any of the two conditions is true ...
but now, just let this conditional only when two conditions are true ...
Code: | #include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,NOSTVREN
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, ERRORS)// RS232 Estándar
#define RESETX 0
char Estado = RESETX;
int8 k;
#define Program PIN_B4
void main()
{
while (TRUE) {
k = 1;
Estado = 1;
do{
if (Estado == 1) {
k++;
}
} while ((input(Program)!=0)||(k < 5)); <<-- *** WRONG LINE, move to the brace above
}
} |
|
_________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Fri Dec 16, 2011 3:48 pm |
|
|
No, it is just awfully laid out, so it _looks_ like it is on the wrong line....
Code: |
void main() {
while (TRUE) {
k = 1;
Estado = 1;
do {
if (Estado == 1) {
k++;
}
} while ((input(Program)!=0)||(k < 5));
}
}
|
However how would you ever know it had left the do...while?. As soon as it leaves, it'll execute the outer while loop, and start again.
Big question though is do you really want to use 'OR'?. The loop will be done, while pin B4 is high _OR_ k is less than 5. So the 'exit condition', is B4 being low _AND_ k>=5.
Best Wishes |
|
|
|
|
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
|