View previous topic :: View next topic |
Author |
Message |
eng/ IBRAHIM
Joined: 14 Aug 2007 Posts: 31
|
push button |
Posted: Mon Sep 24, 2007 2:16 am |
|
|
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
|
|
Posted: Mon Sep 24, 2007 4:14 am |
|
|
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
|
push button |
Posted: Mon Sep 24, 2007 5:35 am |
|
|
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
|
|
Posted: Mon Sep 24, 2007 6:32 am |
|
|
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
|
Re: push button |
Posted: Mon Sep 24, 2007 9:13 am |
|
|
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
|
|
Posted: Wed Sep 26, 2007 2:05 am |
|
|
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
|
|
|
eng/ IBRAHIM
Joined: 14 Aug 2007 Posts: 31
|
|
Posted: Thu Sep 27, 2007 2:10 am |
|
|
many thank for all i use debounce and play good |
|
|
|