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

using switch

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



Joined: 08 Jan 2011
Posts: 18

View user's profile Send private message

using switch
PostPosted: Tue Feb 01, 2011 6:05 pm     Reply with quote

Hi all,

I have a small switch as above (not actually it but similar).

Now I want to try external interrupt using this switch. It has 3 wires, I connect the one at the leftmost to the +5v pin of PIC16f877, the one at the rightmost to the ground pin of pic, and the one at the middle to RB0 pin. Using it, I toggle the state of a Led. I have 2 problems:

1) Initially the led is on. When I press the switch, led becomes off, when I release the switch the led becomes on again. I only want it to change state when I press the switch, not release. How to achieve that?
(if you want I can post my small piece of code).

2) L7805CV in my circuit becomes very hot when I try things above for 2 minutes, which is normally not so hot. Am I connecting the wires wrong?Or should I use an external voltage source for the switch?

Thanks in advance
vinniewryan



Joined: 29 Jul 2009
Posts: 154
Location: at work

View user's profile Send private message MSN Messenger

PostPosted: Tue Feb 01, 2011 6:44 pm     Reply with quote

If you want this switch to 'toggle' the LED on and off, you have to write a function that checks for an input from the switch, changes the state of the LED, then waits until the switch is released to reset, otherwise it will toggle many times in the instant you press it. Something like this should do the trick:

Code:

#define PIN_XX LED
#define PIN_XX SWITCH_PIN
int1 button=0;

if(!input(SWITCH_PIN))
{
   if(button==0)
   {
      button=1;
      if(ledon==1)
      {
         ledon=0;
         output_low(LED);
      }
      else
      {
         ledon=1;
         output_high(LED);
      }
   }
}
else
{
   button=0;
}


This is just a working example. Is your switch ON-MOM or OFF-MOM?
_________________
Vinnie Ryan
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Tue Feb 01, 2011 7:59 pm     Reply with quote

You also need a delay in there for sensing the switch position - mechanical switches are notorious for "bouncing" - the contacts bounce several times when they first hit so you need to provide "debouncing" for the switch if you don't want the processor to see close/open/close/open/close/open ... in the first few ms when you press the switch. The fact that the regulator gets hot makes it sound like you are possibly shorting the regulator out with the switch - if there are 3 terminals on the switch, one should be N.O. (normally open), one N.C. (normally closed) and a common terminal - the common should go to the input of the pic, the NO to gnd and the NC to +5 if that is what you are trying to do - it is better to have some resistance in there to limit any current flow though. Once you have debounced the switch, you do need to wait for it to open again before waiting for the next close. If the processor also becomes hot when the regulator does, I would suspect you are trying to drive a pin configured as output. On the other hand, if only the regulator is getting hot, I think you will find you have the switch wired to short out the 5 volts to ground when you close it.

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
bigfish



Joined: 08 Jan 2011
Posts: 18

View user's profile Send private message

PostPosted: Wed Feb 02, 2011 6:45 am     Reply with quote

Hi vinniewryan, thanks for the reply.
I will try your code. By the way, I don't know what is On-mom or off-mom.
Throughout the motion, this "pushing" action will occur only one time, so instead of checking it continuously, should I use interrupt? Probably you are much more experienced than me so I am open to suggestions.

vinniewryan wrote:
If you want this switch to 'toggle' the LED on and off, you have to write a function that checks for an input from the switch, changes the state of the LED, then waits until the switch is released to reset, otherwise it will toggle many times in the instant you press it. Something like this should do the trick:

Code:

#define PIN_XX LED
#define PIN_XX SWITCH_PIN
int1 button=0;

if(!input(SWITCH_PIN))
{
   if(button==0)
   {
      button=1;
      if(ledon==1)
      {
         ledon=0;
         output_low(LED);
      }
      else
      {
         ledon=1;
         output_high(LED);
      }
   }
}
else
{
   button=0;
}


This is just a working example. Is your switch ON-MOM or OFF-MOM?
bigfish



Joined: 08 Jan 2011
Posts: 18

View user's profile Send private message

PostPosted: Wed Feb 02, 2011 6:52 am     Reply with quote

Hi gpsmikey, thanks for your reply, too.
I think you are right about that open-close-open-close-open-close action due to debouncing. Pic16f877 does not get hot, only regulator gets hot, and very rapidly.

Yes there are NC,NO,C signs near to 3 wires on the switch. (I didnt know their meanings.) I may be wired them wrong, I will try connecting resistance in between as you said.
In fact, I didn't configured that pin as neither output nor input I will try to add settris to my code. Again thanks for the reply.


gpsmikey wrote:
You also need a delay in there for sensing the switch position - mechanical switches are notorious for "bouncing" - the contacts bounce several times when they first hit so you need to provide "debouncing" for the switch if you don't want the processor to see close/open/close/open/close/open ... in the first few ms when you press the switch. The fact that the regulator gets hot makes it sound like you are possibly shorting the regulator out with the switch - if there are 3 terminals on the switch, one should be N.O. (normally open), one N.C. (normally closed) and a common terminal - the common should go to the input of the pic, the NO to gnd and the NC to +5 if that is what you are trying to do - it is better to have some resistance in there to limit any current flow though. Once you have debounced the switch, you do need to wait for it to open again before waiting for the next close. If the processor also becomes hot when the regulator does, I would suspect you are trying to drive a pin configured as output. On the other hand, if only the regulator is getting hot, I think you will find you have the switch wired to short out the 5 volts to ground when you close it.

mikey
bigfish



Joined: 08 Jan 2011
Posts: 18

View user's profile Send private message

PostPosted: Wed Feb 02, 2011 7:20 am     Reply with quote

I tried as your advice, I connected "C" to input pin, NC to +5v of PIC, NO to ground of PIC with a resistance. Now its working, regulator is not getting hot. Thank you very much....
vinniewryan



Joined: 29 Jul 2009
Posts: 154
Location: at work

View user's profile Send private message MSN Messenger

PostPosted: Wed Feb 02, 2011 3:02 pm     Reply with quote

On-mom and off-mom are configurations for a switch, but I misread your information and you answered my question in the first post.

Using an interrupt is generally a much more resourceful way to monitor a button, especially when your program is time critical.
_________________
Vinnie Ryan
bigfish



Joined: 08 Jan 2011
Posts: 18

View user's profile Send private message

PostPosted: Fri Feb 04, 2011 9:54 pm     Reply with quote

Thanks, I used interrupt.
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