|
|
View previous topic :: View next topic |
Author |
Message |
orgee Guest
|
Adding debounce into my program |
Posted: Wed Jan 28, 2009 10:26 am |
|
|
Hi, I'm newbie in PIC programming. I'm trying to design an infrared sensor with LED output. The program work but i want to upgrade with 'debounce' condition. Anybody can help how to integrate 'debounce' into my program?
Code: | #include <16f877a.h>
#use delay (clock = 20000000)
#fuses hs,noprotect,nowdt,nolvp
#BYTE PORTA=5
#BYTE PORTB=6
#BYTE PORTC=7
#BYTE PORTC=8
void main()
{
set_tris_b (0xff);
set_tris_c (0);
portc = 0b0000000; //initial condition
do
{
if (portb == 0b00000001) //LED left
portc = 0b00000001;
if (portb == 0b00000100) //LED right
portc = 0b00000010;
if (portb == 0b00000010) //LED both
portc = 0b00000011;
}
while(true);
}
|
|
|
|
daraos
Joined: 16 Oct 2008 Posts: 18 Location: Chile
|
|
Posted: Wed Jan 28, 2009 10:43 am |
|
|
an easy way to do it is to check the buttons every 10 ms, and activate the leds when the program detects the button presed twice in a row... something like:
Code: |
int left_led;
do{
delay_ms(10);
if (portb == 0b00000001) left_led++; //LED left
else left_led=0;
if(left_led>2) left_led=2;
if(left_led==2) portc = 0b00000001;
}
|
you have to add the code for the rest of the leds.
Not the most elegant solution, but for something as simple as this, it will do the task.
For something more complex, you can have an interrupt checking the inputs, using the same logic.
EDIT:
an enhancement in the code would be to check individual pins from the port, using
Code: |
if(input(pin_b0)) //to check the pin
OUTPUT_HIGH(pin_c0); //to set the pin
else
OUTPUT_LOW(pin_c0);
|
Doing it this way, you don't need to make all the combinations of ON and OFF buttons.
Of course, you'll have to apply the debouncer function to this, but it should be easy now
Cheers[/code] |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|