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

toggle switch

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Sep 28, 2010 2:38 pm     Reply with quote

The following program will turn on an LED when the switch is turned on,
and it will turn off the LED when the switch is turned off.
Code:

#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)

#define LED_PIN     PIN_B3
#define SWITCH_PIN  PIN_B0

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

output_low(LED_PIN);  // LED is initially set off

while(1)
  {
   if(input(SWITCH_PIN))  // Read the switch
      output_low(LED_PIN);  // Turn LED off if switch is off
   else
      output_high(LED_PIN); // Turn LED on if switch is on

   delay_ms(10);  // Debounce period
  }

}


Your switch must be connected to the PIC with a circuit similar to
the schematic below. It must have a pull-up resistor (but you could
enable internal pullups in the PIC instead of using an external resistor):
Code:

           +5v
            |
            <
            > 4.7K       
            <         ___  Switch 
To          |        _|_|_
PIC -----------------o   o------
pin                            |             
                              --- GND
                               -   
entah



Joined: 23 Sep 2010
Posts: 7

View user's profile Send private message

PostPosted: Thu Sep 30, 2010 12:49 am     Reply with quote

ok tq...i'll try first...
_________________
helpme!! Im the blind IT person!!
entah



Joined: 23 Sep 2010
Posts: 7

View user's profile Send private message

PostPosted: Thu Sep 30, 2010 1:52 am     Reply with quote

I try the first program...and it work how i want
then i try to add the switch but it can't work..so
how can i want add switch..just use that program or use another statement or program...
_________________
helpme!! Im the blind IT person!!
vinniewryan



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

View user's profile Send private message MSN Messenger

PostPosted: Thu Sep 30, 2010 2:54 am     Reply with quote

If you want it to toggle on and off with a push button, you'll want something more like this:

Code:



#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)

#define LED_PIN     PIN_B3
#define BUTTON_PIN  PIN_B0

int1 ledON=0;

//==========================================
void main()
{
   output_low(LED_PIN);  // LED is initially set off

   while(1)
   {
      if(input(BUTTON_PIN))  // Read the button
      {
         if(ledON==0)
         {
            ledON=1;
            output_high(LED_PIN); // Turn LED on button is pressed
            delay_ms(100); //delay so it doesnt turn on and off when you press the button
         }
         else
         {
            ledON=0;
            output_low(LED_PIN); // Turn LED off if button is pressed
            delay_ms(100);
         }
      }
   }
}



*Updated 9/30/2010 - fixed error "if(ledON=0)"*
_________________
Vinnie Ryan
entah



Joined: 23 Sep 2010
Posts: 7

View user's profile Send private message

PostPosted: Thu Sep 30, 2010 9:38 pm     Reply with quote

vinniewryan:-------> thanks a lot....but i want ask how if i want add the switch and led..plzzzz
_________________
helpme!! Im the blind IT person!!
Linuxbuilders



Joined: 20 Mar 2010
Posts: 193
Location: Auckland NZ

View user's profile Send private message

PostPosted: Thu Mar 24, 2011 4:19 am     Reply with quote

Code:


   case 3: //input3
      pin = In_3;
      input_buffer3_ = input(pin);
      
            if (button_3count == 255) {
               button_3count = 1;
            }


// 1. button in zero position after boot or reset - pushed on

            if ((input_buffer3_ == 0) && (button3_pressed == 0) && (button_3count == 0))  { // when button pushed and set flag not set then set the flag and run counter until button is not released
               button3_pressed = 1;
               button_3count++;
               input3shadow = 0;

               outputHigh(Buzzer_on);
               delay_custom_ms(50);
               outputLow(Buzzer_on);
            }

// 2. button released after above condition

            if ((input_buffer3_ == 1) && (button3_pressed == 1) && (button_3count != 0))  { // when button pushed and set flag not set then set the flag and run counter until button is not released
               button_3count = 0;
            }


// 3. button pushed again

            if ((input_buffer3_ == 0) && (button3_pressed == 1) && (button_3count == 0))  { // when button pushed and set flag not set then set the flag and run counter until button is not released
               button3_pressed = 0;
               button_3count++;
               input3shadow = 1;

               outputHigh(Buzzer_on);
               delay_custom_ms(50);
               outputLow(Buzzer_on);
            }


// 4. button released - reset

            if ((input_buffer3_ == 1) && (button3_pressed == 0) && (button_3count != 0))  { // when button pushed and set flag not set then set the flag and run counter until button is not released
               button_3count = 0;
            }

      break;




...this forum is useless, so many trols here with zero help to simple tasks...

enjoy
_________________
Help "d" others and then you shell receive some help from "d" others.
temtronic



Joined: 01 Jul 2010
Posts: 9174
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Mar 24, 2011 5:40 am     Reply with quote

This forum should be viewed as a resource NOT a free 'cut and paste' section for students who won't pick up the manual on the compiler and at least read some of it.Most of the questions posed here seem to be from students, looking for instant working programs for their assignments,usually due tomorrow.Another group are those that are 'programming' PICs using the Proteus simulation program.Frankly it isn't even CLOSE to a proper simulator, full of bugs, errors, and glaring omissions.Trying to solve Proteus problems would be a fulltime job.
If you want to spoon feed everyone with a 'simple task' please do,supply them with full, well documented code,tested in the real world, but they will never learn,never be able to think for themselves, or suceed in the real world.
Linuxbuilders



Joined: 20 Mar 2010
Posts: 193
Location: Auckland NZ

View user's profile Send private message

PostPosted: Thu Mar 24, 2011 5:46 am     Reply with quote

You are right, it is true however not only students are using this forum. And sometimes would be easier to adapt such example instead of thinking how the hell to do it. So far during my use of this forum I have found only one useful information here and one person really helped me what saved me lots of work. That is an idea of forum. Your job as teacher is to dig here and see if your students will use my code and give them F if they do. Another problem here is few guys who wants full code to be posted, they do nothing with it, probably collect it. They do not help for sure. For me solution can be sorted out with a little of code, I do not need monster listing. But I do understand it, they probably not and then they try to work it out with Proteus as you say. If you need real code then do it yourself, here you will not find help for sure. I have seen here just few really useful things. Rest of those posts are just rubbish made by the some people.
_________________
Help "d" others and then you shell receive some help from "d" others.
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