View previous topic :: View next topic |
Author |
Message |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
problem with the logic |
Posted: Wed Sep 19, 2012 9:55 am |
|
|
Hi!
The controller is PIC10F200.
I want to make a start button. While I'm pushing the button, the relay should be on, and while the button is not pressed, the relay is off. Every time I push the button again, the relay shold not start again, unless the first time pushing.
The problem is that it's not happening every time.
Code: |
#include <10f200.h>
#fuses NOMCLR,NOWDT,NOPROTECT
#use delay(clock = 4M)
#define butt pin_b3
#define led pin_b2
#define relay pin_b1
void main ()
{
int val=0;
short int start=1;
set_tris_b(0);
output_b(0);
delay_ms(100);
while (true)
{
if(input(butt)==1&&start==1)
{
do
{
output_high(led);
output_high(relay);
}
while(input(butt)==1);
start=0;
}
output_low(led);
output_low(relay);
}
} |
Excuse my bad English! |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Sep 19, 2012 12:20 pm |
|
|
you are time domain impaired here.
you need to debounce the key press
and
add a deadband before a new keypress can be acted on. |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Wed Sep 19, 2012 2:52 pm |
|
|
Since you say English isn't your strongest language maybe an explanation might help. A button type switch or any mechanical switch will vibrate when making contact. The vibration electrically appears as a number of on off contacts for a few thousands of a second. This is called bouncing. De-bouncing is transforming these signals into a single signal (on or off). This can be done by having the button on a pin that can interrupt the PIC..ex external interrupt pin. In that case the interrupt service routine (ISR) would see the bounces and determine the final state of the press and set a button state flag. This interrupt way is called non blocking since it allows the button to be pressed at anytime ( even when the PIC is executing the middle of a line of your main code). The other way is to do this in your main code. This is considered blocking in that you will need a loop that periodically checks if the button has been pressed down for a certain small amount of time. Ex test button pin if pressed delay a short time that spans the bounce time. Don't alter the initial button state inside the bounce time. |
|
|
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Thu Sep 20, 2012 10:53 am |
|
|
First, the controller I'm using(10f200), do not have any interrupts.
Second, I have a capacitor to prevent bouncing.
Is that possible the problem benig lack of braces between both conditions inside:
Code: | if((input(butt)==1)&&(start==1)) OR if(input(butt)==1&&start==1) |
|
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Thu Sep 20, 2012 12:19 pm |
|
|
this is like pulling teeth
the capacitor is not a guaranteed Debounce as we dont know its TC etc etc .
Quote: |
add a deadband before a new keypress can be acted on. |
you STILL have no clean way to deal with a fixed WAIT( aka deadband) after a key is released - and BEFORE a new one can be detected.
as i said before - your real issue is not getting the TIME DOMAIN of what you want to do.
per your title - i am not sure you even intend the 'logic' of your primary while loop in MAIN either .
LOOK closely at your code -
minus your confusion - it really ONLY does THIS:
Code: |
while (1) {
if(input(butt) { output_high(led); output_high(relay); }
ELSE { output_low(led); output_low(relay); }
}
|
You don't even need a PIC for THAT |
|
|
|