The Idea is right but you must have some form of debounce code try something like this> flags equ 10h flags register sw equ 0 bit ra0 on port led equ 1 bit ra1 on port same equ 1 store result in same register w equ 0 store result in w register switch btfsc porta,sw test for switch pressed goto switch switch not pressed decfsz counter,same time how long switch is held goto switch not held long enough loop back movlw 255 reload debounce timer movwf counter btfsc flags,0 test led status goto ledoff switch led off bsf flags,0 set led status on bsf porta,led switch on led goto test1 go test if switch released ledoff bcf flags,0 set led status off bcf porta,led switch led off test 1 btfss porta,sw test if switch released goto test1 switch not released goto switch switch released return to switch Regards Mark Esilva saver@pta.lia.net -----Original Message----- From: David Olson To: PICLIST@MITVMA.MIT.EDU Date: Sunday, April 11, 1999 5:52 AM Subject: Newbie Toggle Switch Woes >I'm working on the PIC equivalent of "Hello World" and using stuff from the >Easy PIC'n book and a PICDEM-1. Now I realize that routines in the book are >not for the PICDEM-1, which may be where my troubles start - at this point, >I label the PICDEM as moderately useless. Anyway... I'm trying to get a >toggle switch routine to work. I've created a register for the on/off status >and it appears that the bits are being set and cleared properly. > >The problem comes when the button is pressed. RA1 is clearly pulling low and >the LED will come on for a few seconds - then it shuts off. It's behaving >like the switch is being pressed again. > >Am I missing a control-of-flow point here with the btfsc's or does my PICDEM >board have some bizarre behavior? - like a leak? > >Here's my code: > > list p=16f84 > radix hex > >porta equ 0x05 >portb equ 0x06 >swst equ 0x0c ;switch status register >st equ 0 ;switch bit > > org 0x000 > >start movlw b'11111111' ;set porta for input > tris porta > movlw b'11110111' ;set bit 3 for output > tris portb > clrf swst ;clear switch register > > bcf portb,3 ;turn the LED off > >switch btfsc porta,1 ;is the switch pressed? > goto switch ;no, check again > btfsc swst,st ;is this already on? > goto off ;yes, turn it off > >on bsf portb,3 ;turn the LED on > bsf swst,st ;set the status bit > goto switch ;go wait for another press > >off bcf portb,3 ;turn the LED off > bcf swst,st ;clear the status bit > goto switch ;go wait for another press > > end >