Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Toggling LED with a pushbutton using interrupt

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
18,087
Helped
4,055
Reputation
8,110
Reaction score
3,972
Trophy points
113
Activity points
119,334
Hi,

You don´t say where you see a problem...

And maybe this is because there is no clear definition of operation.
Use a paper and a pencil and do some drawings - we professionals need to do this, too.

* Schematic
* some kind of timing diagram where we see initial states, the key press signals and the LED signals..

Klaus
 

John99407

Junior Member level 1
Joined
Jul 14, 2019
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
154
H
* some kind of timing diagram where we see initial states, the key press signals and the LED signals..
Klaus
when key is pressed, we just wait for debouncing and then again check key press

We can set timer interttupt i don't understand how the interttupt is use for debouncing time

IMG_20200926_191040.jpg
 
Last edited:

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
18,087
Helped
4,055
Reputation
8,110
Reaction score
3,972
Trophy points
113
Activity points
119,334
Hi,

Do you want me to guess what the problem is...
Do you want me to guess what´s your timing for debouncing, toggling, timer...

... which microcontroller, which language, which compiler, which port pins..

really?

Klaus
 

wwfeldman

Advanced Member level 3
Joined
Jan 25, 2019
Messages
747
Helped
177
Reputation
354
Reaction score
183
Trophy points
43
Activity points
5,442
somewhere in the code there is a timer that starts when the button is pushed
it then counts down a few milliseconds (50?)

when the count gets to zero, the bouncing of switch should be finished,
and the interrupt is executed

you really need to get the manual for the processor and start learning the assembler
 

John99407

Junior Member level 1
Joined
Jul 14, 2019
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
154
Hi,

Do you want me to guess what the problem is...
Do you want me to guess what´s your timing for debouncing, toggling, timer...

... which microcontroller, which language, which compiler, which port pins..

really?

Klaus
Problem is I don't understand how to use timer interrupt for the switch debouncing ,

I understand simple we check button if key is pressed, wait for 40ms, again check button if key is pressed toggle LED

Pic16F877A, c programming, MPLAB XC8,
RB0 for LED
RD0 for button
 

danadakk

Advanced Member level 1
Joined
Mar 26, 2018
Messages
408
Helped
79
Reputation
157
Reaction score
90
Trophy points
28
Activity points
2,063
First decision is do you want key to be just bounced in or in and out before you
act on the key press. That controls of course the program logic flow.

Generally speaking if you generate an interrupt on a pin edge or level, then start
a timer and set a flag. Set timer up to generate an interrupt when it is done, or poll it.
When timer finishes and you check that flag is set, and switch pin is still pressed, then
either perform the function needed or bounce it out/pin release and then perform function.

Best way is draw a flow diagram for your code, makes it easy to see and find where you have
not done everything necessary.

Many people use 50 mS or there abouts for bounce time, I have had switches bounce for
several hundred mS. That is your timer period. Note when you get first isr off pin you reset
and start the timer.


Regards, Dana.,
 
Last edited:

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
18,087
Helped
4,055
Reputation
8,110
Reaction score
3,972
Trophy points
113
Activity points
119,334
Hi,

if you want to write code - it doesn´t care whether you are experienced or not - you need to have clear requirements. .. an idea how you want it to work.

Your requirements don´t depend on microcontroller or timer...
In your case it´s just a button and a LED...
You need to exactly desribe how the LED should work with repsect to button press .. and maybe your timing requirements.
A timing diagram could do. It is simple, just two lines. One for the button one for the LED to start with.
Then you may add some lines and text for timing and other information. Everyone in the world will understand - independent of language.
If you are not able to show what you want - nobody can help you ....

****
Function:
There are many ways for doing what you want.
You may do an internet search to find (maybe millions) examples, descriptions, code, schematics, timing diagrams...
Read through some of them .. then decide your own way to go.
There will be no 100% wrong, there will be no 100% right. Ask 10 engineers and get 10 different opinions.
Thus "your" way...not our or anyone else´s way

****

Klaus
 

danadakk

Advanced Member level 1
Joined
Mar 26, 2018
Messages
408
Helped
79
Reputation
157
Reaction score
90
Trophy points
28
Activity points
2,063
Some ref material on bounce handling, attached.


Regards, Dana.
 

Attachments


Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top