View previous topic :: View next topic |
Author |
Message |
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
[SOLVED] Input loop? |
Posted: Sun Oct 19, 2014 2:10 pm |
|
|
Dear CCS fanatics
Take a look at this code...
This is in the main loop
Code: |
if (waitforbuttoninput)
{
if (!input(PIN_C2))
{
putc('.');
waitforbuttoninput=0;
}
}
|
Why when I set the waitforbuttoninput to true it read correctly and putchar . character over serial BUT never stop listening for input??
If I keep pressing the switch I still get dot characters but it should not since I've set the flag to 0...
Any idea??
Would a break statement inserted after waitforbuttoninput=0; help?
I know this is for exiting a while,switch and for loop...
EDIT (For clarifying):
Code to enable reading of the switch which works great (to receive from serial)
Code: |
case 'Y' : waitforbuttoninput = 1; // read start button and flash
break;
case 'y' : waitforbuttoninput = 0;
break;
|
Using pcwhd 5.027 on a PIC18F4685 _________________ Regards,
Laurent
-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
Last edited by ELCouz on Sun Oct 19, 2014 5:37 pm; edited 1 time in total |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun Oct 19, 2014 3:00 pm |
|
|
Post SHORT but complete, compilable code which shows us exactly what your problem is.
(Save us from having to second guess.)
Mike |
|
|
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
|
Posted: Sun Oct 19, 2014 3:06 pm |
|
|
I don't know what more you can see but here it is...
Code: | #include <18F4685.h>
#FUSES HS,NOMCLR,NOLVP,NOPBADEN,BROWNOUT,BORV46,PUT
#use delay(clock=12M)
#use rs232(baud=9600, xmit=PIN_C6,rcv=PIN_C7,errors)
int8 dummy;
int1 waitforbuttoninput;
#int_rda
void serial_isr()
{
dummy=getc();
}
void main()
{
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
while(1)
{
switch (dummy)
{
case 'Y' : waitforbuttoninput = 1; // read start button
break;
case 'y' : waitforbuttoninput = 0;
break;
}
if (waitforbuttoninput)
{
if (!input(PIN_C2))
{
putc('.');
waitforbuttoninput=0;
}
}
}
} |
_________________ Regards,
Laurent
-----------
Here's my first visual theme for the CCS C Compiler. Enjoy! |
|
|
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
|
Posted: Sun Oct 19, 2014 4:22 pm |
|
|
Code: |
if (waitforbuttoninput)
{
if ((!input(PIN_C2))&&(waitforbuttoninput))
{
waitforbuttoninput=0;
putc('.');
}
}
|
Even this doesn't work the int1 waitforbuttoninput isn't set to 0 or the IF statement ignore the 0 value...
Shouldn't the pic check if waitforbuttoninput is set to 1 before reading the input? _________________ Regards,
Laurent
-----------
Here's my first visual theme for the CCS C Compiler. Enjoy! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Oct 19, 2014 5:01 pm |
|
|
Quote: | Shouldn't the pic check if waitforbuttoninput is set to 1 before reading the input? |
You press a key and 'dummy' gets set to 'Y'. Once it's set, it stays that
way forever (unless you press a different key). You never clear 'dummy'.
You crank through your while() loop, it sees the 'Y', it sets
'waitforbuttoninput' to 1. It's because you never clear 'dummy'. |
|
|
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
|
Posted: Sun Oct 19, 2014 5:28 pm |
|
|
PCM programmer wrote: | Quote: | Shouldn't the pic check if waitforbuttoninput is set to 1 before reading the input? |
You press a key and 'dummy' gets set to 'Y'. Once it's set, it stays that
way forever (unless you press a different key). You never clear 'dummy'.
You crank through your while() loop, it sees the 'Y', it sets
'waitforbuttoninput' to 1. It's because you never clear 'dummy'. |
You are right!
I've modified my working code too fast. Clearly skipped that...
Code: |
.....
case 'x' :output_low(pin_a0);
dummy = 0;
break;
case '?' :printf("$!*"); //keep alive
savedcount = count;
dummy = 0;
break;
case '@' :printf("$%s*",version); // version info
dummy = 0;
break;
case 'Y' : waitforbuttoninput = 1; // read start button and flash
break;
case 'y' : waitforbuttoninput = 0;
break;
}
|
You could see its been there before but managed to forget to add this...
Thanks for pointing me out PCM Programmer! _________________ Regards,
Laurent
-----------
Here's my first visual theme for the CCS C Compiler. Enjoy! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Mon Oct 20, 2014 12:40 am |
|
|
You can also see 'why' PCM_programmer asked for complete code. The problem was actually 'outside' the bit you were looking at.... |
|
|
|