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.

Togglin LED with Switch

Status
Not open for further replies.

karan123

Member level 3
Joined
Feb 2, 2009
Messages
55
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,710
Hello,

I am struggling to Toggle LED with Switch with PIC18F4520.I have written below code. but LED is not toggling properly.

; SW Connected with 4.7 K Pull up Resistance

ORG 0
MOVLF 0xFF
MOVWF ADCON1 ; CONFIGURE all pins Digital
BSF TRISB,1 ; Configure as Input PIN
BCF TRISB,4 ; Configure as Output PIN
AGAIN
BTFSS PORTB,1 ; BIT TEST RB7 for HIGH
GOTO OVER ; ONLY if LOW
BTG LATB,4 ; LED=0 ON
BRA AGAIN
OVER
BRA AGAIN
END


It some time toggle and sometime not, some time light glow.

What the reason?

Thanks

--
Karan
 

I actually have no experience with ASM.
But 'i think' in ur case,
if u make a single key press, then also, it may toggle many times because u cant keep the switch ON for such a very small interval ie u need to press key and after checking the status, then the switch must be OFF at least just before the next switch status check, but this is practically impossible. So , now , it will toggle like "TOSSING A COIN", 'i think so'

Then, what u need is, u need to introduce a delay if u detected a switch ON . U can give this aprox 50ms so that i think it will work . But still, if u press the switch continuously then it will toggle continuously with delay 50ms.

More over, since it is a mechanical switch, it will 'bounce' for a small interval of time after switching ON. At these interval, the status of switch can be 1 or 0. (random). So u must be bothered about these things....
 

Karan

You did not implemented some debouncing feature to avoid switch flicking.
So, output status will be not predictable.

+++
 
  • Like
Reactions: IanP

    IanP

    Points: 2
    Helpful Answer Positive Rating
To toggle a led WITH as switch, the only parts needed are: Voltage source, Resistor (if a voltage source is higher than forward LED voltage), LED, and, of course, switch :)
 

A mechanical push-button switch may generate many short pulses when pressed then released.

The MCU loop is fast enough to catch them all so the LED output will act as a single bit random generator after each press.

So let us suppose that the pin is high normally.
When it becomes low:

The LED toggles

The program enters a 50ms delay for example while checking the pin status. In this delay loop, anytime the pin returns to low the delay counter is reset. In other words, to exit this loop, a continuous high state for 50ms should be detected.

Then the program returns to the main loop waiting for the pin to be low again.

Kerim
 

Hi,



I have also included CALL_DELAY50MS, but not Toggling Properly

ORG 0
MOVLF 0xFF
MOVWF ADCON1 ; CONFIGURE all pins Digital
BSF TRISB,1 ; Configure as Input PIN
BCF TRISB,4 ; Configure as Output PIN
AGAIN

BTFSS PORTB,1 ; BIT TEST RB1 for HIGH
BRA AGAIN
CALL_DELAY50MS
AGAIN1
BTFSS PORTB,1 ; BIT TEST RB1 for HIGH
BRA AGAIN1
GOTO OVER ; ONLY if LOW
BTG LATB,4 ; LED=0 ON
BRA AGAIN
OVER
BRA AGAIN
END


It some time toggle and sometime not, some time light glow, sometime
flickering?
Any suggestion to sort out this problem
 

Code:
#include <pic.h>
#define _XTAL_FREQ 20e6
__CONFIG(0x3F3A);
unsigned char count=0;
main()
{ ADCON1=0b00001111;    //to make
PORTE as digital I/O pins.
TRISD=0;
TRISE0=1;
TRISE1=1;
while(1)     {
    if(RE0==1)
        {
        if(count<255){count++;}
        __delay_ms(50);
        while(RE0==1);         __delay_ms(50);
        }     if(RE1==1)
        {
        if(count>0){count--;}
        __delay_ms(50);
        while(RE1==1);
        __delay_ms(50);         }
    PORTD=count;
    }
}
Above is an UP / DOWN counter program controlled by two push buttons. In my case switch on means logic 1. Any way, just try to understand it and then you can modify your ASM.
 

AGAIN

BTFSS PORTB,1 ; BIT TEST RB1 for HIGH
BRA AGAIN
CALL_DELAY50MS
AGAIN1
BTFSS PORTB,1 ; BIT TEST RB1 for HIGH
BRA AGAIN1
GOTO OVER ; ONLY if LOW
BTG LATB,4 ; LED=0 ON
BRA AGAIN
OVER
BRA AGAIN
END

First:
I am afraid you need to write a subroutine for the 50ms delay which can reset its timer (its loop counter for example) whenever the pin is low.
Here it is just a delay.

Second:
Right after the main loop detects a low state, it is better to toggle the LED first then you call your delay subroutine (that checks low state to reset its time to zero).

Third:
You can increase the delay till you get a good reponse. Perhaps 50ms is short for your switch glitches.

Above is edited.
 
Last edited:

Try this


processor definition
config reg

#define LED, PORTB,0

CBLOCK H'20'

delay1
delay2

ENDC

org 0
goto main
org 8

init:
bcf TRISB,0
bsf TRISB,1
clrf PORTB

return


*/ For a 4 MHz crystal oscillator(Delay routine)

nsec_a:
movlw D'100'
movwf delay1
nsecloop_a:
nop
nop
nop
nop
nop
nop
nop
nop
nop
clrwdt
decfsz delay1
goto nsecloop_a
return

nsec1a:
call nsec_a
decfsz delay2
goto nsec1a
return

msec50a:
movlw D'50'
movwf delay2
goto nsec1a

msec250a:
movlw D'250'
movwf delay2
goto nsec1a


main:
call init
loop:
btfsc PORTB,1
goto loop
call msec50a */Debounce delay
btfsc PORTB,1
goto loop

here:
bsf LED
call msec250a
bcf LED
call msec250a

goto loop

end
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top