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

question about status of pin

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



Joined: 06 Nov 2010
Posts: 22
Location: montreal qc

View user's profile Send private message Send e-mail

question about status of pin
PostPosted: Fri Oct 12, 2012 4:34 am     Reply with quote

Hi I'm newbie I'm trying to do something like...if an input (ex:RA4)
is high or low.

If my input is high nothing happened,
but if my input RA4 is low more than 3 second then call a function.
If is less then 3 seconds nothing happened.
Ex of my code:

void prob (void)
{
if(input(PIN_A4))
{
delay_ms(3000);
output_high(LED2);
output_low(LED1);
zap:
lcd_putc('\f');
lcd_gotoxy(1,1);
printf(lcd_putc,"PROBLEM");
delay_ms(5000);
goto zap;
}

Thanks for help!!!
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Fri Oct 12, 2012 5:31 am     Reply with quote

Using a delay for three seconds will not really do what you want. Consider what might happen if the input goes low for 1 seconds, then goes high for 1.5 seconds then goes low for anything between just over 0.5 seconds and just less than 3 seconds. Your code would incorrectly think it was low for the full three seconds, which is NOT what you state you want.

What you have to do is detect the pin going low, then start some sort of timer and wait for it to time out. If at any time BEFORE it times out the pin goes high then you should ignore it, and go back to waiting for it to go low. If the timer (however it is implemented) times out without the pin having gone high then you declare it a valid detection and do whatever you need to do.

The problem is that if you are polling the pin while you wait, then there will always be a finite non-zero time depending on the polling interval, during which the pin could have changed state without you knowing about it. To get round this you might be tempted to use an interrupt on change pin, but this suffers from the same problem to a limited extent as the code will be blind to changes while the ISR is running.

However as I see you are using a goto (!) I doubt any of this will matter to you and you'll just use a delay anyway. Shocked

RF Developer
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Fri Oct 12, 2012 5:46 am     Reply with quote

Read the guidelines on posts.

Use the code button to preserve indentation etc.

Avoid goto like the plague.

I suggest you poll the button every say 1 to 10ms with an interrupt. That way you deal with the issues associated with switch bounce, those raised by RF_Developer, and allow your code to do something else at the same time.

Mike

EDIT. A search on this forum will yield loads of code to achieve your objective.
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