CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Adding debounce into my program

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
orgee
Guest







Adding debounce into my program
PostPosted: Wed Jan 28, 2009 10:26 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jan 28, 2009 10:43 am     Reply with quote

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 Wink

Cheers[/code]
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Wed Jan 28, 2009 12:41 pm     Reply with quote

Check out my post at http://www.ccsinfo.com/forum/viewtopic.php?t=24103 to see if this might be something you can use.

Ronald
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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