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.

on/off momentary switches.

Status
Not open for further replies.

sam_ecko

New Member
Hi i am trying to create code to turn 3 leds on and off using 3 switches. (1 switch per LED)

the theory is to have the code watch out for the inputs and act accordingly (duel functionality of switches). I have looked at "Nigel's tutorial 2.2" (). and have found code which gives an on/off function to one switch.

My problem is when i come to modify it to use as i intend it i can only use the first switch and not the others, almost as if the programme is stuck just looking for the first input and not the other 2.

The theory should be simple... but I am lost again, why doesn't the code exit the subroutines and test the other switches. :confused:
 
LIST P=16F628
#include "P16F628.INC"
__config _INTRC_OSC_NOCLKOUT & _LVP_OFF & _WDT_OFF & _PWRTE_ON & _BODEN_ON & _MCLRE_OFF

;----------------------------
CBLOCK 0X20
count1 ;used in delay routine
counta ;used in delay routine
countb ;used in delay routine
ENDC

;----------------------------

SWDel Set Del50 ;set the de-bounce delay (has to use 'Set' and not 'Equ')
;Initialise
;----------------------------
ORG 0X00
goto START
ORG 0x10
INIT
CLRF PORTA ; Initialize port A
CLRF PORTB ; Initialize port B

BSF STATUS,RP0 ; RAM bank 1

CLRF TRISA ; All pins port A output
CLRF TRISB ; All pins port B output
BCF STATUS,RP0 ; RAM bank 0
;-----------------------------
MOVLW 7
MOVWF CMCON ; Comparators off, all pins digital I/O
RETURN
;------------------------------
START
CALL INIT
Loop btfsc PORTB,0
call Switch1
loop2 btfsc PORTB,1
call Switch2
btfsc PORTB,2
call Switch3
goto Loop

Switch1 call SWDel ;give switch time to stop bouncing
btfsc PORTB,0 ;check it's still pressed
retlw 0x00 ;return is not
btfss PORTA,3 ;see if LED1 is already lit
GOTO LED1ON
GOTO LED1OFF
goto loop2

LED1ON bsf PORTA,3 ;turn LED1 on
call SWDel
btfsc PORTB,0 ;wait until button is released
retlw 0x00
GOTO LED1ON

LED1OFF bcf PORTA,3 ;turn LED1 on
call SWDel
btfsc PORTB,0 ;wait until button is released
retlw 0x00
GOTO LED1OFF

Switch2 call SWDel ;give switch time to stop bouncing
btfsc PORTB,1 ;check it's still pressed
retlw 0x00 ;return is not
btfss PORTA,6 ;see if LED2 is already lit
goto LED2ON
goto LED2OFF

LED2ON bsf PORTA,6 ;turn LED2 on
call SWDel
btfsc PORTB,1 ;wait until button is released
retlw 0x00
goto LED2ON

LED2OFF bcf PORTA,6 ;turn LED2 on
call SWDel
btfsc PORTB,1 ;wait until button is released
retlw 0x00
goto LED2OFF

Switch3 call SWDel ;give switch time to stop bouncing
btfsc PORTB,2 ;check it's still pressed
retlw 0x00 ;return is not
btfss PORTA,7 ;see if LED3 is already lit
goto LED3ON
goto LED3OFF

LED3ON bsf PORTA,7 ;turn LED3 on
call SWDel
btfsc PORTB,2 ;wait until button is released
retlw 0x00
goto LED3ON

LED3OFF bcf PORTA,7 ;turn LED3 on
call SWDel
btfsc PORTB,2 ;wait until button is released
retlw 0x00
goto LED3OFF

;modified Delay routine, direct calls for specified times
;or load W and call Delay for a custom time.

Del0 retlw 0x00 ;delay 0mS - return immediately
Del1 movlw d'1' ;delay 1mS
goto Delay
Del5 movlw d'5' ;delay 5mS
goto Delay
Del10 movlw d'10' ;delay 10mS
goto Delay
Del20 movlw d'20' ;delay 20mS
goto Delay
Del50 movlw d'50' ;delay 50mS
goto Delay
Del100 movlw d'100' ;delay 100mS
goto Delay
Del250 movlw d'250' ;delay 250 ms
Delay movwf count1
d1 movlw 0xC7 ;delay 1mS
movwf counta
movlw 0x01
movwf countb
Delay_0
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0

decfsz count1 ,f
goto d1
retlw 0x00
end
 
If I understand your description correctly, the tutorial you link to is exactly the code you want, but for 4 leds. just remove the lines / subs that refer to sw4 and led4.
 
hi baldor, it is what I want but it rotates (switch 2 turns on led1, 3 turns on 2 and so on). i changed one thing in it "btfss SWPORT, SW1" to btfsc because I didn't understand why it would be looping when the button was set instead of calling.
 
You have both ports set as outputs.;
Code:
BSF STATUS,RP0 ; RAM bank 1

CLRF TRISA ; All pins port A output
CLRF TRISB ; All pins port B output
BCF STATUS,RP0 ; RAM bank 0
 
surely that cant be the cause? i was running the initialise code with other code I had and they worked as intended
(error noted however and i will rectify it)
 
Try this, I have and it ignores switch 4.

Code:
;Tutorial 2.2 - Nigel Goodwin 2002
	LIST	p=16F628		;tell assembler what chip we are using
	include "P16F628.inc"		;include the defaults for the chip
	__config 0x3D18			;sets the configuration settings (oscillator type etc.)

	cblock 	0x20 			;start of general purpose registers
		count1 			;used in delay routine
		counta 			;used in delay routine 
		countb 			;used in delay routine
	endc

LEDPORT	Equ	PORTA			;set constant LEDPORT = 'PORTA'
SWPORT	Equ	PORTA			;set constant SWPORT = 'PORTA'
LEDTRIS	Equ	TRISA			;set constant for TRIS register
SW1	Equ	7			;set constants for the switches
SW2	Equ	6
SW3	Equ	5
SW4	Equ	4
LED1	Equ	3			;and for the LED's
LED2	Equ	2
LED3	Equ	1
LED4	Equ	0

SWDel	Set	Del50			;set the de-bounce delay (has to use 'Set' and not 'Equ')

;end of defines
	
	org	0x0000			;org sets the origin, 0x0000 for the 16F628,
					;this is where the program starts running	
	movlw	0x07
	movwf	CMCON			;turn comparators off (make it like a 16F84)

   	bsf 	STATUS,		RP0	;select bank 1
   	movlw 	b'11110000'		;set PortA 4 inputs, 4 outputs
   	movwf 	LEDTRIS
	bcf	STATUS,		RP0	;select bank 0
	clrf	LEDPORT			;set all outputs low


Loop	btfss	SWPORT,	SW1
	call	Switch1
	btfss	SWPORT,	SW2
	call	Switch2
	btfss	SWPORT,	SW3
	call	Switch3
	;btfss	SWPORT,	SW4	;<----------	Commented out
	;call	Switch4			;<----------	Commented out
	goto	Loop

Switch1	call	SWDel			;give switch time to stop bouncing
	btfsc	SWPORT,	SW1		;check it's still pressed
	retlw	0x00			;return is not
	btfss	SWPORT,	LED1		;see if LED1 is already lit
	goto	LED1ON
	goto	LED1OFF

LED1ON	bsf	LEDPORT,	LED1	;turn LED1 on
	call	SWDel
	btfsc	SWPORT,	SW1		;wait until button is released
	retlw	0x00
	goto	LED1ON	

LED1OFF	bcf	LEDPORT,	LED1	;turn LED1 on
	call	SWDel
	btfsc	SWPORT,	SW1		;wait until button is released
	retlw	0x00
	goto	LED1OFF		

Switch2	call	SWDel			;give switch time to stop bouncing
	btfsc	SWPORT,	SW2		;check it's still pressed
	retlw	0x00			;return is not
	btfss	SWPORT,	LED2		;see if LED2 is already lit
	goto	LED2ON
	goto	LED2OFF

LED2ON	bsf	LEDPORT,	LED2	;turn LED2 on
	call	SWDel
	btfsc	SWPORT,	SW2		;wait until button is released
	retlw	0x00
	goto	LED2ON	

LED2OFF	bcf	LEDPORT,	LED2	;turn LED2 on
	call	SWDel
	btfsc	SWPORT,	SW2		;wait until button is released
	retlw	0x00
	goto	LED2OFF

Switch3	call	SWDel			;give switch time to stop bouncing
	btfsc	SWPORT,	SW3		;check it's still pressed
	retlw	0x00			;return is not
	btfss	SWPORT,	LED3		;see if LED3 is already lit
	goto	LED3ON
	goto	LED3OFF

LED3ON	bsf	LEDPORT,	LED3	;turn LED3 on
	call	SWDel
	btfsc	SWPORT,	SW3		;wait until button is released
	retlw	0x00
	goto	LED3ON	

LED3OFF	bcf	LEDPORT,	LED3	;turn LED3 on
	call	SWDel
	btfsc	SWPORT,	SW3		;wait until button is released
	retlw	0x00
	goto	LED3OFF


;---------------------------------------- This bit never gets called ---------------------------------------- 
Switch4	call	SWDel			;give switch time to stop bouncing
	btfsc	SWPORT,	SW4		;check it's still pressed
	retlw	0x00			;return is not
	btfss	SWPORT,	LED4		;see if LED4 is already lit
	goto	LED4ON
	goto	LED4OFF

LED4ON	bsf	LEDPORT,	LED4	;turn LED4 on
	call	SWDel
	btfsc	SWPORT,	SW4		;wait until button is released
	retlw	0x00
	goto	LED4ON	

LED4OFF	bcf	LEDPORT,	LED4	;turn LED4 on
	call	SWDel
	btfsc	SWPORT,	SW4		;wait until button is released
	retlw	0x00
	goto	LED4OFF
;------------------------------------------------------------------------------------------------------------------------



;modified Delay routine, direct calls for specified times
;or load W and call Delay for a custom time.

Del0	retlw	0x00			;delay 0mS - return immediately
Del1	movlw	d'1'			;delay 1mS
	goto	Delay
Del5	movlw	d'5'			;delay 5mS
	goto	Delay
Del10	movlw	d'10'			;delay 10mS
	goto	Delay
Del20	movlw	d'20'			;delay 20mS
	goto	Delay
Del50	movlw	d'50'			;delay 50mS
	goto	Delay
Del100	movlw	d'100'			;delay 100mS
	goto	Delay
Del250	movlw	d'250'			;delay 250 ms
Delay	movwf	count1
d1	movlw	0xC7			;delay 1mS
	movwf	counta
	movlw	0x01
	movwf	countb
Delay_0
	decfsz	counta, f
	goto	$+2
	decfsz	countb, f
	goto	Delay_0

	decfsz	count1	,f
	goto	d1
	retlw	0x00
	end
 
thanks house of wax, does the code chase (the led you want to turn on is activated by the switch below). I commented out switch 4 originally but found that this was the function of the code, not just to watch and turn on/off simple (portb,0 switch1) =turns on and off= (porta, 3 switch2). If it doest cycle/chase then i am doing something terribly wrong.
 
No, there is no chasing. All the code does is toggle 4 (or 3 now) LEDs with their own switch.
They start out all off, and you can switch whichever one on with it's own button. Then off again.
Press button = LED on, press again = LED off.
 
I tried copying the code and burning it to a pic. I initially had an error with my programmer and its fuses (it does not recognise hex in the config). I then replaced the config (__config 0x3D18) with (__config _INTRC_OSC_ & _LVP_OFF & _WDT_OFF & _PWRTE_OFF & _BODEN_ON & _MCLRE_OFF). when i burnt the pic and powered it up, all that came up was a single red LED, no other functionality.
 
That's interesting. I changed my config to the one you tried, it wouldn't compile. I got the error "Symbol not previously defined (_INTRC_OSC_)".
So I changed that to '_INTRC_OSC_NOCLKOUT'. It compiles fine and the code does exactly the same as it did before.

What are you using to compile the code? I'm using Mpasm win v5.20. Also, are you sure of your hardware setup?
On my setup, all the LEDs are off when power is applied.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top