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

switch control

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







switch control
PostPosted: Mon Jun 15, 2009 5:17 am     Reply with quote

Hello,
I have built a PIC circuit that is controlled by four switch inputs. I want to press one of the switches and increment a counter from 1 - 7 each time it is pressed regardless of how long it is held for (and then reset to 1 when it reaches 7). The counter will then control a display cursor on an lcd module.

At the moment I can press a switch and read whether it is in an on or off state but I just cant seem to get my head round the code that I need to achieve the counter function.

I know this sounds silly cause I am sure it is quite simple. I am pretty new to this whole thing and I am not asking for written code particularly just an explained approach or tips or advice? Any help would be much appreciated, cheers
SherpaDoug



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

View user's profile Send private message

PostPosted: Mon Jun 15, 2009 6:41 am     Reply with quote

What you need is the software construct called a "state machine". Google "state machine" and you should find more than you could possibly need to know ;-)
_________________
The search for better is endless. Instead simply find very good and get the job done.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Mon Jun 15, 2009 9:09 am     Reply with quote

This may not be the best way to do this but it works. We'll assume you're using A0 as your input and it is active LOW when you press the switch. Now, I've not added any debouncing to this.

Code:
int1 entered = 0;
int8 counter = 0;

if(input_low(PIN_A0) && !entered)
{
  entered = 1;// change state so we don't re-enter until we've released
  if(++counter > 7)//the button first
  {
    counter = 1;
  }
}

if(entered && input_high(PIN_A0))
{
  entered = 0;// reset flag so we can wait for the next button press
}


This is only one way to accomplish this.

Ronald
QBoxer
Guest







PostPosted: Tue Jun 16, 2009 4:22 am     Reply with quote

Hello,

Thank you for that rnielsen, have adapted and got it more or less working perfectly for my circuit (it seems a little bit temperamental sometimes -don't know why this would be?)

Also thank you for the pointer to the state machine approach SherpaDoug, I would never have thought of that and even though some of it above my head, I can see it is very good approach, especially for the finally software and actually the program is a lot easier to think about in that way. Cheers Smile
SherpaDoug



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

View user's profile Send private message

PostPosted: Tue Jun 16, 2009 6:03 am     Reply with quote

Being "a little temperamental" sounds like you need switch debouncing. Any mechanical switch is likely to produce a series of short make and breaks as the metal contacts collide and microscopically bounce. If you read the switch fast enough you may see more that one electrical switch closure for a single mechanical activation.

Search on this forum for "debounce" for some ideas to deal with this.
_________________
The search for better is endless. Instead simply find very good and get the job done.
QBoxer
Guest







PostPosted: Tue Jun 16, 2009 9:18 am     Reply with quote

Added debouncing code it is working perfectly. Thank you very much for your help!
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue Jun 16, 2009 9:37 am     Reply with quote

Quote:
if(input_low(PIN_A0) && !entered)


Stupid me (what else is new?). Morphed input & output commands. Should have been

Code:
if(!input(PIN_A0) && !entered)


But, hopefully you saw that little goof and got it to work correctly.

Ronald
PROCRASTINATORS UNITE!!!........ tomorrow
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