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

push button

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
eng/ IBRAHIM



Joined: 14 Aug 2007
Posts: 31

View user's profile Send private message

push button
PostPosted: Mon Sep 24, 2007 2:16 am     Reply with quote

we need code for this(16f877)

when I push the button once, led go to high.
when I push the button again, led go to low.
umka



Joined: 28 Aug 2007
Posts: 99
Location: New Zealand

View user's profile Send private message

PostPosted: Mon Sep 24, 2007 4:14 am     Reply with quote

this is really simple code. you should try it yourself if you want to learn something. post your code and people will help correct it if its wrong
eng/ IBRAHIM



Joined: 14 Aug 2007
Posts: 31

View user's profile Send private message

push button
PostPosted: Mon Sep 24, 2007 5:35 am     Reply with quote

umka wrote:
this is really simple code. you should try it yourself if you want to learn something. post your code and people will help correct it if its wrong



i try this code
void main()
{
while(1)
{
if(input(pin_d2))
{
output_high(pin_b6);
delay_ms(300);
}
if(input(pin_d2))
{
output_low(pin_b6);
delay_ms(300);
}
}
}

but the led is on&off when pushed continus.and the time not stable
i need the led on when pushed, led off when pushed agin not depending on the time off push
Foppie



Joined: 16 Sep 2005
Posts: 138
Location: The Netherlands

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Mon Sep 24, 2007 6:32 am     Reply with quote

Something like this might work: (this code is not tested)
Code:
main()
{
   int1 oldState = 0, newState = 0;
   
   output_low(PIN_B6);
   while(TRUE)
   {
      newState = input(PIN_D2);

      if ((oldState == 0) && (newState == 1)
         output_toggle(PIN_B6);

      oldState = newState;
      delay_ms(1);
   }
}

But this is less efficient as using an interrupt, my advice is to use the external interrupt and just to output_toggle when the interrupt happens.


Last edited by Foppie on Tue Sep 25, 2007 12:52 am; edited 1 time in total
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

Re: push button
PostPosted: Mon Sep 24, 2007 9:13 am     Reply with quote

eng/ IBRAHIM wrote:

but the led is on&off when pushed continus.and the time not stable
i need the led on when pushed, led off when pushed agin not depending on the time off push


Two points to fix the original code:
1) You need to wait for the button the be pressed, then you need to wait for it to be released.
2) Look up "debounce". When you close most any mechanical switch the metal contacts bounce, giving several openings & closings in a few milliseconds. You need to be careful when you consider the switch closed or opened.
_________________
The search for better is endless. Instead simply find very good and get the job done.
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

PostPosted: Wed Sep 26, 2007 2:05 am     Reply with quote

Hello,

Here is a clear document about debouncing: www.ganssle.com/debouncing.pdf

Personaly, the simple code :
Code:
if ( key is pressed ) {
  delay 50 ms
  wait while key is pressed
  delay 50 ms
  ...
}
is very well working every time!

Of course, some time, a timer can help to debounce without stop the working program with a delay as :
Code:

#define  INIT_debounceTime (a value for autorepeat key)

#INT_TIMER0 
void TIMER0_isr(){
  if ( debounceTime != 0 ) --debounceTime ;
}

void main () {

  ...

 while( true ) {
    ...

    if ( key is pressed and debounceTime == 0 ) {

      ...

      debounceTime = INIT_debounceTime
    }
  }
 

}

_________________
in médio virtus


Last edited by inservi on Thu Sep 27, 2007 2:42 am; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Sep 26, 2007 11:02 am     Reply with quote

For questions like this, you should always use the forum's search page.
Search for: button
There are many posts with sample code. This thread has one that I did:
http://www.ccsinfo.com/forum/viewtopic.php?t=31849


Also search for: debounce
Here's a thread with sample code:
http://www.ccsinfo.com/forum/viewtopic.php?t=19874
eng/ IBRAHIM



Joined: 14 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Thu Sep 27, 2007 2:10 am     Reply with quote

PCM programmer wrote:
For questions like this, you should always use the forum's search page.
Search for: button
There are many posts with sample code. This thread has one that I did:
http://www.ccsinfo.com/forum/viewtopic.php?t=31849


Also search for: debounce
Here's a thread with sample code:
http://www.ccsinfo.com/forum/viewtopic.php?t=19874



many thank for all i use debounce and play good
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