1. 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.
    Dismiss Notice

Middle key not correctly recognized by 8051 processor

Discussion in '8051/8951' started by mik3ca, Jul 1, 2017.

  1. mik3ca

    mik3ca Member

    Joined:
    Jun 24, 2017
    Messages:
    184
    Likes:
    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.
     

    Attached Files:

  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    Where do the buttons connect to the 8051... It looks to me like you are only connecting to two pins..
     
  3. alec_t

    alec_t Well-Known Member Most Helpful Member

    Joined:
    Jul 10, 2011
    Messages:
    9,289
    Likes:
    1,223
    Location:
    Cardiff, Wales
    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?
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. mik3ca

    mik3ca Member

    Joined:
    Jun 24, 2017
    Messages:
    184
    Likes:
    1

    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?
     
  6. hexreader

    hexreader Member

    Joined:
    Apr 10, 2011
    Messages:
    214
    Likes:
    9
    Location:
    England
    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. mik3ca

    mik3ca Member

    Joined:
    Jun 24, 2017
    Messages:
    184
    Likes:
    1
    Ok, so I do detection, and about how much of a delay? equivalent to 0.69 times R times 3 times C?
     
  8. hexreader

    hexreader Member

    Joined:
    Apr 10, 2011
    Messages:
    214
    Likes:
    9
    Location:
    England
    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.
     
  9. alec_t

    alec_t Well-Known Member Most Helpful Member

    Joined:
    Jul 10, 2011
    Messages:
    9,289
    Likes:
    1,223
    Location:
    Cardiff, Wales
    How much time are you allowing for switch de-bouncing?
     
  10. mik3ca

    mik3ca Member

    Joined:
    Jun 24, 2017
    Messages:
    184
    Likes:
    1
    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.
     
  11. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    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
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    In fact I didn't use caps at all... It works just as you have it.. What is your values of R1 and R2... I used 1k..
     
  13. mik3ca

    mik3ca Member

    Joined:
    Jun 24, 2017
    Messages:
    184
    Likes:
    1
    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?
     
  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    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..
     
  15. mik3ca

    mik3ca Member

    Joined:
    Jun 24, 2017
    Messages:
    184
    Likes:
    1
    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.

    I also want to make it where the CPU doesn't spend too much time dealing with key presses.
     
  16. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    Like I said... Non standard ideas can cause problems, but if you just use two caps directly on the pins you'll be able to debounce better!
     
  17. mik3ca

    mik3ca Member

    Joined:
    Jun 24, 2017
    Messages:
    184
    Likes:
    1
    ok I took the caps off but now I think I'll need to replace the diodes with ultra-fast types (faster than 1N914)
     
  18. mik3ca

    mik3ca Member

    Joined:
    Jun 24, 2017
    Messages:
    184
    Likes:
    1
    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?
     
  19. alec_t

    alec_t Well-Known Member Most Helpful Member

    Joined:
    Jul 10, 2011
    Messages:
    9,289
    Likes:
    1,223
    Location:
    Cardiff, Wales
    How would a faster diode help? A finger press lasts many tens of mS. Switch bounce can also last tens of mS.
     
  20. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    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?
     
  21. mik3ca

    mik3ca Member

    Joined:
    Jun 24, 2017
    Messages:
    184
    Likes:
    1
    Nevermind. I removed the caps then I changed a minor boo-boo in my code and now the buttons work
     

Share This Page