GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Feb-2012, 14:51
harami harami is offline
New Member
 
Join Date: Feb 2012
Posts: 2
harami is on a distinguished road

C code for push button


how do you write a code in C language for push buttons. im using two push buttons as an input to pic18f452. push buttons will be pressed one after another. and when push button A is pressed followed by push button B, number will increment and if button B is pressed followed by button A number will decrement. any suggestions/help??
  #2  
Old 01-Mar-2012, 17:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C code for push button


Quote:
Originally Posted by harami
how do you write a code in C language for push buttons. im using two push buttons as an input to pic18f452. push buttons will be pressed one after another. and when push button A is pressed followed by push button B, number will increment and if button B is pressed followed by button A number will decrement. any suggestions/help??
Here's a possible description of the problem:

Two Inputs : Switch A and Switch B.
Two Outputs: "Increment" and "Decrement"

Define a (Moore) State machine as follows:
There are seven states: Idle, A1, A2, AB, B1, B2, BA

We are assuming that there is no contact bounce for the switches, so that values for Pushed and not(Pushed) can be obtained by reading the input pin assigned to a switch.

Bounce can be handled by separate state machines for the switches, but will not be addressed for starters.

Code:
Starting from Idle: If A is pushed when in Idle state, here's the sequence: State A1: A has been pushed; waiting for A to be released. A2: A has been pushed and released; waiting for B to be pushed. AB: A has ben pushed and released; B has been pushed; waiting for B to be released, then it goes back to Idle. If A has not been pushed in Idle state and B is pushed, here's the sequence: State B1: B has been pushed; waiting for B to be released. B2: B has been pushed and released; waiting for A to be pushed. BA: B has been pushed and released; A has been pushed; waiting for A to be released, then it goes back to Idle. When in state A2 and B is pushed, output the "increment" pulse signal. When in state B2 and A is pushed, output the "decrement" pulse signal.

A state transition table defines how to get from one state to another and what to do for each transition. This is how hardware guys do the deed:

Code:
================================================================== Present State Logic Condition Next State/Output ------------------------------------------------------------------ Idle Not(Apushed) and Not(Bpushed) Idle/Nothing Idle Apushed A1/Nothing Idle Not(Apushed) and Bpushed B1/Nothing A1 Apushed A1/Nothing A1 Not(Apushed) A2/Nothing A2 Bpushed AB/Increment A2 Not(Bpushed) A2/Nothing AB Bpushed AB/Nothing AB Not(Bpushed) Idle/Nothing B1 Bpushed B1/Nothing B1 Not(Bpushed) B2/Nothing B2 Apushed BA/Decrenent B2 Not(Apushed) B2/Nothing BA Apushed BA/Nothing BA Not(Apushed) Idle/Nothing ==================================================================
A C-flavored pseudo-code description could go something like this:

Code:
Program to implement state machine: Define values for seven states. Define binary value for Pushed, depending on how the switches are wired. (Pushed might be '1' or Pushed might be '0') Declare a variable to hold the state value, and a variable to hold the "next_state" value. Define a variable for "Increment" and a variable for "Decrement." Initialize them to zero. Do setup for signal pin assignments and direction assignments. Set state to Idle. Loop Forever: BEGIN LOOP switch(state) { case Idle: IF A is Pushed THEN next_state = A1 ELSE IF B is Pushed THEN next_state = B1 ELSE next_state = idle. END IF case A1: IF A is Pushed THEN next_state = A1 ELSE Increment = 1 next_state = AB END IF Break case AB: IF B is Pushed THEN next_state = AB ELSE next_state = Idle Break. . . . . } // End of the switch statement IF Increment is equal to 1 THEN Pulse the Increment output pin Increment = 0 END IF IF Decrement is equl to 1 THEN Pulse the Decrement output pin Decrement = 0 END IF state = next_state END LOOP // It goes on forever unless you put additional stuff // inside the loop to let it do something else...


Regards,

Dave

Footnote:
My stab at the state machine is for starters. I didn't actually write the code and test it using this, so make sure you understand what it does for all possible input sequences.

For example, suppose it's in idle. The user presses 'A' and then presses 'B' and then releases 'B' and then releases 'A'. What do you want the system to do? Does my state machine do that? If you want it to do something other than my example, you have to re-write the state machine. (May need some additional states.) A more complete treatment of more complicated sequences might be found in references for "Finite Automata" or some such thing.

A real system specification might actually start with the state machine and all you have to do is write the code. My loop based on the state transition table, is one way do to it in software. (Easy-peasy-lemon-squeezy, right?)
__________________
Sometimes I just can't help myself...
  #3  
Old 02-Mar-2012, 09:23
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C code for push button


Quote:
Originally Posted by davekw7x
...(Easy-peasy-lemon-squeezy, right?)
There are a couple of discrepancies (bugs) between the description and pseudo-implementation. I did not introduce them deliberately, but I'm going to leave them for now. After all, we don't know what the real requirements are for the original question.

If anyone is interested in actually implementing this stuff, my suggestion is to start with a clean sheet of paper. Using my suggestions as a starting point (or not). Create a state transition table and/or a state diagram and/or pseudo-code that describes your requirements.

If there are questions on how to complete the job, post what you have and tell us what you don't understand.

Having said that, I have to say:

This forum is most useful for people needing help with programs running on platforms such as PC workstations and using standard C compilers.

Embedded systems consisting of small microprocessors (maybe not running under operating systems) commonly have other considerations (concept of :real" time, concept of apparent concurrency, concept of physical input/output) that may not be easy to address by potential helpers here.

Some of the concepts commonly used by embedded systems programmers (state machines to implement behavior for certain specific input sequences) may be useful to workstation programmers, and I don't mind trying to convey my opinions, based on my personal experience, but there are limits on what we can accomplish here.


Regards,

Dave
__________________
Sometimes I just can't help myself...
Last edited by davekw7x : 02-Mar-2012 at 10:03.
  #4  
Old 02-Mar-2012, 14:20
harami harami is offline
New Member
 
Join Date: Feb 2012
Posts: 2
harami is on a distinguished road

Re: C code for push button


Quote:
Originally Posted by davekw7x
Here's a possible description of the problem:

Two Inputs : Switch A and Switch B.
Two Outputs: "Increment" and "Decrement"

thank you dave for your help.the way you have done made it a lot easier and clear now.. however if im to display the incremented/decremented number (0-9) on a seven segment LED display, how would i implement it??
  #5  
Old 02-Mar-2012, 16:16
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C code for push button


Quote:
Originally Posted by harami
...display the incremented/decremented number (0-9) on a seven segment LED display,,,

My suggestion:
Get the data sheet and/or schematic for whatever development system you are using. Look (on http://www.microchip.com) for examples and/or application notes about 7-segment display software routines for the class of MCU that you are using. There are also Microchip forums that might be more tuned in to your situation, and you might be able to get specific help.

My opinion:
[/begin opinion]
It's just too hardware-specific for this forum, and unless someone has the exact same hardware and compiler that you are using, we would just be blowing smoke...
[/end opinion]

Regards,

Dave
__________________
Sometimes I just can't help myself...
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing button text sudox FLTK Forum 4 05-Mar-2011 07:26
Change the caption on button when clicked drewdaman MS Visual C++ / MFC Forum 5 31-May-2009 20:56
FLTK && fluid In Motion cable_guy_67 FLTK Forum 4 20-Mar-2008 03:52
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 15:41
Code Library Elmseeker PHP Code Library 0 13-Jan-2003 07:58

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:15.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.