View previous topic :: View next topic |
Author |
Message |
freedom
Joined: 10 Jul 2011 Posts: 54
|
push button |
Posted: Sun Jul 31, 2011 5:08 am |
|
|
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
|
|
Posted: Sun Jul 31, 2011 5:50 am |
|
|
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
|
|
Posted: Sun Jul 31, 2011 8:39 am |
|
|
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
|
|
Posted: Sun Jul 31, 2011 1:29 pm |
|
|
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
|
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Sun Jul 31, 2011 6:38 pm |
|
|
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
|
|
Posted: Mon Aug 01, 2011 2:34 am |
|
|
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
|
push button |
Posted: Mon Aug 01, 2011 1:19 pm |
|
|
Thanks a lot Jhonny
it is working |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Aug 01, 2011 3:31 pm |
|
|
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. |
|
|
|