View previous topic :: View next topic |
Author |
Message |
nikolasvr
Joined: 01 Jun 2011 Posts: 4
|
new user |
Posted: Wed Mar 21, 2012 6:40 am |
|
|
I am a new user and i would like to ask you that :
I have this code and i want to stuck the prog when it print the "LOW" or "HIGH" condition, not to running all the time. How can i do that?
Code: |
main()
{
while(1)
{
if (input(pin_A0)==0)
printf("\r\nLOW");
if (input(pin_A0)==1)
printf("\r\nHIGH");
}
} |
|
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Mar 21, 2012 7:11 am |
|
|
The while(1) is what makes it run continuously. However you need to keep the PIC running long enough to send the serial strings, so you can not stop the program right after the print statements.
You could remove the main while(1) loop and add a while(1) after each of the print statements. Then the PIC would boot, test the pin, print the appropriate message, and then stay in a loop forever. That sounds like what you want.
For better style you should look at using an if/else construction. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
nikolasvr
Joined: 01 Jun 2011 Posts: 4
|
|
Posted: Mon Mar 26, 2012 9:50 am |
|
|
I know about while(1), is it possible to have an example? |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
Re: new user |
Posted: Mon Mar 26, 2012 10:17 am |
|
|
Code: |
main()
{
if (input(pin_A0)==0)
{
printf("\r\nLOW");
while(1);
}
if (input(pin_A0)==1)
{
printf("\r\nHIGH");
while(1);
}
} |
Code: |
main()
{
if (input(pin_A0)==0)
{
printf("\r\nLOW");
while(1);
}
else
{
printf("\r\nHIGH");
while(1);
}
} |
_________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
nikolasvr
Joined: 01 Jun 2011 Posts: 4
|
|
Posted: Mon Mar 26, 2012 10:26 am |
|
|
I'm trying this code but when I change the state of input, it do nothing..... |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Mon Mar 26, 2012 11:17 am |
|
|
Code: | main()
{
while(1)// main loop
{
if(input(pin_A0) == 0)// if the input is LOW
{
printf("\r\nLOW");
while(input(pin_A0) == 0)// stay here if the input is still LOW
;
}
if(input(pin_A0) == 1)// if the input is HIGH
{
printf("\r\nHIGH");
while(input(pin_A0) == 1)// stay here if the input is still HIGH
;
}
}// end of while loop
}// end of main() |
|
|
|
nikolasvr
Joined: 01 Jun 2011 Posts: 4
|
|
Posted: Mon Mar 26, 2012 11:01 pm |
|
|
Thank you very much my friend!! |
|
|
|