PDA

View Full Version : Serial Relays



tazntex
- 17th May 2007, 16:55
After looking at http://www.rentron.com/PIC16F84.htm I thought I would try some experimenting with the serial I/O expander. Using a keypad to send the data with the [254] qualifier I am able to turn each relay on, however, I would like to be able to turn a relay on and hold it on while I am pressing the button on the keypad but as soon as I let go of the button I want the relay to turn off.
Also I am using PicBasic Pro compiler and have read through the manual but a lot of things remain unclear. Is there any supplement to this manual or other clear examples to shed some light on things like i=i+1? Here is the code I've been trying to make work the original version is www.rentron.com/PIC16F84.htm Any suggestion?

INCLUDE "bs2defs.bas"
relay VAR b3 'relay number storage variable
stat VAR b4 'relay status ON/OFF variable
serpin VAR porta.4 'serial input pin
trisa = %00010000
trisb = %00000000

loop:
SERIN serpin,N2400,[254],relay,stat
IF relay = 1 THEN outr1
IF relay = 2 THEN outr2
IF relay = 3 THEN outr3
IF relay = 4 THEN outr4
IF relay = 5 THEN outr5
IF relay = 6 THEN outr6
IF relay = 7 THEN outr7
IF relay = 8 THEN outr8
IF relay = 9 THEN outr9
IF relay = 10 THEN outr10
IF relay = 11 THEN outr11
IF relay = 12 THEN outr12
GOTO loop

outr1:
IF stat = 1 THEN high1
LOW 0: GOTO loop
high1:
HIGH 0:
gosub loop1
low 0: GOTO loop

outr2:
IF stat = 1 THEN high2
LOW 1: GOTO loop

high2:
HIGH 1:
gosub loop1
low 1: GOTO loop

outr3:
IF stat = 1 THEN high3
LOW 2: GOTO loop

high3:
HIGH 2:
gosub loop1
low 2: GOTO loop

outr4:
IF stat = 1 THEN high4
LOW 3: GOTO loop

high4:
HIGH 3:
gosub loop1
low 3: GOTO loop

outr5:
IF stat = 1 THEN high5
LOW 4: GOTO loop

high5:
HIGH 4:
gosub loop1
low 4: GOTO loop

outr6:
IF stat = 1 THEN high6
LOW 5: GOTO loop

high6:
HIGH 5:
gosub loop1
low 5: GOTO loop

outr7:
IF stat = 1 THEN high7
LOW 6: GOTO loop

high7:
HIGH 6:
gosub loop1
low 6: GOTO loop

outr8:
IF stat = 1 THEN high8
LOW 7: GOTO loop

high8:
HIGH 7:
gosub loop1
low 7: GOTO loop

outr9:
IF stat = 1 THEN high9
porta.0 = 0: GOTO loop

high9:
porta.0 = 1:
gosub loop1
porta.0 = 0: GOTO loop

outr10:
IF stat = 1 THEN high10
porta.1 = 0: GOTO loop

high10:
porta.1 = 1:
gosub loop1
porta.1 = 0: GOTO loop

outr11:
IF stat = 1 THEN high11
porta.2 = 0: GOTO loop

high11:
porta.2 = 1:
gosub loop1
porta.2 = 0: GOTO loop

outr12:
IF stat = 1 THEN high12
porta.3 = 0: GOTO loop

high12:
porta.3 = 1:
gosub loop1
porta.3 = 0: GOTO loop

loop1:
SERIN serpin,N2400,[254],relay,stat
IF relay = 1 THEN loop
IF relay = 2 THEN loop
IF relay = 3 THEN loop
IF relay = 4 THEN loop
IF relay = 5 THEN loop
IF relay = 6 THEN loop
IF relay = 7 THEN loop
IF relay = 8 THEN loop
IF relay = 9 THEN loop
IF relay = 10 THEN loop
IF relay = 11 THEN loop
IF relay = 12 THEN loop
return

I am using a 4Mhz resonator, and have set my config at XT, is this correct?
Thanks to all of you for any help.

T.Jackson
- 17th May 2007, 17:12
After looking at http://www.rentron.com/PIC16F84.htm I thought I would try some experimenting with the serial I/O expander. Using a keypad to send the data with the [254] qualifier I am able to turn each relay on, however, I would like to be able to turn a relay on and hold it on while I am pressing the button on the keypad but as soon as I let go of the button I want the relay to turn off.


Hmm - Perhaps, if the keypad issues another serial command to tell the controller when a key has been released.

skimask
- 17th May 2007, 17:12
Is there any supplement to this manual or other clear examples to shed some light on things like i=i+1?
PBP is PicBasicPro...emphasis on BASIC. There really isn't a supplement to the manual for BASIC, because the whole manual is about BASIC. I can't cite any specific BASIC tutorial other than to Google it and see what you find. Your question on the 'i = i + 1' is reference, albiet indirectly in the manual. i is a variable stored in local memory. = and + are the operators. Whatever is on the right side of the = goes to the left side of the =. Therefore, whatever was in 'i', gets 1 added to it and put back in 'i'.

And your program needs fixing..........



IF relay = 1 THEN outr1
...............................
IF relay = 12 THEN outr12

Check the PBP manual under branch, save some space and headache



outr1:
IF stat = 1 THEN high1
LOW 0: GOTO loop
high1:
HIGH 0:
gosub loop1
low 0: GOTO loop

HIGH 0: will be recognized as a label and not a command, same thing with the rest of the chunks in the rest of the program. You can't have a colon at the end of a line unless it's a label.



IF relay = 1 THEN loop
.............................
IF relay = 12 THEN loop


How about this instead:

IF relay => 0 AND relay <= 12 THEN GOTO LOOP

T.Jackson
- 17th May 2007, 17:42
As Ski pointed out, i is a variable. It's called a variable because, unlike fixed constants it changes. If i is a byte sized variable it can take on any form between (0-255) Whilst if it's a word sized it can be assigned with anything up to 65,536.

When we see something like this:


i = i + 1

It's basically just adding 1 to the existing contents of i. This is referred to as incrementing. So, if i = 100 and we encounter i = i + 1, the new value of i will be 101.

For Next Loops:


FOR i = 1 TO 10
j = j + 20
NEXT


Strangely enough, more than often we encounter i as a variable within these loops. It could be anything actually. J, K - doesn't matter. FOR NEXT loops will execute the contents between FOR and NEXT a predefined number of times which is set by the numbers after TO minus the numbers after FOR. In the example above, j will have 20 added to it 10 times. The final result of j will be 200 when the loop has finished. However, this assumes that the initial starting value of j was 0.
<br/>