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.

How to do software debounce on MIKROC ???

Status
Not open for further replies.

BGAmodz

Member
Hello everyone .

AS always am doing a lot of programming training , as a preparation for my project .

So 2 days ago i decided to make this simple program

In human language :
- I have 4 leds , one puch-on button , a pic16f84a .
- when i press the first press , led 1 light up , another press , the second one lights up the first shuts down .........and so on , till i redo the cycle .

Now this is Computer language , the program seems to work with respect to my logic but the problem is that leds light up randomly , and there is a lot of flickering .

So is there a possibility to do the software debounce as easy as possible ??
Code:
#define start porta.b0
#define led1  portb.b0
#define led2  portb.b1
#define led3  portb.b2
#define led4  portb.b3
int x0,x1,x2,x3;

void main() {
porta=0x00;
portb=0xff;
trisa=0xff;
trisb=0x00;
x0=1;x1=0;x2=0;x3=0;

while(1){


if((start==1)&&(x0==1)){
x0=0;x1=1;led4=1;led1=0;
}

if((start==1)&&(x1==1)){
x1=0;x2=1;led1=1;led2=0;
}

if((start==1)&&(x2==1)){
x2=0;x3=1;led2=1;led3=0;
}

if((start==1)&&(x3==1)){
x3=0;x0=1;led3=1;led4=0;
}

}
}
 
The simplest way is not to react on button presses for a certain period of time (200ms?) after the button has been pressed. If you don't have other things to do, you can just wait for 200ms without doing anything.
 
Hey bro i have successfully achieved this , thanks .
It did work after i added another delay between variable value trasition too .
Here are the changes i made :


Code:
while(1){


if((start==1)&&(x0==1)){
delay_ms(200);
x0=0;x1=1;led4=1;led1=0;
}

if((start==1)&&(x1==1)){
delay_ms(200);
x1=0;
delay_ms(200);
x2=1;led1=1;led2=0;
}

if((start==1)&&(x2==1)){
delay_ms(200);
x2=0;
delay_ms(200);
x3=1;led2=1;led3=0;
}

if((start==1)&&(x3==1)){
delay_ms(200);
x3=0;
delay_ms(200);
x0=1;led3=1;led4=0;
}

}
 
I found the best way is to use an audible signal to debounce the button

If keypress then
buzzer on
wait 250
buzzer off
end if

This way the user hears the keypress and lets go...
Thanks for this , may try it later .

I need to learn more on the C language , am reading this book right now (The C Programming Language, 2nd Edition by BRIAN W.KERNIGHAN & DENNIS M.RITCHIE .)

What do you think of this book ??
 
Howdy, given 30+ years of coding, 90% assembler, I've hit this wall.... just a few times....

Look in the code repository for my best debounce algorithm:
Set a countout for "debounced loops means valid input", this value is number of scan loops until confident of valid state change.

A> It takes the current switch state, and loops on that it doesn't change, until a countout that means it Didn't change. == exit with new switch state
if it changed: reload countout & current switch state and go back to A>

If you've got a Really noisy environment: set another countout for the number of state change/reloads until you "ignore" it.
Then a Genuinely pushed button will "eventually" win, just by breaking through enough "ignore" loops.

This method is the only nonprobabilistic approach I've ever seen/heard of.
PITA--- I had to think of it, because I didn't have anyone to show it to me...

If you sense some ego in this, consider it frustration that I thought this wheel was already round....
I shouldn't have had to think of it (or did somebody else not want to share...)

Sorry it's written in Freescale SO8 assembler, but if you can't port it, you're not a real coder yet.

Try it... I'm thinking you'll like it. It's eminently tunable (you can even split the state change +->- count from ->+ if you can characterize your switch that well)
It's fun to watch a test pin reflect the switch state as the software scans it on an o'scope. That really lets you set the validity loop counts.
Enjoy.. & G,H... <<<)))
 
Hi olphart .

I appreciate your help , and really impressed by your career .

Am not a very good coder yet , i have only succeeded to make an electronic LCD clock ( the hardest one for me at the moment ) , but have not completed it yet .

Right now am reading a book on C language ( see previous post) , and hoping to get better in order to apply the learned Technics on some programs from my own imagination .

As for the debounce method , i have managed to make a software debounce using delays only , but i guess its not the best method .

If you can explain to me step by step your own debounce method , we can try to make a C language version of it .
 
Without using interrupts, something along the lines of this should work: Read your port, mask your inputs (keys/ switches etc) and temporarily store your result. Call a short delay, read the port again, mask your inputs (keys/ switches etc) and compare the result of the first read to this one, if it's the same call another short delay etc. Keep doing this as many times as you want until you are satisfied that the key press is stable. If the result of the compare is not the same on any iteration call a longer delay and start over from the beginning. At some point your key/ switch press will become stable and you can proceed with your code :)
 
Howdy, tunedwolfs' got it, with 1 variation: I don't wait a short delay to check again. I go full speed looping and count the loops. If the switch state changes, reset the count. If no change at full count, the state is stable. This works even in noisy environments, it just takes longer for the new state to be considered stable. G.H... <<<)))
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top