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.

how to set the relay dont turn on all the time?

Status
Not open for further replies.

babboy12345

New Member
now i have a problem for my music input keep turn on my relay.what can i slove this??? i try to edit my program to let my relay dont turn on too oftenly,but fail,any 1 pls help???

Code:
		LIST P=16F877A
		INCLUDE "P16F877a.inc"
	;	__CONFIG _CP_OFF & _WDT_OFF & _HS_OSC & _PWRTE_ON



status	EQU	0x03
TRISA	EQU	0X85
PORTA	EQU	0X05
TRISB	EQU	0X86
PORTB	EQU	0X06
CHK1 	EQU	0X21
CNT1	EQU	0CH
CNT2	EQU	0DH
count	EQU	0x20
NumH	equ	0x22
CounterA	equ	0x23
CounterB	equ	0x24
CounterC	equ	0x25

		org		0x000
		nop		
		
		BSF		STATUS,RP0
		
		MOVLW	H'FF'
		MOVWF	TRISA
		movlw	H'00';
		movwf	TRISB;

		banksel	PORTA	
		BCF		STATUS,RP0
		clrf 	PORTB

		banksel	ADCON1
		movlw	b'00000000'
		movwf	ADCON1

		banksel	ADCON0
		movlw	b'10000001'
		movwf	ADCON0
		goto	start

getAD	movlw	007
		movwf	count
down	decfsz	count
		goto	down

		bsf		ADCON0,GO
		btfsc	ADCON0,GO
		goto	$-1
		return		


delay10s	movlw 	d'10'
			movwf	count

delay10sloop	call	delay1s
				decfsz	count,f
				goto	delay10sloop
				return

delay1s			movlw	d'4'
				movwf	CounterA

delay1sloop		call 	delay250ms
				decfsz	CounterA,f
				goto	delay1sloop
				return

delay250ms		movlw	d'250'
				movwf	CounterB

delay250msloop	call	delay1ms
				decfsz	CounterB,f
				goto	delay250msloop
				return

delay1ms		movlw	d'250'
				movwf	CounterC

delay1msloop	NOP
				decfsz	CounterC,f
				goto	delay1msloop
				return

Sub1	
		btfss	status,0
		goto	Sub2
		bsf		PORTB,1
		call	delay10s
		RETLW 	0x00
	


Sub2	btfsc	status,0
		goto	start
		bsf		PORTB,2
		call	delay10s
		RETLW	0x00
	


start	call 	getAD		; get adc result 
		movf	ADRESH,w
		movwf	NumH
		sublw	D'8'

		btfss	status,2
		goto	Sub1
		bsf		PORTB,0
		call 	delay10s
		goto	start
		end
 
now i have a problem for my music input keep turn on my relay.what can i slove this??? i try to edit my program to let my relay dont turn on too oftenly,but fail,any 1 pls help???

hi,
Please tell us exactly what you are trying to do.?:)
 
Last edited:
i'm using music as my ADC ipput,and turn on my relay......my problems is.....my portb keep turn on my relay!

So when the music volume is over a certain level you want the relay to turn ON for a set period of time.

How have you connected the 'music' output to the input of the PIC's adc.?
 
i buy a jack and cut off the cable,i will get the 2 input cable and 1 ground.i feed the music to the adc port,all work well. My problems is,when my music keep running,my led oso keep on n off,n this will make my relay keep on n off.i hope can use a delay to hold it more than 10sec,but i dono to write that program.any1 have any ideal???
 
i buy a jack and cut off the cable,i will get the 2 input cable and 1 ground.i feed the music to the adc port,all work well. My problems is,when my music keep running,my led oso keep on n off,n this will make my relay keep on n off.i hope can use a delay to hold it more than 10sec,but i dono to write that program.any1 have any ideal???

hi,
A quick look thru your program, it does not have a command to turn the PORTB off.??

Which pin is the relay connected to. 0,1,or 2.?
 
i connect portb wiv a LED,then use a transistor to turn on my relay.Because the portb output just 1.smtgV,so i use transistor. Is that i need to clf portb after my call delay??

can u trach me how to write a 10sec delay, so i can have enough time to turn on my pump,thx
 
i connect portb wiv a LED,then use a transistor to turn on my relay.Because the portb output just 1.smtgV,so i use transistor. Is that i need to clf portb after my call delay??

can u trach me how to write a 10sec delay, so i can have enough time to turn on my pump,thx

hi,
You are using bsf PORTB,n to turn on the relay, you must use bcf PORTB.n after the delay to turn the relay off.

Look at this link for delays, let it calculate the delay.
 
DEAR SIR, i changing the delay time to 50sec,but the LED still blinking very fast,here my program.

Code:
		LIST P=16F877A
		INCLUDE "P16F877a.inc"
	;	__CONFIG _CP_OFF & _WDT_OFF & _HS_OSC & _PWRTE_ON



status	EQU	0x03
TRISA	EQU	0X85
PORTA	EQU	0X05
TRISB	EQU	0X86
PORTB	EQU	0X06
CHK1 	EQU	0X21
CNT1	EQU	0CH
CNT2	EQU	0DH
count	EQU	0x20
NumH	equ	0x22
CounterA	equ	0x23
CounterB	equ	0x24
CounterC	equ	0x25

	cblock
	d1
	d2
	d3
	d4
	
	endc
		
		org		0x000
		nop		
		
		BSF		STATUS,RP0
		
		MOVLW	H'FF'
		MOVWF	TRISA
		movlw	H'00';
		movwf	TRISB;

		banksel	PORTA	
		BCF		STATUS,RP0
		clrf 	PORTB

		banksel	ADCON1
		movlw	b'00000000'
		movwf	ADCON1

		banksel	ADCON0
		movlw	b'10000001'
		movwf	ADCON0
		goto	start

getAD	movlw	007
		movwf	count
down	decfsz	count
		goto	down

		bsf		ADCON0,GO
		btfsc	ADCON0,GO
		goto	$-1
		return		


delay10s	movlw	0xF0	;DELAY 50SEC
			movwf	d1
			movlw	0xDB
			movwf	d2
			movlw	0xA8
			movwf	d3
			movlw	0x02
			movwf	d4
Delay_0
			decfsz	d1, f
			goto	$+2
			decfsz	d2, f
			goto	$+2
			decfsz	d3, f
			goto	$+2
			decfsz	d4, f
			goto	Delay_0

			;5 cycles
			goto	$+1
			goto	$+1
			nop


			return

Sub1	
		btfss	status,0
		goto	Sub2
		bsf		PORTB,1
		call	delay10s
		BCF		PORTB,1
		NOP
		NOP
		NOP
		NOP
		NOP
		RETLW 	0x00
	


Sub2	btfsc	status,0
		goto	start
		bsf		PORTB,2
		call	delay10s
		BCF		PORTB,2
		NOP
		NOP
		NOP
		NOP
		NOP
		RETLW	0x00
	


start	call 	getAD		; get adc result 
		movf	ADRESH,w
		movwf	NumH
		sublw	D'8'

		btfss	status,2
		goto	Sub1
		bsf		PORTB,0
		call 	delay10s
		BCF		PORTB,0
		NOP
		NOP
		NOP
		NOP
		NOP
		goto	start
		end
 
DEAR SIR, i changing the delay time to 50sec,but the LED still blinking very fast,here my program.

I'll run it in my Simulator.

Which PORTB pin has the led/transistor driver connected.????
 
now i unplug all trnsistor...just left 3 led,which install at portb 0,1,2.So hope sir can tell me my error.Because i was try this program from morning till night,but still facing same problems.thx
 
now i unplug all trnsistor...just left 3 led,which install at portb 0,1,2.So hope sir can tell me my error.Because i was try this program from morning till night,but still facing same problems.thx

OK, I thought you had only 1 portb pin with a led, but you have 3 pins with leds.


EDIT:
You cannot use registers below 0x20 in a 16F877A as user regs.
This isnt the cause of the led flash, but it will cause other problems.
Code:
CHK1 	EQU	0X21;********

CNT1	EQU	0CH
CNT2	EQU	0DH


count	EQU	0x20;**** assembler error raised for cblock start
NumH	equ	0x22
CounterA	equ	0x23
CounterB	equ	0x24
CounterC	equ	0x25
 
Last edited:
now i unplug all trnsistor...just left 3 led,which install at portb 0,1,2.So hope sir can tell me my error.Because i was try this program from morning till night,but still facing same problems.thx
you could write lot of comments and define these Rb pins as led1, led2 and perhaps relay etc so that an observer can understand. you can't imagine the schematic and write software without comments, will only waste you time, as you are now suffering.
also if you have the schematic you may upload for faster appreciation.
 
Last edited:
i try to correct it le,but still fail,so wat can i do???

Code:
		LIST P=16F877A
		INCLUDE "P16F877a.inc"
	;	__CONFIG _CP_OFF & _WDT_OFF & _HS_OSC & _PWRTE_ON



status	EQU	0x03
TRISA	EQU	0X85
PORTA	EQU	0X05
TRISB	EQU	0X86
PORTB	EQU	0X06
CHK1 	EQU	0X21
CNT1	EQU	0CH
CNT2	EQU	0DH
count	EQU	0x26
NumH	equ	0x22
CounterA	equ	0x23
CounterB	equ	0x24
CounterC	equ	0x25

	cblock	
	d1		
	d2		
	d3		
	d4		
	endc
		
		org		0x0000
		nop		
		
		BSF		STATUS,RP0
		
		MOVLW	H'FF'
		MOVWF	TRISA
		movlw	H'00';
		movwf	TRISB;

		banksel	PORTA	
		BCF		STATUS,RP0
		clrf 	PORTB

		banksel	ADCON1
		movlw	b'00000000'	;set adc left justify
		movwf	ADCON1

		banksel	ADCON0
		movlw	b'10000001'	;set 20Mhz,ANo input
		movwf	ADCON0
		goto	start

getAD	movlw	007
		movwf	count
down	decfsz	count
		goto	down

		bsf		ADCON0,GO
		btfsc	ADCON0,GO
		goto	$-1
		return		


delay10s	movlw	0xF0	;DELAY 50SEC
			movwf	d1
			movlw	0xDB
			movwf	d2
			movlw	0xA8
			movwf	d3
			movlw	0x02
			movwf	d4
Delay_0
			decfsz	d1, f
			goto	$+2
			decfsz	d2, f
			goto	$+2
			decfsz	d3, f
			goto	$+2
			decfsz	d4, f
			goto	Delay_0

			;5 cycles
			goto	$+1
			goto	$+1
			nop


			return

Sub1	
		btfss	status,0	;bit set if carry=1
		goto	Sub2		; carry no =1
		bsf		PORTB,1		;turn on LED1
		call	delay10s
		BCF		PORTB,1
		NOP
		NOP
		NOP
		NOP
		NOP
		RETLW 	0x00
	


Sub2	btfsc	status,0	;bit set if carry =0
		goto	start		; carry no = 0
		bsf		PORTB,2		;LED 2 on
		call	delay10s
		BCF		PORTB,2		;clear all portb
		NOP
		NOP
		NOP
		NOP
		NOP
		RETLW	0x00
	


start	call 	getAD		; get adc result 
		movf	ADRESH,w
		movwf	NumH
		sublw	D'8'	;sub NumH with 8

		btfss	status,2	;bit set if w=0
		goto	Sub1		;if w np = 0
		bsf		PORTB,0		;on led 0
		call 	delay10s
		BCF		PORTB,0
		NOP
		NOP
		NOP
		NOP
		NOP
		goto	start
		end
 
i try to correct it le,but still fail,so wat can i do???

hi,
You are still using
Code:
CNT1	EQU	0CH
CNT2	EQU	0DH

I have made some small changes to your program, try this.

The ADC routine is not controlling PORTB.!!
 

Attachments

  • leds3a.asm
    1.6 KB · Views: 139
got improve,at least my circuit look more stable,but,for the LED problems still the same.Blink fast,haiz.......God bless me!!
 
got improve,at least my circuit look more stable,but,for the LED problems still the same.Blink fast,haiz.......God bless me!!

I would suggest you write a new simple program that just flashes one led.
Say flash ON for 5 secs and then OFF for 5 secs.... repeating

Dont include the adc or any other leds.:)

Dont forget, you must not use registers less than 0x20 for your equates, when using the 16F877A.
 
this program also cant work??

Code:
	list	P=16F877A
	INCLUDE	"P16F877a.inc"
	
	__CONFIG  0x3F32


;;	__CONFIG _CP_OFF & _WDT_OFF & _HS_OSC & _PWRTE_ON 

	errorlevel -302, -207, -313

status	equ	0x03
TRISA	equ	0X85
PORTA	equ	0X05
TRISB	equ	0X86
PORTB	equ	0X06


count	equ	0x20
NumH	equ	0x22


CounterA equ	0x23
CounterB equ	0x24
CounterC equ	0x25

CHK1	equ	0X27

CNT1	equ	0x29
CNT2	equ	0x2B


d1 equ 0x30
d2 equ 0x31
d3 equ 0x32
d4 equ 0x33

	org	0x000
	nop

	bsf	STATUS,RP0

	movlw	H'FF'
	movwf	TRISA
	movlw	H'00'		;
	movwf	TRISB		
	clrf	PORTB;
	GOTO 	start
	

delay10s
	;;;return;;;; 
	movlw	0xF0		;DELAY 50SEC
	movwf	d1
	movlw	0xDB
	movwf	d2
	movlw	0xA8
	movwf	d3
	movlw	0x02
	movwf	d4
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	$+2
	decfsz	d4, f
	goto	Delay_0

	;5 cycles 
	goto	$+1
	goto	$+1
	nop


	return


start	bsf		PORTB,0
		call	delay10s
		bcf		PORTB,0
		end
 
Status
Not open for further replies.

Latest threads

Back
Top