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.

simple On,Off led with switches

Status
Not open for further replies.

ryan_ryan

New Member
Hi,

Me doing some switch testing and i am just trying to turn LEDs on and off to detect a positive switch change..For ST_ST_button, i am just trying to detect whether led has been on or off, if it was off then on it, if it was on then off it..but is only able to detect an ON led and off it but when it is off, it is unable to ON the led..why is this so?I thought the btfss command wld be suffice enough to detect the state of the led.pls point out what has gone wrong for this simple code.Thanks

Code:
		org		0x0000			;org sets the origin, 0x0000 for the 16F628				
			goto	MAIN			;this is where the program starts running	
			

INIT		movlw	0x07
			movwf	CMCON				;turn comparators off (make it like a 16F84)

	   		BANK1						;select bank 1
			movlw 	b'00000000'			
   			movwf 	TRISB				;set PortB all outputs
			
			movlw	b'00111111'
			movwf	TRISA				;set PortA to be 6 inputs and 2 outputs

			BANK0						;select bank 0
			clrf	PORTB				;set all outputs low
			

CHKBUTTON	btfss	PORTA,sw0			;Note when swtiched is closed/push, it goes LOW
			call	MIN_button			;Switch 1 is pressed
			
			btfss	PORTA,sw1
			call 	SEC_button			;Switch 2 is pressed
			
			btfss	PORTA,sw2
			call	ST_ST_button		;Switch	3 is pressed
			
			;btfss	PORTA,sw3
			;call	RESET_button		;Switch	4 is pressed
			
			;btfss	PORTA,sw4
			;call	TRANSMIT_button		;Switch	5 is pressed
	
			goto	CHKBUTTON			;Go back and check the state of the switches	

;----------------------------------------------------------------------
MIN_button		call	Switchdelay			;call debounce delay
				btfsc	PORTA,sw0			;is switch still pressed?
				goto	CHKBUTTON			;switch has been released			
				call	UP_DOWN_MIN			;yes,switch still closed
				return

UP_DOWN_MIN		clrf	PORTB
				bsf		PORTB,0
				return

;------------------------------------------------------------------------
SEC_button		call	Switchdelay			;call debounce delay
				btfsc	PORTA,sw1			;is switch still pressed?
				goto	CHKBUTTON			;switch has been released
				call	UP_DOWN_SEC			;yes,switch still closed
				return

UP_DOWN_SEC		clrf	PORTB
				bcf		PORTB,0
				return

;-------------------------------------------------------------------------
ST_ST_button	call	Switchdelay			;call debounce delay
				btfsc	PORTA,sw2			;is switch still pressed?
				goto	CHKBUTTON			;switch has been released
				call	START_STOP			;yes,switch still closed
				return

START_STOP		clrf	PORTB				
				btfss	PORTB,0				;chk if led is ON
				bsf		PORTB,0				;its OFF, then switch LED ON
				bcf		PORTB,0				;its ON,then switch led OFF
				return

Also how come even using code tags, the code seems messy and out of place?
 
There are quite a few issues. First, your CHKBUTTON routine calls the MIN_Button function. Anytime you call a function, that function must -ALWAYS- return. But yours does not. Sometimes your MIN_Button may return. Other times, it will not, because you have a "goto CHKBUTTON" in there. So the stack is going to fill up really quick. That's bad. After that point, it's going to see a return, and wonder "return where?" because it's out of stack space, and won't go to the right place. I think what you are wanting to do is have it "goto MIN_Button", so that it continually loops itself, so it is continually testing the button.

The first instruction of START_STOP is clrf PORTB. That turns off every single bit of Port B. Port B = '00000000'. Then right after that, you test to see if Port B bit 0 is on. Guess what? I assure you it isn't. :twisted:

Now on the the actual testing of the bit 0. Assuming you take out the clrf PORTB command (why was that even in there?), your bit test can have two possible branches. If B0 is on, it skips the next command, and goes straight to the bit clear B0 and return. If B0 is off, it bit sets B0, then it clears B0, then returns. Notice that no matter what, it is always clearing B0. You need to have a goto command immediately after the bit test, so that it will do a bit set and return.

As for the messy code even with code tags, this message board software uses different tab settings than MPLab does, so each TAB spacing may be different.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top