CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

if (TOUCHPAD_HIT()) is always true

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
JerryR



Joined: 07 Feb 2008
Posts: 167

View user's profile Send private message

if (TOUCHPAD_HIT()) is always true
PostPosted: Thu Feb 09, 2017 3:23 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Feb 09, 2017 7:32 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Feb 09, 2017 7:45 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Feb 10, 2017 2:09 am     Reply with quote

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: 19539

View user's profile Send private message

PostPosted: Fri Feb 10, 2017 2:28 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Feb 10, 2017 7:26 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Feb 13, 2017 3:34 am     Reply with quote

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.
     }   
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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