One Series Button Debouncing with Event System Example Code

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

I read the Atmel example of the AVR128DA48 debouncing with Event System and TCB Timer at here.   Seem the code much easier than the old series MCU debouncing algorithms by Snigelen, is the new Event system method achieve the same function of button debouncing of those written by Snigelen or Jack.C Ganssle ?  

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Another question arise if I use four buttons which need to be debounce. Can I use only one TCB0 ?  If the user press the button1 and press the button2 in a short time where the TCB0 have not complete counting or not release , what will happen ?

 

  

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Run a separate state machine for each button. Poll the buttons from a timer interrupt. No need for this method to be any different with Mega0 or Mega1 compared to classic AVRs. 

 

It need not be a classic FSM. Just a counter that increments on each poll if the current button state is the same as its last state (and resets if the current button state is different from the last). Very compact. For multiple buttons, you need a "last state" for each button and a software counter for each and you are done. When a button counter reaches some arbitrary value, say 5 or 6 or ?, stop counting and report the new stable switch state to the outside world.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

Last Edited: Sun. Nov 21, 2021 - 06:57 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
No need for this method to be any different with Mega0 or Mega1 compared to classic AVRs

But the question was whether the "new" method gives any advantages/disadvantages

 

(I guess lack of backwards compatibility might be considered a disadvantage in some cases?)

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, certainly, use of the Event System disallows backward compatibility. The method I suggested uses counter/timer ISR and is fully compatible. Having to "manually" clear the interrupt flag in Mega0/1 devices is a difference, but causes no problem (other than unnecessary code execution) with classic devices.

 

The Event System method does appear to add significant challenge to handle more than one switch. The interrupt/pin-poll method handles multiple switches with little problem.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hamisu wrote:

Seem the code much easier than the old series MCU debouncing algorithms...

 

Really? If you use 'vertical counters'  you can debounce a whole 8-bit port in about 12 lines of code.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
Really? If you use 'vertical counters'  you can debounce a whole 8-bit port in about 12 lines of code.

 

You sure ?  Can you share your code.  I was stuck with many complicated deboucing method.  I just need multiple buttons code up to 4. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hamisu wrote:
Can you share your code. 
Just search "danni theusch debounce" here are Freaks, both their codes have been posted multiple times.

 

See this thread: https://www.avrfreaks.net/forum/... - especially post #23

Last Edited: Mon. Nov 22, 2021 - 08:45 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hamisu wrote:
I read the Atmel example of the AVR128DA48 debouncing with Event System and TCB Timer

To be pedantic that is a Microchip example. It uses the Microchip Code Configurator (ergo: the mcc_generated_files folder)) which has been a component pf MPLAB for many years. If you had built the code for a PIC instead, the code would look virtually identical.

 

Despite the numerous files the example is just too simple, most users would immediately hit the same hurdle you have when adding another button.

 

If you are running MPLABX you could try using the Microchip Code Configurator to generate a similar button denounce project for multiple buttons.

 

Last Edited: Mon. Nov 22, 2021 - 09:14 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just watched this video on hardware debouncing.  Might be an alternative.

https://www.youtube.com/watch?v=...

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jnorman wrote:
I just watched this video on hardware debouncing

The method highlighted above IS hardware debouncing. (It's the event sub-system that makes it so.)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Some things just go over my head when I make assumptions. I will have to look into that event sub system for my 4 momentary switches. Thanks!

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

fyi, internal to AVR are Schmitt trigger gates.

Overview | Getting Started with General Purpose Input/Output (GPIO) (very bottom, right)

 

"Dare to be naïve." - Buckminster Fuller