Continue to Site

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.

  • 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.

MCU Switch State Change Detection | Falling Edge Detection Problem

Status
Not open for further replies.
This morning my head is a little more awake and the solution to your problem is rather simple. Just use 1 side of your switches so it's either on or off and do the edge detection in software. Edge detection is easy in software. Assuming your keyboard routine returns a 32 bit variable with 1 bit for each switch, to find changed switches xor the current switch state with the previous state - any 1 bits are the switches that toggled.

Code:
    keys = ReadKeys();
    edges = Keys ^ Previous;
    previous = keys;

Mike.
 
I think I get that now, your coding style is very different to mine, but then no 2 are the same.
 
You can also detect key presses and key releases - I.E. rising and falling edges.

Assuming active keys are logic 1 in a variable keys,

NewPresses = (Keys^Previous) & Keys
NewReleases = (Keys^Previous) & Previous

If the keys are active low then swap the above two calculations.

This comes in very handy sometimes. It can also be used on portb change interrupts to work out which bit triggered the interrupt.

Mike.
 
Just use 1 side of your switches so it's either on or off and do the edge detection in software
Thanks a lot for this idea! I swear to god that every time I came across a problem in electronics so far, it turned out to be very simple and I was just overcomplicating things. This is just another one to add that the list.

I have tried software edge detection before, a little bit differently because I ran a function for each button instead of gathering information about all of them and then executing code based on that. Your approach is very interesting and I might end up implementing it in the end because its very neat!

The way to detect key presses and releases is also very good because allows you to know the position of a push button as well. Thanks for that!

Lukasz
 
I was going to say this in #22.
You can edge detect using the interrupt system, and you dont need to actually have interrupts, you can poll the interrupt bit to see if a change has occurred even with interrupts off.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top