Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

New using PIC18F4520 with C Programming

Status
Not open for further replies.
That's outputting, so it is under your control. For sourcing,
Code:
PORTDbits.RD1 = 1;
will turn on the LED.
 
Ok, honestly I am getting more confused here. I know that PORTDbits:RD1 = 1; will turn on the led. But how do my microcontroller knows that the push button has already pressed once before?
 
Input is actually the Push Button RA4 (S2)

Output is actually the LED at PORTD

Err.... so u mean I read from the LED. If it turns ON then just keep looping the flashing command until the next button pressed?
 
mrfunkyjay said:
Input is actually the Push Button RA4 (S2)

Output is actually the LED at PORTD
Yea I know these..
Err.... so u mean I read from the LED. If it turns ON then just keep looping the flashing command until the next button pressed?
Yes, you can read the output as I said before. If you're only waiting for the button to be pressed, then you don't need to loop as the LED will stay on. Just poll the input RA4.
 
What I need now maybe:

Push the button = led will turn on Forever

Because what I have now:

If Push Button pressed = Led turn on

else = Led turn off

Btw, what is Poll???
 
ARGHHH, I am getting more and more confused!!! Can you just please give me examples how can I program my LED so that when I pressed it once it stays ON forever??? BIG thanks!
 
The code I posted above is in two parts.

First part is a way to read a key and only execute the code if the key has just been pressed. So it will execute the code in the braces once per key press.

Second part is a way to change the LEDs. If there off they go on. If there on they go off.

Put the two together and you have the functionality that you asked about.

BTW, you have changed your requirements from your earlier question.
Your original question was,
When I pressed it once, LEDS switched on forever until I pressed the push button again, then LEDS switched off.
That was the question I answered.

Mike.
 
Thanks Mike!!

I have included your code in my Sourcecode but the thing is now Previous is not defined (error). I am wondering how to define it. I mean... how?
 
Pushbuttons are mechanical devices and when you press them the contacts can bounce for a few milliseconds.
I used the PICkit2 LA tool on my Junebug kit to capture a switch bounce.
**broken link removed**
 
Now the LED is turning on as always, wherever Power Supply is connected. Hmm... let me post my code here.

Code:
#include <p18f4520.h>
#include <delays.h>

#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF

char Previous;
void main(void)

{	
	/*Settings*/
	TRISD=0b00000000;
	TRISA=0b00010000;
	ADCON1=7;
	TRISB=0b00000001;
	Previous=PORTAbits.RA4;
    if(PORTAbits.RA4==0 && Previous==1)
	{
	PORTDbits.RD1 = ~PORTDbits.RD1;
	}
}
 
You missed out the delay and therefore have no debounce and very little chance of detecting a keypress.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top