• 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.

Need help with this Code, Please

Status
Not open for further replies.

Kingdom Man

New Member
Hello, I am programming a 16f690 and I want to trigger one of my outputs to go high based on one of my inputs going high momentarily. I basically want to have a push button, when the button is pressed- it triggers one of my outputs to go high and stay high.
 
You didn't say what language.......so not being a mind reader, I get to choose?

Code:
#chip 16f690,8
#config INTRC_OSC_NOCLKOUT
dir PortA.0 in
Main:
If PortA.0 On Then
    wait 30 ms
    If PortA.0 Off then goto Main
    Set PortA.1 On
End if
Do
    nop
Loop
goto Main
 

be80be

Well-Known Member
You know I bet not one person on the forum will tell you how to do that.
and it work good in assembly

It's like this press switch led on press same switch led off

How do you do that

You test for button press and test for led to be off
If button pressed and led off turn on led
If button pressed and led on turn off led

This will do what you want maybe some one can post something better

Code:
#include <p16F690.inc>
     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
     	cblock	0x20
	d1
	d2
	endc
	org 0

init:
	banksel ANSEL		;switch banks 
	clrf	ANSEL 		;digital I/O
	banksel TRISA		;bank switch
	movlw	b'00001000' ;set ra3 as input
	movwf	TRISA
	banksel PORTA		;switch to banks
	clrf	PORTA		;set all low
	banksel TRISC		;swich banks
	movlw	b'00000000'
	movwf	TRISC		; set port to all outputs 
	banksel PORTC		;switch banks 
	clrf 	PORTC
	goto 	start    	;goto main program
start:
	bcf 	STATUS,RP0
	
	btfss	PORTA,3
	call	test
	call    Delay	
	goto 	start
test:
	call    Delay
	btfss	PORTC,0
	goto	on
	goto	off
	return
on:

	bsf		PORTC,0
	return
off:

	bcf		PORTC,0
	return
Delay
			;4993 cycles
	movlw	0xE6
	movwf	d1
	movlw	0x04
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return


 end
 
Last edited:

AtomSoft

Well-Known Member
I call that turning a analog button into a digital switch

EDIT:
Hey Burt u know im not picky at all but that one line it bothers me heh :)

Code:
	goto 	start    	;goto main program
also that code wont work right he wants it to stay on so he either needs to check a flag or just opposite the pin.
 
Last edited:

AtomSoft

Well-Known Member
Here ya go:
Code:
#include <p16F690.inc>
     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
     org 0
	cblock
	d1
	d2
	flag
	endc

init:
	banksel ANSEL		;switch banks 
	clrf	ANSEL 		;digital I/O
	banksel TRISA		;bank switch
	movlw	b'00001000' ;set ra3 as input
	movwf	TRISA
	banksel PORTA		;switch to banks
	clrf	PORTA		;set all low
	banksel TRISC		;swich banks
	movlw	b'00000000'
	movwf	TRISC		;set port to all outputs 
	movwf	flag		;init out flag with 0 aka off
	banksel PORTC		;switch banks 
	clrf 	PORTC
start:
	bcf 	STATUS,RP0
	
	btfsc	PORTA,3		;check if button is pressed (PULLED LOW - PRESS = HIGH)
	call	toggle		;if pressed (HIGH) call toggle
	call    Delay		;if not press or when returning from toggle call delay
	goto 	start		;repeat it all
toggle:
	btfsc   flag,0		;check if flag is cleared if so turn it on
	goto	off			;if not and flag is set turn it off
on:
	bsf		PORTC,0		;turn led on
	bsf		flag,0		;set our flag
	return				;go back
off:
	bcf		PORTC,0		;turn led off
	bcf		flag,0		;clear flag
	return				;go back

Delay
	movlw	0xE6
	movwf	d1
	movlw	0x04
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0
	goto	$+1
	nop
	return


 end
 

be80be

Well-Known Member
Atom the code works it's using the led as a flag lol. Only bad thing about that is you can see the Led flicker as the switch bounce.

Atom your code doesn't work at all I think its because your checking for the switch to go high That's not it . Don't take my word you got one of these try it for your self
 
Last edited:

AtomSoft

Well-Known Member
SW1 on that is for RA3 but the JP5 must be connected and JP1 for led

http://www.electro-tech-online.com/custompdfs/2009/08/Low20Pin20Count20User20Guide2051556a.pdf

Connect JP1 and JP5 and this should work:

Code:
#include <p16F690.inc>
     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
     org 0
	cblock
	d1
	d2
	flag
	endc

init:
	banksel ANSEL		;switch banks 
	clrf	ANSEL 		;digital I/O
	banksel TRISA		;bank switch
	movlw	b'00001000' ;set ra3 as input
	movwf	TRISA
	banksel PORTA		;switch to banks
	clrf	PORTA		;set all low
	banksel TRISC		;swich banks
	movlw	b'00000000'
	movwf	TRISC		;set port to all outputs 
	movwf	flag		;init out flag with 0 aka off
	banksel PORTC		;switch banks 
	clrf 	PORTC
start:
	bcf 	STATUS,RP0
	
	btfss	PORTA,3		;check if button is pressed
	call	toggle		;if pressed call toggle
	call    Delay		;if not press or when returning from toggle call delay
	goto 	start		;repeat it all
toggle:
	btfsc   flag,0		;check if flag is cleared if so turn it on
	goto	off			;if not and flag is set turn it off
on:
	bsf		PORTC,0		;turn led on
	bsf		flag,0		;set our flag
	return				;go back
off:
	bcf		PORTC,0		;turn led off
	bcf		flag,0		;clear flag
	return				;go back

Delay
	movlw	0xE6
	movwf	d1
	movlw	0x04
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0
	goto	$+1
	nop
	return
 end
 
Last edited:

be80be

Well-Known Member
SW1 on that is for RA3 but the JP5 must be connected and JP1 for led

http://www.electro-tech-online.com/custompdfs/2009/08/Low20Pin20Count20User20Guide2051556a-1.pdf

Connect JP1 and JP5 and this should work:

Code:
#include <p16F690.inc>
     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
     org 0
	cblock
	d1
	d2
	flag
	endc

init:
	banksel ANSEL		;switch banks 
	clrf	ANSEL 		;digital I/O
	banksel TRISA		;bank switch
	movlw	b'00001000' ;set ra3 as input
	movwf	TRISA
	banksel PORTA		;switch to banks
	clrf	PORTA		;set all low
	banksel TRISC		;swich banks
	movlw	b'00000000'
	movwf	TRISC		;set port to all outputs 
	movwf	flag		;init out flag with 0 aka off
	banksel PORTC		;switch banks 
	clrf 	PORTC
start:
	bcf 	STATUS,RP0
	
	btfss	PORTA,3		;check if button is pressed
	call	toggle		;if pressed call toggle
	call    Delay		;if not press or when returning from toggle call delay
	goto 	start		;repeat it all
toggle:
	btfsc   flag,0		;check if flag is cleared if so turn it on
	goto	off			;if not and flag is set turn it off
on:
	bsf		PORTC,0		;turn led on
	bsf		flag,0		;set our flag
	return				;go back
off:
	bcf		PORTC,0		;turn led off
	bcf		flag,0		;clear flag
	return				;go back

Delay
	movlw	0xE6
	movwf	d1
	movlw	0x04
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0
	goto	$+1
	nop
	return
 end
Thats only if you cut them I haven't cut my jumpers
 

be80be

Well-Known Member
Atom the board is made with the jumpers set to use leds and switch you cut the tracks and add two pin headers for jumpers only if you cut the tracks
 

be80be

Well-Known Member
Atom the switch is high going low on press there are four leds on portC 0-3
that's how the board works OK

Your code turns on all the leds that's funny because I can't see that happening
 

be80be

Well-Known Member
Atom whats 'the part in red for your not calling it
Code:
#include <p16F690.inc>
     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
     org 0
	cblock
	d1
	d2
	flag
	endc

init:
	banksel ANSEL		;switch banks 
	clrf	ANSEL 		;digital I/O
	banksel TRISA		;bank switch
	movlw	b'00001000' ;set ra3 as input
	movwf	TRISA
	banksel PORTA		;switch to banks
	clrf	PORTA		;set all low
	banksel TRISC		;swich banks
	movlw	b'00000000'
	movwf	TRISC		;set port to all outputs 
	movwf	flag		;init out flag with 0 aka off
	banksel PORTC		;switch banks 
	clrf 	PORTC
start:
	bcf 	STATUS,RP0
	
	btfsc	PORTA,3		;check if button is pressed (PULLED LOW - PRESS = HIGH)
	call	toggle		;if pressed (HIGH) call toggle
	call    Delay		;if not press or when returning from toggle call delay
	goto 	start		;repeat it all
toggle:
	btfsc   flag,0		;check if flag is cleared if so turn it on
	goto	off			;if not and flag is set turn it off
[COLOR="Red"]on:
	bsf		PORTC,0		;turn led on
	bsf		flag,0		;set our flag
	return				;go back[/COLOR]
off:
	bcf		PORTC,0		;turn led off
	bcf		flag,0		;clear flag
	return				;go back

Delay
	movlw	0xE6
	movwf	d1
	movlw	0x04
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0
	goto	$+1
	nop
	return


 end
 

AtomSoft

Well-Known Member
heh yes i am...

Code:
	btfsc   flag,0		;check if flag is cleared if so turn it on
	goto	off			;if not and flag is set turn it off
If the flag is cleared it goes to the red you highlighted if its set then it goes to off
 

AtomSoft

Well-Known Member
Atom the switch is high going low on press there are four leds on portC 0-3
that's how the board works OK

Your code turns on all the leds that's funny because I can't see that happening
hence why this should work fine:
Code:
#include <p16F690.inc>
     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
	cblock 0x20
	d1
	d2
	flag
	endc

     org 0
init:
	banksel ANSEL		;switch banks 
	clrf	ANSEL 		;digital I/O
	banksel TRISA		;bank switch
	movlw	b'00001000' ;set ra3 as input
	movwf	TRISA
	banksel PORTA		;switch to banks
	clrf	PORTA		;set all low
	banksel TRISC		;swich banks
	movlw	b'00000000'
	movwf	TRISC		;set port to all outputs 
	movwf	flag		;init out flag with 0 aka off
	banksel PORTC		;switch banks 
	clrf 	PORTC
start:
	bcf 	STATUS,RP0
	
	btfss	PORTA,3		;check if button is pressed
	call	toggle		;if pressed call toggle
	call    Delay		;if not press or when returning from toggle call delay
	goto 	start		;repeat it all
toggle:
	btfsc   flag,0		;check if flag is cleared if so turn it on
	goto	off			;if not and flag is set turn it off
on:
	bsf		PORTC,0		;turn led on
	bsf		flag,0		;set our flag
	return				;go back
off:
	bcf		PORTC,0		;turn led off
	bcf		flag,0		;clear flag
	return				;go back

Delay
	movlw	0xE6
	movwf	d1
	movlw	0x04
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0
	goto	$+1
	nop
	return
 end
 
Last edited:

be80be

Well-Known Member
Atom the Op has the same board let him see how it works

your code just turns the leds on on PORTC

My code turned RC0 on and it stayed on till you press the switch but you can see the bounce of the switch because I was using RC0 as the flag

Here why all the leds where coming on
Code:
	cblock	0x20
	d1
	d2
	flag
	endc
	org 0
Look at the code you posted Atom
 
Last edited:

AtomSoft

Well-Known Member
Atom the Op has the same board let him see how it works

your code just turns the leds on on PORTC

My code turned RC0 on and it stayed on till you press the switch but you can see the bounce of the switch because I was using RC0 as the flag

Here why all the leds where coming on
Code:
	cblock	0x20
	d1
	d2
	flag
	endc
	org 0
Look at the code you posted Atom
heh i copied the main code from you since i never used that PIC before i assumed you had it correct :D
 

be80be

Well-Known Member
Atom I didn't put a debounce delay in at first then I cut and pasted I messed the 0x20 part
and put it after the org 0 LOl

Your code works now mine worked like it was and and now the debounce delay is working
they both work the same
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top