[question] Buttons scan routine

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

Hello everyone!

I have this project in witch I have 7 buttons
4 controlls air temperature and air speed, 3 buttons that are left is responsible for presets

So I have to build button scan/poll routine.

What it has to do:
- support short press
- long press and repeat function (if held down it should repeat function every n times a sec)
- individual hold time definition if possible

Mcu is atmega16

I do know that I have to use timer of mcu to poll buttons, but how often, at the same time I have to control pwm signal and send data to display. So if I do it to often, like 5ms timer, it interferes with lcd and other stuff

P.S I saw some libs for multiple buttons.

But documentarion is close to norhing and I do not know how to implement it...

Do tou have some tips on how to design this type of routine?

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

VeryFastSnail wrote:
but how often,
Many scan routines use something like 5ms or 10ms. A button is considered in a stable state when it has been seen in the same state for N ticks where N might be something like 5 or 8 or something.

 

Not sure why you think a 5ms timer (that itself then takes a few us to run each time) should have any major effect on something like LCD? Remember an LCD is actually a "slow" (in computing terms) device. It's generally aimed at a shaved ape and they can't really handle information updates faster than something like 100ms. So, yeah, there might be 20 updates on a 5ms period during one 100ms LCD update cycle but if each interrupt is only 30us (say) then you are only really stealing a fractional percentage of the CPU time. The ape won't care.

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

VeryFastSnail wrote:
I have to use timer of mcu to poll buttons

well, you don't have to - but it does seem the obvious solution!

 

VeryFastSnail wrote:
documentarion is close to norhing

documentation of what, exactly?

 

Scanning of keypads was well established before the AVR was even invented ...

 

EDIT

 

The techniques of switch debouncing are basically what you need - a load here:

 

https://www.avrfreaks.net/forum/guides-switch-debouncing

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...
Last Edited: Fri. Feb 14, 2020 - 12:48 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

Scanning of keypads was well established before the AVR was even invented ...

 

I meant this library, sorry for not pointing it out...

 

https://www.avrfreaks.net/projec...

 

This library has everything that I need, but as for now I do know how to implement it. I'm stuck at:

 

void InputProcess(INPUT_EVENT_BUFFER_DATATYPE AInput)

function, how do I pass all required buttons to it?

 

 

P.S sorry for my newbie'ness I'm new to AVR direct programming...

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

clawson wrote:

 

Not sure why you think a 5ms timer (that itself then takes a few us to run each time) should have any major effect on something like LCD?

 

If I understand it correctly timer will interrupt every 5 ms + code execution in ISR right? So what if during that time I have to change PWM duty?

Again sorry if this looks easy to you, but I'm very motivated to learn as much as possible. :)

 

Thank you!

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

VeryFastSnail wrote:
So what if during that time I have to change PWM duty?
If you design ISRs correctly you should be in and out well within 10us (say). If so then if one particular change of PWM happens to be delayed by an entire 10us every once in a blue moon does it actually matter - does anything in the system actually notice?

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

clawson wrote:

VeryFastSnail wrote:
So what if during that time I have to change PWM duty?
If you design ISRs correctly you should be in and out well within 10us (say)

I do know that ISR should olny perform minimaliatic commands, flag setting, change variables and so on. And this question is just of the curiosity, so that I would avoid it if it’s a problem. Thank you.

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

You really should try this yourself.  Using a library to bypass all the learnings will not go in your favor.   You can find out a lot by developing your routines.  And if you do it yourself, you get to join the bounce barons club, with the first year of fees waived.  Read & think, then plan,  then code.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

Consider how fast to need to attend to each device - for the average character lcd, it is no use updating them faster than 10 times a second and there's two reasons for that - one: the human needs time to read and process the message and two: the lcd specs say that the lcd won't physically change much faster than that.

 

Also consider how fast the human can press the buttons and how quickly the air temperature changes as well as changing the fan speed. All these times are 10's of milliseconds.

 

As such, you don't need an interrpt - you can simply poll the compare flag of the timer in CTC mode and run your code to 

1. read the inputs.

2. do program logic

3. update outputs.

 

rinse and repeat. That loop can run at 10ms to give you a reasonable timebase. You want a time delay? - count ticks each loop until the required time is reached.

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

avrcandies wrote:
Using a library to bypass all 

You are right, I will develop my own buttons class. Thank you.

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

I wouldn't be thinking of classes. You need to consider the larger architecture picture.

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

avrcandies wrote:
And if you do it yourself, you get to join the bounce barons club

 

Well this reason alone make me want to make it myself :D