• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Middle key not correctly recognized by 8051 processor

Thread starter #1
circuit.png
I don't understand why my routine doesn't work. The left and right buttons (S1 and S3 in schematic) always function normally, but sometimes S2 (center) functions normally, and sometimes it functions as button S1. In the circuit, all resistors are 10K, all diodes are actually 1N914, and all capacitors are 2.2uF tantalum. I have attached source code to the function that is problematic. I'm not sure what strategy to use to rectify my problem, but I'm trying to eliminate the debouncing effects via software and capacitors in parallel with the buttons and if possible, I want to avoid redoing my circuit.

Any ideas what I can do to my code or whether I should add/remove parts to my circuit without changing PCB layout?

Also, the middle button is equivalent to the left and the right button pressed simultaneously.
 

Attachments

alec_t

Well-Known Member
Most Helpful Member
#3
Pressing S1 or S2 discharges only a single capacitor. Pressing S3 discharges all 3 caps. Are you sure your code allows enough time for that discharge and subsequent longer re-charge period?
 
Thread starter #4
Where do the buttons connect to the 8051... It looks to me like you are only connecting to
two pins..
The pins connect to the resistors. Yes I know there's only two GPIO pin connections, but with my method, I conserve pin usage on the micro. Middle button pressed means both pins are set to low.

Now I could use smaller capacitor values but at the same time, I don't want to create a jitter-effect. For example, I don't want it where when the button is pressed, the micro sees it as pressed for 2 microseconds then released for 2 microseconds when in fact the button is still held down.

The buttons I'm using are omron's B3F-40xx series.

What capacitor values should I use?
 
#5
the middle button is equivalent to the left and the right button pressed simultaneously.
No, the middle button is equivalent to one button indicating pressed a fraction of a second before the other. No two events in real life are exactly simultaneous.

I think you maybe need a second read of the buttons, after a short delay from first detecting button press.
 
#7
In theory, Capacitors will discharge instantly, since you are shorting them out.

Maybe your formula works if you take R to be capacitor ESR. Maths and theory are not my strong points.

In reality, I have no idea regarding resistances and timing. Switch bounce is a pretty random phenomenon that varies with button, age and finger pressure.

Shorting out capacitors with every key press presumably affects the switches over time.
 

alec_t

Well-Known Member
Most Helpful Member
#8
How much time are you allowing for switch de-bouncing?
 
Thread starter #9
From my code, it detects when a key is pressed down right away but then it counts for at least 2 microseconds from the moment all buttons are released before the system believes a valid keypress is registered. Look at the code for details.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
#10
The pins connect to the resistors. Yes I know there's only two GPIO pin connections, but with my method, I conserve pin usage on the micro. Middle button pressed means both pins are set to low.
Looking through the code... Just tell me which pins are being used...

Tristating 8051 pins depends very much on the pins used... You only need a cap on the two pins.. Charge the caps then tristate the pins to read... It wont bode well ( IMO) if the caps are across the switches..

You are trying to ( after some delay ) read both pins, if switch one is pressed, then pin 1 will be low. if switch 2 is pressed then pin 2 will be low The middle just becomes pressing two at the same time..

Set pin1 and 2 high
Tristate them
delay 20mS+ and read..
Then count 250mS to allow the finger to be released..

upload_2017-7-2_19-16-29.png
 
Thread starter #12
my R1 and R2 are 10K each. But if I remove the capacitor across the keys, wouldn't weird things happen once a key is pressed such as the processor taking it as key pressed for 1us and key released for 1us and repeat? I thought by adding capacitors that I eliminate bouncing effects of the buttons?
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
#13
Again... Which pins are you using..

When using micro's you eliminate bounce in software... Its easier, It looks to me like you are trying to both! Change the resistors to 1k and see if it's better... Then take the caps out and see if its better... Then place the software debounce in the chip..
 
Thread starter #14
Again... Which pins are you using..
The actual pin numbers on the micro? I believe P1.3 and P1.4. I can't remember to be exact but I know its two pins between P1.2 and P1.7.

When using micro's you eliminate bounce in software... Its easier, It looks to me like you are trying to (do) both!
I also want to make it where the CPU doesn't spend too much time dealing with key presses.
 
Thread starter #17
Now I am baffled. I removed the caps and now no keys are being picked up properly. Is there something else I need to know regarding the debouncing of buttons that no one is telling me?
 

alec_t

Well-Known Member
Most Helpful Member
#18
How would a faster diode help? A finger press lasts many tens of mS. Switch bounce can also last tens of mS.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
#19
This is why I asked about the port pins.... The P1 and P3 port has "Extremely" weak pullups You will need to boost this
P0 has no pullups and P2 has controllable pullups.. Can you bolster the pin to Vcc by adding a 1k?
 

Latest threads

EE World Online Articles

Loading

 
Top