Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 07:07 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : A different take (maybe not) on switch debounce..

Author Message
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 07:17pm 08 Jun 2022
Copy link to clipboard 
Print this post

I don't care for "PAUSE" because I like to keep SETTICK alive.


Do
 for i% = 1 to 500    'Needs 500 consecutive samples of high, to be valid
   if not pin(GP14) then i% = 1
 next

 select case tempset%
   case 0
     tempset% = 1
   case 1
     tempset% = 2
   case 2
     tempset% = 4
   case 4
     tempset% = 0
 end select

 port(GP11,3)=tempset%

 for i% = 1 to 2000    'Needs 2000 consecutive samples of low, to be valid
   if pin(GP14) then i% = 1
 next
 
Loop until inkey$<>""
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5091
Posted: 08:06pm 08 Jun 2022
Copy link to clipboard 
Print this post

This keyscan completely saturates the CPU. If that is your plan that is okay (when you have nothing to do otherwise, but spend time in an "idle" DO:LOOP). That means that all processing has to take place in the SETTICK.

I know sometimes solutions like that are needed, but I prefer to see SETTICK as an interrupt, and would spend little time there. And much time at main level.

By the way: SETTICK is not affected by PAUSE. It simply continues through the PAUSE (picomiteVGA 5.07.05b6)
Edited 2022-06-09 06:10 by Volhout
PicomiteVGA PETSCII ROBOTS
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 08:17pm 08 Jun 2022
Copy link to clipboard 
Print this post

Jack Gansell did a lot of research on the best algorithms with really good supplemental evidence.

I took one of his examples which keeps a rolling sample allowing your program to get on with it's stuff but still know the state of a switch when you need it. I use it a lot, I even added it to Volhout's component tester software when I replaced the touch buttons with real ones.

find my MMBasic code here
Edited 2022-06-09 06:33 by CaptainBoing
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 08:37pm 08 Jun 2022
Copy link to clipboard 
Print this post

  Volhout said  This keyscan completely saturates the CPU. If that is your plan that is okay (when you have nothing to do otherwise, but spend time in an "idle" DO:LOOP). That means that all processing has to take place in the SETTICK.

I know sometimes solutions like that are needed, but I prefer to see SETTICK as an interrupt, and would spend little time there. And much time at main level.

By the way: SETTICK is not affected by PAUSE. It simply continues through the PAUSE (picomiteVGA 5.07.05b6)


I totally agree! And to paraphrase Charles Petzold (Programming Windows); "the best way to use interrupts is not to" and I concur, which is why I am a devout Propeller user...we don't need the darned things.

However, in process control, you often need some form of determinism and this is where SETTICK comes in. I don't subscribe to the "just set a flag and get out". Moreso using ByPic, my entire process runs in the ISR.
Good to know that PAUSE is non-blocking  



Craig
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 08:47pm 08 Jun 2022
Copy link to clipboard 
Print this post

  CaptainBoing said  Jack Gansell did a lot of research on the best algorithms with really good supplemental evidence.

I took one of his examples which keeps a rolling sample allowing your program to get on with it's stuff but still know the state of a switch when you need it. I use it a lot, I even added it to Volhout's component tester software when I replaced the touch buttons with real ones.

find my MMBasic code here



  Quote  
will shift a one in and "spoil" our counter for which will start again.


Same as I just demonstrated, no?  


Craig
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 05:08am 09 Jun 2022
Copy link to clipboard 
Print this post

I think we are saying the same thing.

Gansell's take was to utilize the loop of Main to do the work and ignore the state, accumulating the sample into a count regardless of whether you care at that time, so no dedicated handler with all its foibles.

Gansell's code just runs a single line of code (per switch) on the main loop. Each time through Main, does the shift then you get on with everything else, regardless of any changes in the state of the switch.

btn=btn<<1 Xor Pin(Switch) And &h1f


That line of code runs like a jack-rabbit even on the lowly '170 and you can use a few tricks to speed it up even further, like using CONSTs for the fixed values, or dropping the final AND, only doing it when you test the variable (although if you do that each time through Main what is the point?). <1.4mS at 5MHz, 159uS at 48 (blimey!).

Whenever you need to check the switch, you just look at variable btn. In the case above with active Lo you are only interested in 0, anything else is either not pressed or bouncing. All the work was done "in the background" by that line of code in the main loop.

jus' sayin'
Edited 2022-06-09 15:36 by CaptainBoing
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 05:35am 09 Jun 2022
Copy link to clipboard 
Print this post

  CaptainBoing said  ... kind of.

you are in a loop for a period of n "activation cycles" and then again for "de-activation cycles". There is a possibility of being stuck in there for some time especially for a crappy input (or user) when you set the counter variable back to the start. As shown, while you are in those loops you aren't running your application.

Gansell's code just runs a single line of code (per switch) on the main loop. Each time through Main, you do the shift then get on with everything else, regardless of any changes in the state of the switch.

btn=btn<<1 Xor Pin(Switch) And &h1f


Whenever you need to check the switch, you just look at variable btn. In the case above with active Lo you are only interested in 0, anything else is either not pressed or bouncing. All the work was done "in the background" by that line of code in the main loop.

jus' sayin'


Oh, absolutely!

I wouldn't actually implement as shown (just test code). I never write anything that could potentially freeze the system.
When checking machine inputs, for example, I always establish a reasonable timeout, after which, I put the machine in a safe condition and throw-up a timeout message.
Otherwise, someone might approach a stopped machine and it could potentially take-off running again...not good  
Actually, this is a scary aspect of programming being accessible to all. One might be a wizard at coding but not understanding the process and safety pitfalls is darned dangerous  


Craig
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 05:38am 09 Jun 2022
Copy link to clipboard 
Print this post

sorry Craig, we keep crossing in the mail   what you doing up at half six, I thought I was the only one sad enough to be doing this stuff with the birds

see my updated post
Edited 2022-06-09 15:38 by CaptainBoing
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 05:54am 09 Jun 2022
Copy link to clipboard 
Print this post

I can see this turning into a Monty Python sketch  

Sad? You don't know the meaning of the word....I'm under the gun and so I have a sleeping-bag in this factory...never take a day off. Not even Xmas. TWO ex-wives can tell you all about it  .

You ever walk away from your work with a niggling issue and you either grab a pint or hit the sack and the solution hits you? And now you can't wait to get back to the project? Drives me nuts  





Craig
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 06:54am 09 Jun 2022
Copy link to clipboard 
Print this post

I don't have the exes (still on wife No.1... I don't think I am gonna make it  ), but yes I completely identify with that situation. ordering dominoes at 11pm is not funny.

In my life I have had, I think, three occasions where I have dreamt the solution to a problem. Not so much now-a-days but when I was a much younger man I used to keep a notepad by the bed. Working out how to get addressable pixel graphics on a text only display using a TI controller... TMS9918 or something like that was a particularly memorable one.

Good luck with your project
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 07:55am 09 Jun 2022
Copy link to clipboard 
Print this post

I think that dogs and cats have it figured out. They take frequent naps and have energy when they need it. We, however are accustomed to making the most of available natural light and so we slog it out. I do the nap-thing at weekends because I have the factory to myself and my weekend productivity is through-the-roof compared to Mon-Fri.

It helps that I live/sleep/breathe machine control and that I can't abide TV or nothingness conversations...Just wish Thebackshed was my local pub  







Craig
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 08:34am 09 Jun 2022
Copy link to clipboard 
Print this post

agree on that.

When I was a programmer full-time, there was always something cathartic about 3am that made writing code so easy

I haven't had a TV licence for two years - finally convinced wife it was all sh*te and just rots yer brain. haven't looked back. the various subscription services give much higher quality content and you can choose what/when/how much to watch. I really can't abide stuff like BGT and strictly etc. just the lowest of the low and I ain't paying for it!
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7938
Posted: 10:19am 09 Jun 2022
Copy link to clipboard 
Print this post

  Quote  Just wish Thebackshed was my local pub


I'm close enough to your local pub to take the Backshed there for you. :)

======================

Cap'n: I wish I could convince 'er indoors of the same... She loves cookery & sewing & crafts and will watch anything associated with them. I've had to learn how to switch off.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3378
Posted: 11:32am 09 Jun 2022
Copy link to clipboard 
Print this post

Haven't had TV/cable for 15 years (but may still spend an inordinate amount of time on Amazon, Netflix, youtube, not to mention doom-scrolling). But re "She loves cookery & sewing & crafts and will watch anything", me wyfe finds plenty of that streaming.

Since high school I've awakened with solutions to daytime problems percolating up.

Nap every afternoon, and have done when possible for 50 years. Even on every sunny day after lunch on a mat in a park across the street from where I was working.

But I'm glad it's been nearly 5 decades since I last used a sleeping bag at a workplace.

Agree about the productivity of weekend times, and for me, especially the week between xmas & new year's when no one else was around.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 12:28pm 09 Jun 2022
Copy link to clipboard 
Print this post

  Mixtel90 said  
Cap'n: I wish I could convince 'er indoors of the same... She loves cookery & sewing & crafts and will watch anything associated with them. I've had to learn how to switch off.


My wife is the same. there is tons and tons of really interesting stuff on YT. get yer missus into that then turn all the broadcast stuff off - its like a breath of fresh air not having the 20 minute loop tape of "news" running and all the garbage cheap telly.

Then you can regale each other with what you found on "Pasta Grannies"
Edited 2022-06-09 22:29 by CaptainBoing
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 12:38pm 09 Jun 2022
Copy link to clipboard 
Print this post

Cap'n, you're suspect...you dropped the TV tax after Jeremy Kyle got shut down, right?
   








Craig
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 01:30pm 09 Jun 2022
Copy link to clipboard 
Print this post

that's daytime TV... who has time or few enough brain cells for DTTV?

garbage. Atch' that's unfair... most TV is garbage. It got to the point I'd be watching 20 year old repeats of star trek as the only thing vaguely entertaining... paying sky for he privilege and paying a TV licence. cancelled them both on the same day  

Lizby is on the money. I am a late starter. to think of the hours I wasted.
Edited 2022-06-09 23:33 by CaptainBoing
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3378
Posted: 01:45pm 09 Jun 2022
Copy link to clipboard 
Print this post

When we travel and turn on the TV in a motel, all the ads are staggering. It's a great reminder of what we're missing (and not missing).
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7938
Posted: 02:50pm 09 Jun 2022
Copy link to clipboard 
Print this post

Those 24hr soddin' craft advertising channels are truly horrible. :(
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025