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

Debounce switch problem

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



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

Debounce switch problem
PostPosted: Tue Sep 08, 2015 12:28 am     Reply with quote

Hi, i'm trying to make a simple debounce switch Function. But the problem is, sometimes it works but sometimes it's not working. What's wrong with my code:
Code:




#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(c20000000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <stdio.h>
#include <string.h>
 
void main()
{


while(true)
  {

             If(!input(PIN_B4)
                       {   output_high(PIN_D0);
                                 If(!input(PIN_B4))
                                         {   output_low(PIN_D0);   }
                         }
       
    }

}

temtronic



Joined: 01 Jul 2010
Posts: 9228
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Sep 08, 2015 5:19 am     Reply with quote

this.....

#use delay(c20000000000)

isn't nice...

I suspect you didn't cut and paste your whole program here, rather typed it in ?

Also what is the value for the pullup resistor on PIN_B4 ?

And, any capacitor across the switch contacts ?

Finally, you should add 'ERRORS' in #USE RS232(....options...). Though not used now, it'll keep the HW UART from 'locking' on you .

Jay
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Tue Sep 08, 2015 5:31 am     Reply with quote

Ok, i change my code. My pullup resistor is 10k and no capacitor across switch.what should i do next?


Code:

#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,UART1,ERRORS)
#include <stdio.h>
#include <string.h>
 
void main()
{


while(true)
  {

             If(!input(PIN_B4)
                       {   output_high(PIN_D0);
                                 If(!input(PIN_B4))
                                         {   output_low(PIN_D0);   }
                         }
       
    }

}
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Tue Sep 08, 2015 6:32 am     Reply with quote

I have to ask where you think the debounce is?.

Bounce takes _time_. Typically switches oscillate at a few KHz, over several mSec. You are reading the switch twice, but within only a handful of instructions of one another. As such almost no effect on bounce....

Have a look in the code library. There is a set of routines there for scanning a keypad, including debounce.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Tue Sep 08, 2015 7:43 am     Reply with quote

Dear ttelmah,

Is there is any example that i can used for referrence? Where can i get the library code?
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Sep 08, 2015 9:01 am     Reply with quote

search the forum

here is one well coded example by a pro

http://www.ccsinfo.com/forum/viewtopic.php?t=23837&highlight=debounce
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Tue Sep 08, 2015 9:05 am     Reply with quote

Also the 'code library', is from the forum index here. Has tens of thousands of routines, some of which are the 'bread and butter' of coding for the PIC with CCS.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Tue Sep 08, 2015 11:28 pm     Reply with quote

Hi ,
I just want to make clear of my problem. Actually i want to make output D0 will remain high when i push button switch B4. And then output D0 will become low when i push button switch B4 once again. Maybe 'debounce problem' title is not suitable here. Please correct me.
jeremiah



Joined: 20 Jul 2010
Posts: 1349

View user's profile Send private message

PostPosted: Wed Sep 09, 2015 7:35 am     Reply with quote

Debounce is still 100% applicable here. You need to get your debounce right before you can handle the logic of toggling your output the way you want.
Sam_40



Joined: 07 Jan 2015
Posts: 127

View user's profile Send private message

PostPosted: Wed Sep 09, 2015 8:18 am     Reply with quote

art,
Try this, FYI; no debounce in your code or the one below:
Code:

#include <18F452.h>
#fuses NOWDT,NOPROTECT,NOLVP
#use delay(clock=20M)
 
void main()
{
   while(true)
   {
      If(!input(PIN_B4)
      {   
         output_high(PIN_D0);
         delay_ms(250);
 
      }
      If(!input(PIN_B4))
      {   
         output_low(PIN_D0);
         delay_ms(250);   
      }
   }
}


Last edited by Sam_40 on Wed Sep 09, 2015 5:36 pm; edited 2 times in total
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Sep 09, 2015 5:05 pm     Reply with quote

IMHO: The code above should not be used in a program
any larger than what is posted.

An un-committed timer, internally clocked,
pre-scaled down and polled on its interrupt bit
is another suggested way for generating delays.

A search for "state machine" on the forum shows clever ways to
deal with de-bouncing multiple buttons at a time -

a problem not considered in the thread(yet).

I'm also concerned that not every soul posting agrees
on the meaning of "debounce".
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Thu Sep 10, 2015 5:12 am     Reply with quote

Here is an excellent paper by Jack Ganssle on debounce and the various
ways to handle it, both hardware and software.

http://www.eng.utah.edu/~cs5780/debouncing.pdf
_________________
Google and Forum Search are some of your best tools!!!!
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Sep 11, 2015 2:03 pm     Reply with quote

At the risk of sounding "me too",
the paper referenced in the above post is 1st rate !!

One other thing that makes debouncing simpler are
switches with "mechanical hysteresis". These are usually
found in more high end industrial applications and are
of an essential nature when the switch is gas pressure or hydraulic activated.

Readers are urged to consider that, even though this forum is full
of student questions pertaining to a single push-button - debounce is an issue
for all mechanical switches. not just the ones being pressed by a user finger.

And one approach to eliminating it logically, may not fit all needs.
temtronic



Joined: 01 Jul 2010
Posts: 9228
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Sep 11, 2015 3:15 pm     Reply with quote

Yup, the posted article is a good read !

this chunk of inline code is crude but works for the 'simple' applications

while (!input(LT) ) ; //wait if low
delay_ms(20); //debounce delay
while (input(LT) ) ; //wait if hi
delay_ms(20); //debounce delay

I know 20ms is like 'forever' but I wasn't in a hurry ! LT is actually the 'Left Turn signal switch for my truck that goes to the trailer wiring. Used an LM7805LP as the 'logic level' translator to convert the 12V signal to 5V for the PIC.It's actually cheaper than R+ zener, R + R. One part, 3 holes, and who doesn't keep 100 of them on the shelf !

Jay
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