PDA

View Full Version : Controlling Relays



tazntex
- 28th January 2008, 16:03
Good Morning,
I have been playing around with MicroCode Studio this morning trying to learn something today. Anyhow I have built a serial keypad board and a serial relay board for experimenting with PicBasic Pro. In this way I can learn as I build. On the relay board I have some relays set to be momentary and the rest I am just toggling. When I send from the keypad to the relays that I want to be on only when the key is pressed they just stay on. When I press the one that toggle they work great. I thought that each time data was received and then branched the output would change as they do with the ones set to toggle. While holding down the switch for the relays that toggle they switch on, off, on, etc.... but the one for momentary they stay on constant. Any ideas what I am doing wrong? Thanks for all your help.
The Pic I am using is a 16f628a i was using a 16f84a. Here is the code.

define osc 4
INCLUDE "MODEDEFS.BAS"
@ DEVICE MCLR_ON, INTRC_OSC, WDT_Off, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_Off
CMCON=7
keydata VAR byte
serpin VAR porta.1
PORTA = 0
PORTB = 0
trisa = %00000010
trisb = %00000000

PAUSE 100 ' settle time

loop:

gosub loop1
branch keydata, [rly1,rly2,rly3,rly4,rly5,rly6,rly7,rly8]
GOTO loop

rly1:
HIGH PORTB.6
pause 50
GOTO loop

rly2:
HIGH PORTB.1
pause 50
GOTO loop

rly3:
HIGH PORTB.2
pause 50
GOTO loop

rly4:
HIGH PORTB.3
pause 50
GOTO loop

rly5:
HIGH PORTB.4
pause 50
GOTO loop

rly6:
toggle PORTB.5
pause 250
GOTO loop

rly7:
TOGGLE PORTB.6
PAUSE 250
GOTO loop

rly8:
toggle PORTB.0
pause 250
goto loop

skimask
- 28th January 2008, 16:33
gosub loop1
branch keydata, [rly1,rly2,rly3,rly4,rly5,rly6,rly7,rly8]
Looks like some of the program is missing...probably leaving out some important information.

Did you try it with LEDs first instead of relays?
Does it work the way you'd expect it to work?
Do you have flyback diodes across the coils of the relays?

sayzer
- 28th January 2008, 16:33
1. Where is Loop1 ?
2. When do you turn off the leds that you turn on initially?

==============

tazntex
- 28th January 2008, 16:40
Thanks for replying,
The relays that toggle work wonderful, and the ones that I want on only when I am pressing the switch, they just stay on. the relay circuits from their corresponding Pic output are Identical with a diode flyback protection.
Thanks for helping
Here is the code again with Loop1,
define osc 4
INCLUDE "MODEDEFS.BAS" 'Serial communication mode definition file
@ DEVICE MCLR_ON, INTRC_OSC, WDT_Off, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_Off
CMCON=7
keydata VAR byte
serpin VAR porta.1 'serial input pin
PORTA = 0
PORTB = 0
trisa = %00000010
trisb = %00000000

PAUSE 100 ' settle time

loop:

gosub loop1
branch keydata, [rly1,rly2,rly3,rly4,rly5,rly6,rly7,rly8]
GOTO loop

rly1:
HIGH PORTB.6
pause 50
GOTO loop

rly2:
HIGH PORTB.1
pause 50
GOTO loop

rly3:
HIGH PORTB.2
pause 50
GOTO loop

rly4:
HIGH PORTB.3
pause 50
GOTO loop

rly5:
HIGH PORTB.4
pause 50
GOTO loop

rly6:
toggle PORTB.5
pause 250
GOTO loop

rly7:
TOGGLE PORTB.6
PAUSE 250
GOTO loop

rly8:
toggle PORTB.0
pause 250
goto loop




loop1:
SERIN serpin,N2400,[254],keydata
return

Acetronics2
- 28th January 2008, 16:44
rly1:
HIGH PORTB.6
pause 50
GOTO loop



Hi,

You have to de-energize your relays ... that's all

for relay one ... what value is Keydata if no key pushed ??? good question !



Alain

tazntex
- 28th January 2008, 16:45
My thinking is that while keeping the switch on for the momentary relay to stay on the data being branch would be the same so the relay would stay on. When I release the switch then the data being branched changes so the relay would turn off. Is this correct?

Acetronics2
- 28th January 2008, 16:54
Yes,


on each loop, you must check which keys are released and de-energize the corresponding 1-5 relays. ( the last ones do not need it ...)

the PIC outputs must be considered as Bistable ones ... and you must write every change you want !!!

Alain

tazntex
- 28th January 2008, 18:15
The value of Keydata is 0 if no key is pressed: here is how I've set that,
if keyin = nokey then keydata = %00000000

Thank you for your help.

tazntex
- 28th January 2008, 18:22
define osc 4
INCLUDE "MODEDEFS.BAS"
CMCON = 7
serpin var porta.3
nokey con %11111111
preamble con %01010101
synch con 254
keyin var portb
keydata var byte
trisb = %11111111
trisa = %00010111
key1 VAR PORTB.0
key2 VAR PORTB.1
key3 VAR PORTB.2
key4 VAR PORTB.3
key5 VAR PORTB.4
key6 VAR PORTB.5
key7 VAR PORTB.6
na VAR PORTB.7




findkey:
clear
pause 50
if key1 = 0 then sw1
if key2 = 0 then sw2
if key3 = 0 then sw3
if key4 = 0 then sw4
if key5 = 0 then sw5
if key6 = 0 then sw6
if key7 = 0 then sw7
if key8 = 0 then sw8

goto findkey

sw1:
keydata = 1
goto transmit
sw2:
keydata = 2
goto transmit
sw3:
keydata = 3
goto transmit
sw4:
keydata = 4
goto transmit
sw5:
keydata = 5
goto dout
sw6:
keydata = 6
goto transmit
sw7:
keydata = 7
goto transmit
sw8:
keydata = 8
goto transmit




transmit:

serout serpin,N2400,[preamble,synch,keydata]
pause 50
if keyin = nokey then keydata = %00000000
goto findkey


end

skimask
- 28th January 2008, 18:39
All define's must be capitalized (not that it makes any difference in this case)

sayzer
- 29th January 2008, 11:55
1. There is no Key8.
2. You can send the content of the whole PORTB when a change occurs; may be it would be more convenient but it is up to your choice.

For example:




FindKey:
CLEAR
KeyData = PORTB
PAUSE 50
IF PORTB = KeyData THEN FindKey 'Transmit only if there is at least a (one) change on PORTB.

SEROUT SerPin,N2400,[PreAmble,Synch,PORTB]
PAUSE 50

GOTO FindKey



This code will transmit the content of the PORTB every time there is a change on PORTB.
Thus, for instance, if PORTB.0 is 0, it will transmit, and then if it is still 0, it will NOT transmit.
If PORTB.0 is still 0 and then another change occurs at say PORTB.1 then it will transmit.

This way, you can handle the process at receiver side.