Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

need a push switch ON ,OFF the Led in Pic using microcontroller

Status
Not open for further replies.

thannara123

Advanced Member level 5
Joined
Jan 7, 2010
Messages
1,580
Helped
122
Reputation
244
Reaction score
114
Trophy points
1,353
Location
India
Activity points
10,382
Good morning friends
I need a pic 16f877a program in Mikroc
one switch control the led On at time one press
next press the LED will OFF
please help how can i create it

I want to operate one LED with single button switch which can operate the first press "on" and second press "Off" the LED also how to make 8 LED individually.

thankingyou
 

You should search for some tutorials, these will give you the basic of using inputs and output on the controller. This is few steps on from the blinking led tutorial.

Just search this forums there are lots of suggestions for tutorials.
 
Last edited:

why not use a variable every time u push it, increment its value.
now when compare its value; when even (0,2,4 etc) on the led. and when odd (1,3,5 etc) off the led. (or u can also use a single value 1 for on and 0 for off)
i assumed the led if off initially.
That could be one logic.
 

why not use a variable every time u push it, increment its value.
now when compare its value; when even (0,2,4 etc) on the led. and when odd (1,3,5 etc) off the led. (or u can also use a single value 1 for on and 0 for off)
i assumed the led if off initially.
That could be one logic.

The simplest way would to use a variable,


set variable up with value 1

loop
key press
xor 1, against the variable
save variable
repeat loop

the result would cycle between 1 and 0
 

thanks for reply


at last i write the program
it is working fine
thanks again


void main() {
trisa=0;
trisb=0xff;
while(1)
{
if(portb.f0==1)

porta=255;
else
{
porta=0x00;
}
}}
 

it turns on a led when button is pressed and when it is released it turns off....i need a code that when a button/switch is pressed,Led continuosly glow and when it is off, Led turns off...

I don't understand you.

it turns on a led when button is pressed and when it is released it turns off....

is the same as

i need a code that when a button/switch is pressed,Led continuosly glow and when it is off, Led turns off...
 

As long as the button is closed the LED will be ON and as soon as the switch opens the LED turns OFF. That is what I have done. I have used TACT switch in Proteus. You can replace it with SPST switch or you can see that TACT switch in Proteus has + and + buttons. It is for locking and unlocking the switch. Click the + and button will close and lock. LED will be ON and turns OFF only when you open the switch by clicking - of button.
 
Please also take care of switch bounce i.e physical switch transition between ON/OFF state causes some oscillations during transition.
 

Anyone can help pls

Hi all. I'm a newbie in PIC and i do this project

Input :

Port A0-A5
Port C0-C2

Output :

PortB

When i push a button form 1-9 Pic will output random on PortB. This is program i have written. I have simulation on Proteus, it working but sometime when i push button, output is blinking, not constant. Is there wrong in this program?

And when I download program to real chip PIC 16F886, it not working. Pls help me make good and clear program. Many thanks !

Hi all. I'm a newbie in PIC and i do this project

Input :

Port A0-A5
Port C0-C2

Output :

PortB

When i push a button form 1-9 Pic will output random on PortB. This is program i have written. I have simulation on Proteus, it working but sometime when i push button, output is blinking, not constant. Is there wrong in this program?

And when I download program to real chip PIC 16F886, it not working. Pls help me make good and clear program. Many thanks !

#include <16F886.h>

#use delay(crystal=20000000)

#define sw1 PIN_A0
#define sw2 PIN_A1
#define sw3 PIN_A2
#define sw4 PIN_A3
#define sw5 PIN_A4
#define sw6 PIN_A5

#define sw7 PIN_C0
#define sw8 PIN_C1
#define sw9 PIN_C2

#FUSES NOWDT //No Watch Dog Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O

void main()
{

while(TRUE)
{
//sw1
if(!input(sw1))
OUTPUT_B(0x70);

///sw2
else if(!input(sw2))
OUTPUT_B(0x0C);

///sw3
else if(!input(sw3))
OUTPUT_B(0x04);

///sw4
else if (!input(sw4))
OUTPUT_B(0x07);

///sw5
else if (!input(sw5))
OUTPUT_B(0x02);

///sw6
else if (!input(sw6))
OUTPUT_B(0x05);


///sw7
else if (!input(sw7))
OUTPUT_B(0x0E);

///sw8
else if (!input(sw8))
OUTPUT_B(0x0B);

///sw9
else if (!input(sw9))
OUTPUT_B(0x0A);

else
OUTPUT_B(0x00);
}

}



**broken link removed**
 

You are testing for !input(SWx) if no switches are pressed and all inputs are low then NOT of inputs will be true so all the if conditions execute one by one and repeats in every loop of while(1). When multiple switches are used you should use a condition like


Code C - [expand]
1
if( (input(sw1)) && ((!input(sw2)) && (!input(sw3)) && (!input(sw4)) && (!input(sw5)) && (!input(sw6)) && (!input(sw7)) )



You should use 7 such if() conditions for each switch.

It works like


Code C - [expand]
1
if( (1) && (1) && (1) && (1) && (1) && (1) && (1) ) //then Turn ON the required LEDs.

 

Good morning friends
I need a pic 16f877a program in Mikroc
one switch control the led On at time one press
next press the LED will OFF
please help how can i create it

I want to operate one LED with single button switch which can operate the first press "on" and second press "Off" the LED also how to make 8 LED individually.

thankingyou

Hi all,

Example:

Code:
    if (PORTB.B0 = 1)
          {
          LATC.B1 = ~LATC.B1;
          }

When button is pressed "~" change current state of port bit.

Dont forget to set initial port state with one line of code.


Use MikroC button library.



Best regards,
Peter

:wink:
 

Hi peter, their question is not toggling they just need ON and off the original question was posted 2 years ago and me too answered it..........
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top