View previous topic :: View next topic |
Author |
Message |
entah
Joined: 23 Sep 2010 Posts: 7
|
toggle switch |
Posted: Thu Sep 23, 2010 11:44 am |
|
|
How to make program if have
to ON LED and OFF LED? Use just one switch and looping again. (I mean what statement must used and how)...plzzzzzz... I'm new in this program. _________________ helpme!! Im the blind IT person!! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
entah
Joined: 23 Sep 2010 Posts: 7
|
|
Posted: Tue Sep 28, 2010 6:32 am |
|
|
but if the switch is toggle wanna to use like push button...how..??? _________________ helpme!! Im the blind IT person!! |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Tue Sep 28, 2010 6:42 am |
|
|
use a j-k flipflop.
cheap and no coding!!! _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 28, 2010 2:38 pm |
|
|
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
|
|
Posted: Thu Sep 30, 2010 12:49 am |
|
|
ok tq...i'll try first... _________________ helpme!! Im the blind IT person!! |
|
|
entah
Joined: 23 Sep 2010 Posts: 7
|
|
Posted: Thu Sep 30, 2010 1:52 am |
|
|
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
|
|
Posted: Thu Sep 30, 2010 2:54 am |
|
|
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
|
|
Posted: Thu Sep 30, 2010 9:38 pm |
|
|
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
|
|
Posted: Thu Mar 24, 2011 4:19 am |
|
|
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: 9241 Location: Greensville,Ontario
|
|
Posted: Thu Mar 24, 2011 5:40 am |
|
|
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
|
|
Posted: Thu Mar 24, 2011 5:46 am |
|
|
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. |
|
|
|