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.

button and led with 16f84a

Status
Not open for further replies.

MARWEN007

Junior Member level 2
Joined
Apr 16, 2011
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,481
Hi to all I want to achieve a small C program with 16F84A like so:
when I press a botton one LED lights
and when I release the botton switches This led turn off??!
* take a0 and b0 is an entry is there anyone out there who help me pouver
I make a small code but the LED remains lit after Still pressing botton
Code:
void main() {

debut:
TRISB = 0x00;
PORTB = 0;

TRISA = 0xff;
PORTA = 0;
   while  (1){
if(portA.f0==0)
 { portB.f0=0;}
  else if (portA.f0==1)
  { portB.f0=1;}
  else {goto debut;}
}

}
 

A digital pin can only be in one of two states so you can simplify it like this:

Code:
void main() 
{
	TRISB = 0x00;
	PORTB = 0;

	TRISA = 0xff;
	PORTA = 0;
   
	while(1)
	{
		if(portA.f0==0) portB.f0=0;
		else  portB.f0=1;
	}

}

Brian.
 

thank you very much but the LED should be off when the button is at rest!
a0 = 0 ; led = 0
a0 = 1 ; led = 1
a solution?
 

That's what it does... when portA.f0 is 0, portB.f0 is 0, and when portA.f0 is 1, portB.f0 is 1. Do you need it to do the inverse?
 

I suppose it really depends on which way the LED is connected. The way I showed and in your original code, it assumes the pin is high to turn the LED on, in other words the PIC is providing the LED current. If you have the LED wired so the PIC sinks current, the logic will be reversed. If that's what is happening, change "if(portA.f0==0)" to "if(portA.f0==1)" and it should work the other way around.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top