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.

relay driver

Status
Not open for further replies.
Im currently working in this project and searched and modified the circuit,

by using a spdt switch i can control a specific relay and LED
where

RB0 controls RB7 and RA0 ===>if RB0=0 then RB7 and RA0=0, else it will be 1 when RB0=1
RB1 controls RB6 and RA1
RB2 controls RB5 and RA2
RB3 controls RB4 and RA3





i want to know the codes in Assembly language

i really want to know how to control each pins, i only know the basics like
movlw b'00000000'
movwf portb
 
hi,
Which PIC type and are you using MPLAB for assembly.??

Look up the test PORTB.n for the switch inputs and the bsf and bcf operators they would be simple to set/reset the PORTB and PORTA output pins
 
Last edited:
#include <P16f84a.inc>
__config _XT_OSC & _PWRTE_ON & _WDT_OFF

e equ h'21'
c equ h'22'



Int:
bsf STATUS,RP0
movlw b'00000'
movwf TRISA
movlw b'00001111'
movwf TRISB
bcf STATUS,RP0



run:

btfss portb,0
bsf portb,7
bsf porta,0

btfss portb,1
bsf portb,6
bsf porta,1

btfss portb,2
bsf portb,5
bsf porta,2

btfss portb,3
bsf portb,4
bsf porta,3
goto run

slow:
movlw 0XFE
movwf e
movlw 0XFE
movwf c

x:
movlw 0XFE
movwf c
y:
DECFSZ c
goto y
DECFSZ e
goto x
RETURN

end
 
im quite not sure about this

hi,
Dont worry, at least you have given it a try, many don't.:rolleyes:

Is this a homework or college assignment.?

You could easily control the relays from the 4 switches.

Will check your program.
 
Code:
	list      p=16F84A            ; list directive to define processor
	#include <p16F84A.inc>        ; processor specific variable definitions
	errorlevel -302, -207	
	
	__config _CP_OFF &_XT_OSC & _PWRTE_ON & _WDT_OFF
	
	

cntr1	equ	h'21'
cntr2	equ	h'22'


	ORG     0x000             ; processor reset vector
  	goto    main              ; go to beginning of program

	ORG     0x004             ; interrupt vector location
	nop
	retfie                    ; return from interrupt

main:
	bsf	STATUS,RP0
	movlw	b'00000'
	movwf	TRISA
	movlw	b'00001111'
	movwf	TRISB
	bcf	STATUS,RP0

loop:
	btfss	PORTB,0
	bsf	PORTB,7
	bsf	PORTA,0
	
	btfss	PORTB,1
	bsf	PORTB,6
	bsf	PORTA,1

	btfss	PORTB,2
	bsf	PORTB,5
	bsf	PORTA,2

	btfss	PORTB,3
	bsf	PORTB,4
	bsf	PORTA,3
	goto	loop

slow:
	movlw	0XFE
	movwf	cntr1
	movlw	0XFE
	movwf	cntr2
del1:
	movlw	0XFE
	movwf	cntr2
del2:
	decfsz	cntr2,F
	goto	del2
	decfsz	cntr1,F
	goto	del1
	return

	end

hi,
Tidied up your listing, have left the 'slow' routine in even though you are not using it.

This piece of code, check it again.? what do you think it will do when PORTB.0 is low and High.:rolleyes:

btfss PORTB,0
bsf PORTB,7
bsf PORTA,0


BTW: I would advise you dont use a single character for your code... example you had e,c,x and y , use a short name that means something so when you read the code in the future it makes sense.
 
Last edited:
it is not a home work but i really want to understand furthermore the discussion we had last meeting
i want to control each bidirectional port in bot porta and b
 
it is not a home work but i really want to understand furthermore the discussion we had last meeting
i want to control each bidirectional port in bot porta and b

OK, thats sounds a good idea to me.
Look at the listing I have posted, also check the code fragment I have queried.
 
this means if Ra0 of porta =1 then make Rb7=1an make RA0=1 ?
im not quite used to it

Hi,
Look at this variation of your bit testing, it works OK.

There is a more efficient way of writing the code for this application, can you figure out what it is.?

Code:
;RB0 controls RB7 and RA0 
;===>if RB0=0 then RB7 and RA0=0, else it will be 1 when RB0=1
;RB1 controls RB6 and RA1
;RB2 controls RB5 and RA2
;RB3 controls RB4 and RA3
	
	list      p=16F84A            ; list directive to define processor
	#include <p16F84A.inc>        ; processor specific variable definitions
	errorlevel -302, -207	
	
	__config _CP_OFF &_XT_OSC & _PWRTE_ON & _WDT_OFF
	
	

cntr1	equ	h'21'
cntr2	equ	h'22'


	ORG     0x000             ; processor reset vector
  	goto    main              ; go to beginning of program

	ORG     0x004             ; interrupt vector location
	nop
	retfie                    ; return from interrupt

main:
	bsf	STATUS,RP0
	movlw	b'00000'
	movwf	TRISA
	movlw	b'00001111'
	movwf	TRISB
	bcf	STATUS,RP0

loop:
	btfss	PORTB,0
	goto    sw1on
	bsf	PORTB,7
	bsf	PORTA,0
	goto 	sw2
sw1on:
	bcf	PORTB,7
	bcf	PORTA,0
	
sw2:	
	btfss	PORTB,1
	goto	sw2on
	bsf	PORTB,6
	bsf	PORTA,1
	goto	sw3
sw2on:
	bcf	PORTB,6
	bcf	PORTA,1	
	
sw3:
	btfss	PORTB,2
	goto	sw3on
	bsf	PORTB,5
	bsf	PORTA,2
	goto 	sw4
sw3on:
	bcf	PORTB,5
	bcf	PORTA,2
	
sw4:	
	btfss	PORTB,3
	goto	sw4on
	bsf	PORTB,4
	bsf	PORTA,3
	goto	loop
sw4on:
	bcf	PORTB,4
	bcf	PORTA,3
	goto 	loop


slow:
	movlw	0XFE
	movwf	cntr1
	movlw	0XFE
	movwf	cntr2
del1:
	movlw	0XFE
	movwf	cntr2
del2:
	decfsz	cntr2,F
	goto	del2
	decfsz	cntr1,F
	goto	del1
	return

	end
 
hi ernest,
Consider this option. Just change the PORTB7,6,5,4 to 4,5,6,7

Code:
;RB0 controls RB7 and RA0 
;RB0=0 then RB4 and RA0=0, else it will be 1 when RB0=1
;RB1 controls RB5 and RA1
;RB2 controls RB6 and RA2
;RB3 controls RB7 and RA3
	
	list      p=16F84A            ; list directive to define processor
	#include <p16F84A.inc>        ; processor specific variable definitions
	errorlevel -302, -207	
	
	__config _CP_OFF &_XT_OSC & _PWRTE_ON & _WDT_OFF
	
cntr1	equ	h'21'
cntr2	equ	h'22'
bfr1	equ 	23h

	ORG     0x000             ; processor reset vector
  	goto    main              ; go to beginning of program

	ORG     0x004             ; interrupt vector location
	nop
	retfie                    ; return from interrupt

main:
	bsf	STATUS,RP0
	movlw	b'00000000'
	movwf	TRISA
	movlw	b'00001111'
	movwf	TRISB
	bcf	STATUS,RP0

newloop:
	movf PORTB,W
	movwf bfr1
	movf bfr1,W
	movwf PORTA
	rlf bfr1,F
	rlf bfr1,F
	rlf bfr1,F
	rlf bfr1,F
	movf bfr1,W
	movwf PORTB
	goto newloop	

	end
 
Code:
loop:
andlw PORTB_buffer, b'00001111'    ;clear bits 4-7
andlw PORTA_buffer, b'11110000'    ;clear bits 0-3

    btfss    PORTB,0
    bsf    PORTB_buffer,7
    bsf    PORTA_buffer,0
 
    btfss    PORTB,1
    bsf    PORTB_buffer,6
    bsf    PORTA_buffer,1
 
    btfss    PORTB,2
    bsf    PORTB_buffer,5
    bsf    PORTA_buffer,2
 
    btfss    PORTB,3
    bsf    PORTB_buffer,4
    bsf    PORTA_buffer,3

   movf PORTB_buffer, W      ;copy to working register
   movwf PORTB                  ;copy working register to PORTB

   movf PORTA_buffer, W      ;copy to working register
   movwf PORTA                  ;copy working register to PORTA

    goto    loop

is this right?
 
hi,
Will not assemble.

andlw PORTB_buffer, b'00001111' ;clear bits 4-7
andlw PORTA_buffer, b'11110000' ;clear bits 0-3

look at this image.
 
im almost there,
program executed in simulator but PORTA is always on and i cant turn off the LEDs after i release the push buttons

Code:
#include <P16f84a.inc>
__config _XT_OSC & _PWRTE_ON & _WDT_OFF

PORTA_buffer equ h'21'
PORTB_buffer equ h'22'



Int:
	bsf STATUS,RP0
	movlw b'00000'
	movwf TRISA
	movlw b'00001111'
	movwf TRISB
	bcf STATUS,RP0



loop:

andlw PORTB_buffer   
andlw PORTA_buffer   

    btfss    PORTB,0
    bsf    PORTB_buffer,7
    bsf    PORTA_buffer,0
 
    btfss    PORTB,1
    bsf    PORTB_buffer,6
    bsf    PORTA_buffer,1
 
    btfss    PORTB,2
    bsf    PORTB_buffer,5
    bsf    PORTA_buffer,2
 
    btfss    PORTB,3
    bsf    PORTB_buffer,4
    bsf    PORTA_buffer,3

   movf PORTB_buffer, W      
   movwf PORTB                  
   movf PORTA_buffer, W      
   movwf PORTA                  

    goto    loop
	
end
 
hi,
You will never turn off the port pins with that program

What are you expecting this piece of code to do.?

andlw PORTB_buffer
andlw PORTA_buffer

When you post code, please add comments to the code lines so that I can see what your are expecting:rolleyes:
 
sorry i didnt fully understand how to use assembly language
heres what i want to do in the previous code
Code:
andlw PORTB_buffer, b'00001111'    ;clear bits 4-7
andlw PORTA_buffer, b'11110000'    ;clear bits 0-3
 
well i think the scenario of what am i doing is like this:
RA0 and RB7 can be actuated by RB0 and retract if no input from RB0
RA1 and RB6 can be actuated by RB1 and retract if no input from RB1
RA2 and RB5 can be actuated by RB2 and retract if no input from RB2
RA3 and RB4 can be actuated by RB3 and retract if no input from RB3
 
That will clear the '0' bits OK in the buffer, but that will not switch the port output pins low.

its this piece of code that wrong.
btfss PORTB,0
bsf PORTB_buffer,7
bsf PORTA_buffer,0

its saying whatever the state of PORTB.0 turn ON PORTA_buffer,0
and depending upon the state of PORTB.0 turn PORTB_buffer.0 On

Look at my code of the earlier post.
loop:
btfss PORTB,0
goto sw1on
bsf PORTB,7
bsf PORTA,0
goto sw2
sw1on:
bcf PORTB,7
bcf PORTA,0
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top