Only one count each time on button

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
Hi
Have this circuit.
The problem is that it count 1-15 times each time switch is pressed.
Direct input to TIMER/COUNTER in MCU ( 16F18877).,

Will it be better to make a IOC on a pin, and then add on to value each time ?

Also i need to change the state of edge,
MCU says rising edge, but my schematic do a falling.
 

Attachments

Alec_t

Joined Sep 17, 2013
14,335
Try increasing the value of either R or C to provide better de-bouncing of the switch. 10k and 100nF have a 1mS time constant, whereas the switch may bounce for many mS.
You can also use a software de-bounce routine.
 

AlbertHall

Joined Jun 4, 2014
12,347
Switch contacts bounce. That's why you have multiple counts. The resistor and capacitor method which your schematic shows is one the standard ways of 'de-bouncing' a contact. However the components you show have a time constant of 1mS while typical switch bounce may last 10mS to 50mS so a time constant of 100mS or so is required. You can get this by changing the capacitor to 10uF or perhaps better use a 47k resistor and 2.2uF capacitor.

If you look online you will find extensive discussions of de-bouncing using hardware and/or software methods.
 

eetech00

Joined Jun 8, 2013
3,961
Hi
Have this circuit.
The problem is that it count 1-15 times each time switch is pressed.
Direct input to TIMER/COUNTER in MCU ( 16F18877).,

Will it be better to make a IOC on a pin, and then add on to value each time ?

Also i need to change the state of edge,
MCU says rising edge, but my schematic do a falling.
Post your code please...
 

Picbuster

Joined Dec 2, 2013
1,047
Hi
Have this circuit.
The problem is that it count 1-15 times each time switch is pressed.
Direct input to TIMER/COUNTER in MCU ( 16F18877).,

Will it be better to make a IOC on a pin, and then add on to value each time ?

Also i need to change the state of edge,
MCU says rising edge, but my schematic do a falling.
in .H
# define KeyPress Rx0 // PORTxbits.Rx0 x is the port you want the interrupt to arrive.

// in interrupt routine
if ( KeyPress) { Key_hit=On;}
// end of this int


while(1) // mainloop
{
if (Key_hit)
{
counter++;
Key_hit=Off;
// delay when needed when main loop is to fast
}

.
.
.
}

Picbuster
 

AlbertHall

Joined Jun 4, 2014
12,347
in .H
# define KeyPress Rx0 // PORTxbits.Rx0 x is the port you want the interrupt to arrive.

// in interrupt routine
if ( KeyPress) { Key_hit=On;}
// end of this int


while(1) // mainloop
{
if (Key_hit)
{
counter++;
Key_hit=Off;
// delay when needed when main loop is to fast
}

.
.
.
}

Picbuster
Unless the delay in the main loop is greater than the bounce period (possibly up to 100mS) this will still register multiple counts when the switch bounces.
 

eetech00

Joined Jun 8, 2013
3,961
Hi

Use an interrupt timer for the debounce service.
Adjust the debounce counter in the interrupt service to provide 100ms delay once a keystroke is detected.
If a keystroke is still detected after the 100ms delay, trigger the count up timer (or whatever you call it).

You don't have to use large RC values for the pushbutton since you are using software debouncing.
The 10k and 100nf cap should be OK.

eT
 

atferrari

Joined Jan 6, 2004
4,771
For years, the initial test of any project involving a keyboard, I implemented with a matrix of 16 buttons of the worst quality I had in my drawers. My matrix-reading code involves a counter which materializes the debouncing delay. 25 msec proved enough, even for those awful switches. Just in case, final version went with 30 msec (and better switches).

The unique exception: a phase adjustable dual signal generator, my farewell design with the 18F84A, where, no mater what delay I used, did unexpected jumps from time to time. I suspect it was a software bug not related with the keyboard reading routine.
 

ErnieM

Joined Apr 24, 2011
8,377
in .H
# define KeyPress Rx0 // PORTxbits.Rx0 x is the port you want the interrupt to arrive.

// in interrupt routine
if ( KeyPress) { Key_hit=On;}
// end of this int


while(1) // mainloop
{
if (Key_hit)
{
counter++;
Key_hit=Off;
// delay when needed when main loop is to fast
}

.
.
.
}

Picbuster
Not only may it indicate multiple hits for a bouncing switch, it may also indicate multiple hits as long as the switch is pressed, depending on what event fires the interrupt routine.

Using Key_hit to transmit the state of the button is a very good way of handling this.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
Code is not yet written in New PIC
Going from 16f1509 to 16f18877
I had many problems when making 1509.
Have used 2 cap and 2 resistors but it is not perfekt
I Will use interrupt and start a timer and 200 ms later add 1 to counter or something like that . Think that is the best
 
Top