View previous topic :: View next topic |
Author |
Message |
JerryR
Joined: 07 Feb 2008 Posts: 167
|
if (TOUCHPAD_HIT()) is always true |
Posted: Thu Feb 09, 2017 3:23 pm |
|
|
Referencing the code below, it seems Touchpad_Hit() is always TRUE.
Code: |
while (TRUE)
{
if (TOUCHPAD_HIT())
Check_TouchPad();
} |
Works fine if I use:
Code: |
while (TRUE)
{
while (!TOUCHPAD_HIT())
{
Routines called when no key
}
Check_TouchPad();
}
} |
What up? Thanks group |
|
|
benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
|
Posted: Thu Feb 09, 2017 7:32 pm |
|
|
Well.... Your question is pretty vague... It all depends what the function Touchpad_Hit() does in the background!
Where does that function come from and how is the touchpad electrically connected and to what pins on the MCU?
Does it use any pull-ups or pull-downs on the rows or the columns?
You haven't provided any details, not much specific answer can be provided.
You also haven't provided your MCU model and your CCS version.
Ben |
|
|
JerryR
Joined: 07 Feb 2008 Posts: 167
|
|
Posted: Thu Feb 09, 2017 7:45 pm |
|
|
Ben:
My question is specific to the software function touchpad_hit(). The hardware (Target 16F1933) works well if I use "while (!touchpad_hit()) "method, but "if (touchpad_hit())" always evaluates as TRUE.
touchpad_hit() is a CCS built in function.
Compiler: PCWH 5.068
Target: PIC16F1933 |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Fri Feb 10, 2017 2:09 am |
|
|
JerryR wrote: |
My question is specific to the software function touchpad_hit(). The hardware (Target 16F1933) works well if I use "while (!touchpad_hit()) "method, but "if (touchpad_hit())" always evaluates as TRUE.
touchpad_hit() is a CCS built in function.
|
Careful. This is confusing compiler functionality. "if (touchpad_hit())" cannot be either true or false, it is a flow control statement, not a boolean expression. Neither does it evaluate to anything. It transfers control one way if the expression its given is true, and another if false. The expression in this case is, of course, touchpad_hit().
The if statement is so fundamental to C that if it doesn't work the whole implementation is dead. I feel pretty happy with stating that I am 99.999% sure that CCS implementation of if statements is good, and that the problem has nothing to do with the if.
The question is, as I think you meant, but didn't say, that touchpad_hit() is not giving you the results you expect. Things that spring to mind are: what does Check_Touchpad() do? Does it call touchpad_getc()? Are global interrupts enabled? Are you using timers 0 and/or 1 for anything in your code? Apparently, the touchpad code relies on timers and interrupts and won't work if they are not available. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Fri Feb 10, 2017 2:28 am |
|
|
Have you calibrated the touchpad?.
Understand that 'hit' returns TRUE, if the value coming from the touchpad, is more than 'threshold' percent above the calibration value. If the touchpad hasn't been calibrated, and returns (say) 20 counts all the time, then 'hit' can be permanently TRUE.
Look at the 'touchpad_state' entry in the manual.
Ideally you need to have a way of calibrating the pad, and making sure that isn't being touched when you do this. |
|
|
JerryR
Joined: 07 Feb 2008 Posts: 167
|
|
Posted: Fri Feb 10, 2017 7:26 am |
|
|
RF: Yes, sorry for the lack of explanation.
The line if touchpad_hit()), in code below, would suggest that the function touchpad_hit() returns a boolean. This is how I'm trying to implement my routines.
Ttelmah: Yes, I calibrate at the beginning of my code using touchpad_state(1). Note, again, everything works well when I implement my code by using while ( ! touchpad_hit() ).
I'm a bit puzzled.
From CCS example:
Code: |
// When the pad connected to PIN_B0 is activated, store the letter 'A'
#USE TOUCHPAD (PIN_B0='A')
void main(void){
char c;
enable_interrupts(GLOBAL);
while (TRUE) {
[b] if ( TOUCHPAD_HIT() ) [/b]
//wait until key on PIN_B0 is pressed
c = TOUCHPAD_GETC(); //get key that was pressed
} //c will get value 'A'
}
|
|
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Mon Feb 13, 2017 3:34 am |
|
|
See comments in code:
Code: |
while (TRUE) {
if ( TOUCHPAD_HIT() )
//wait until key on PIN_B0 is pressed - no, it doesn't wait, this checks
// to see if the touchpad needs servicing (i.e. reading) and if it does,
// then the next line reads it. Most of the time Touchpad_Hit() returns
// FALSE and only returns TRUE when a touch needs to be read,
// which is only now and then.
c = TOUCHPAD_GETC(); //get key that was pressed
// Do other stuff that the program needs to do: read ADCs, check for inputs, control things, etc.
}
|
|
|
|
|