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 support@ccsinfo.com

push button

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



Joined: 10 Jul 2011
Posts: 54

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

push button
PostPosted: Sun Jul 31, 2011 5:08 am     Reply with quote

Hi there.
I am new in PIC programming.
I want to make a lcd counter with a push button in following condition...

1. I want to count in each push.
2. If I push and hold it nothing count until I release the push button.

Need your help

my code as follows
Code:
#include <16F877A.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#include <flex_LCD420.c>   


//===================================
void main()
{

float y=0 ;
lcd_init();

// Clear the LCD.
printf(lcd_putc, "\f");
delay_ms(100);

while(1)
  {
   lcd_gotoxy(4, 4);
   if (input(pin_D1)==1) y=y+1;
   printf(lcd_putc, "%3.0f",y);
 
    delay_ms(100);
}
}


Last edited by freedom on Tue Aug 02, 2011 1:19 pm; edited 1 time in total
SherpaDoug



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

View user's profile Send private message

PostPosted: Sun Jul 31, 2011 5:50 am     Reply with quote

I would use a "state machine" with two states, 'in' and 'out'.

When in state 'out' wait for the button to be pushed in for 100ms, then increment the count and go to state 'in'. If the button is let out before 100ms remain in state 'out'.

When in state 'in' wait for the button to be let out for 100ms, then go to state 'out'. If the button is pushed in before 100ms remain in state 'in'.

The 100ms delay is a simple debouncing technique for mechanical switches. There are more sophisticated methods, but you need to know more about just how your switch behaves before you get too complex.

The state machine technique is very powerful and if you use more states with clearly defined rules to go from one state to another you can do very complex things with great reliability.
_________________
The search for better is endless. Instead simply find very good and get the job done.
freedom



Joined: 10 Jul 2011
Posts: 54

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

PostPosted: Sun Jul 31, 2011 8:39 am     Reply with quote

Thanks SherpaDoug for your kind reply.

Actually I'm going to develop a machine where I need to use it. Actually a inductive proximity sensor will be used there which is acted as similar as push button.

In my operation, there are some situation where the push switch in hold for 10 sec or 30 sec or 2 min or 30 min or etc.

In my above mentioned code , when i press its counting. But problem is that I want count only one time in each press, If I hold it then do nothing until I release the switch and push it again.

Waiting for your help.
Jhonny



Joined: 30 Jan 2011
Posts: 16

View user's profile Send private message

PostPosted: Sun Jul 31, 2011 1:29 pm     Reply with quote

Here is my version. There is a parameter that monitors the key is released and pressed (status). I hope you understand ...

Code:
    if (!input (PIN_A4) && status==0) //input button
    {
      status=1;
      delay_ms(100);           
      count++;
    }
    if (input (PIN_A4))
    {
    status=0;
    }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 31, 2011 1:30 pm     Reply with quote

Here is the "Button Command" code that I wrote for the Code Library:
http://www.ccsinfo.com/forum/viewtopic.php?t=23837

Here's a version of it that polls the buttons during a timer interrupt:
http://www.ccsinfo.com/forum/viewtopic.php?t=31849

This version stores the button presses in a buffer, which can be read:
http://www.ccsinfo.com/forum/viewtopic.php?t=39585&start=1

This code can detect if the button is pressed, held, released or up:
http://www.ccsinfo.com/forum/viewtopic.php?t=42027
SherpaDoug



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

View user's profile Send private message

PostPosted: Sun Jul 31, 2011 6:38 pm     Reply with quote

With my state machine suggestion it will only count a single count when it goes from state 'out' to state 'in'. You can hold the button in all day and you will only get one count because you only ENTER state 'in' once. It can't make a second count until it exits 'in' and enters 'out' and exits 'out' and enters 'in' again. One count per button press.
_________________
The search for better is endless. Instead simply find very good and get the job done.
freedom



Joined: 10 Jul 2011
Posts: 54

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

PostPosted: Mon Aug 01, 2011 2:34 am     Reply with quote

Thanks a lot Jhonny, PCM programmer and SherpaDoug for your valued reply.

I'm going to try Jhonny's code. I'll be back very soon what happened.

Telling to SherpaDoug , would you like to show code example as I am new in PIC programming.
freedom



Joined: 10 Jul 2011
Posts: 54

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

push button
PostPosted: Mon Aug 01, 2011 1:19 pm     Reply with quote

Thanks a lot Jhonny

it is working
SherpaDoug



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

View user's profile Send private message

PostPosted: Mon Aug 01, 2011 3:31 pm     Reply with quote

Jhonny's code would be a true state machine with the addition in bold:

if (input (PIN_A4) && status==1)

But as you can see, in this trivial case it is not needed.
_________________
The search for better is endless. Instead simply find very good and get the job done.
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