PDA

View Full Version : output's on as power is supplied



tazntex
- 21st May 2007, 21:19
First I would like to thank those of you who helped me last week with the serial I/O expander that I was experimenting with last Friday. I now have everything working momentary now but at times when I apply power some if not all relays activate. Below is my code to activate the serial relays using a PIC16f84A:

INCLUDE "bs2defs.bas"
keydata VAR b3
serpin VAR porta.4
trisa = %00010000
trisb = %00000000

loop:
gosub loop1
goto keyed
keyed:
IF keydata = 1 THEN outr1
IF keydata = 2 THEN outr2
IF keydata = 3 THEN outr3
IF keydata = 4 THEN outr4
IF keydata = 5 THEN outr5
IF keydata = 6 THEN outr6
IF keydata = 7 THEN outr7
IF keydata = 8 THEN outr8
IF keydata = 9 THEN outr9
IF keydata = 10 THEN outr10
IF keydata = 11 THEN outr11
IF keydata = 12 THEN outr12
GOTO loop
outr1:
HIGH 0
gosub loop1
low 0
GOTO loop

outr2:
HIGH 1
gosub loop1
low 1
GOTO loop

outr3:
HIGH 2
gosub loop1
low 2
GOTO loop

outr4:
HIGH 3
gosub loop1
low 3
GOTO loop

outr5:
TOGGLE 4
PAUSE 250
GOTO loop

outr6:
TOGGLE 5
PAUSE 250
GOTO loop

outr7:
HIGH 6
gosub loop1
low 6
GOTO loop

outr8:
HIGH 7
gosub loop1
low 7
GOTO loop

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

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

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

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

loop1:
SERIN serpin,N2400,[254],keydata
IF keydata => 1 AND keydata <= 12 THEN GOTO keyed
return

Thanks again for your help

skimask
- 21st May 2007, 21:29
keydata var b3
What is 'b3' supposed to be?


Your logic is messed up...


INCLUDE "bs2defs.bas"
keydata VAR b3 <----- what is this again?
serpin VAR porta.4
trisa = %00010000 : trisb = %00000000

loop:
gosub loop1
goto keyed <------Why GOTO the next line below?
Let the program go to it, after all, it's right there
keyed:
IF keydata = 1 THEN outr1........IF keydata = 12 THEN outr12
and again, from the last thread you posted this in, it's fairly
bad form using all these IF/THEN statements,
look at using BRANCH in the manual instead
.....................
loop1:
SERIN serpin,N2400,[254],keydata
IF keydata => 1 AND keydata <= 12 THEN GOTO keyed
You GOSUB'd into this loop1: but you GOTO to get out of it if need be.
You can't do that, at least not for very long, depending on the program.
That's a stack overflow waiting to happen which leads to a PIC reset,
or at least a big mess.
return

tazntex
- 21st May 2007, 21:46
thanks Skimask for the reply here is my code again:

INCLUDE "bs2defs.bas"
keydata VAR b3 'relay number storage variable
serpin VAR porta.4 'serial input pin
trisa = %00010000
trisb = %00000000

loop:
gosub loop1 ' I am going to loop one to receive data on the serial pin then I am going to "keyed" to instruct the pic which relay to turn on.

goto keyed


loop1:
SERIN serpin,N2400,[254],keydata
IF keydata => 1 AND keydata <= 12 THEN GOTO keyed 'goto keyed if key is 'pressed
return

tazntex
- 21st May 2007, 22:15
I read in the PicBasicPro manual about using the branch as you suggested,
branch b3, [outr1,outr2,outr3,outr4,outr5,outr6,outr7,outr8,ou tr9,outr10,outr11,outr12]
I just compiled and tried the circuit seems to work fine. I am curious about why using many "If, Then" statements is not a good way of doing things.

Thanks again, as you may have guess I new into this and still learning .

skimask
- 21st May 2007, 22:47
keydata VAR b3 'relay number storage variable

IF keydata => 1 AND keydata <= 12 THEN GOTO keyed 'goto keyed if key is 'pressed


And again...b3 does not make any sense.
When you define a variable, you define it as a type; a bit, a byte, a word, a bucket, a scoop of ice cream, a car, whatever...

What exactly is a b3?

And again...in the If/Then, you GOSUB into the LOOP1, and then you GOTO out of LOOP1 back to the main loop. When you GOSUB, you push a return address onto the stack (i.e. a place to remember where I was before, look up GOSUB and RETURN in the manual), and from there, you must RETURN out of it. If you GOTO out of a GOSUB, you keep remembering where you were, but you'll never forget. A PIC only has so many empty spaces to remember, and by RETURNing, you free up one of those spaces. If you never RETURN, eventually, you won't remember either.

skimask
- 21st May 2007, 22:49
I just compiled and tried the circuit seems to work fine. I am curious about why using many "If, Then" statements is not a good way of doing things.

A lot of If/Then statements, all back-to-back, just kinda looks ugly, and makes it a bit harder to read. You have to scroll up, scroll down, move all around. With the BRANCH, it's all on one line. It doesn't neccessarily save memory, code, or whatever, it just looks better.

mister_e
- 21st May 2007, 22:56
Never trust a PIC at boot, it's output level is hard to predict. you should set it yourself at the top of your code and add a little delay


PORTA = 0
PORTB = 0
trisa = %00010000
trisb = %00000000
PAUSE 50 ' settle time

loop:

It's not a bad idea to enable the Power Up timer in your Configuration fuses as well.

@ __CONFIG _XT_OSC & _PWRTE_ON & _WDT_OFF & _CP_OFF
http://www.picbasic.co.uk/forum/showthread.php?t=543

Darrel Taylor
- 21st May 2007, 23:02
And again...b3 does not make any sense.
When you define a variable, you define it as a type; a bit, a byte, a word, a bucket, a scoop of ice cream, a car, whatever...

What exactly is a b3?

B3 is defined in the "bs2defs.bas" INCLUDE file.

It's an alias to W1.HIGHBYTE. W1 is also defined there as a WORD.
<br>

skimask
- 21st May 2007, 23:08
B3 is defined in the "bs2defs.bas" INCLUDE file.
It's an alias to W1.HIGHBYTE. W1 is also defined there as a WORD.
<br>

Ahhh...that covers why it compiles with errors...

mister_e
- 21st May 2007, 23:11
2nd round ;)
IT COMPILE WITHOUT ERROR, IT'S DEFINED IN THE BS2DEFS.BAS file. ;)

unless you removed the INLCUDE "BS2DEFS.BAS" linet, IT WILL COMPILE WITHOUT ERROR.

Once again ? ;)

Darrel Taylor
- 21st May 2007, 23:25
It's not a bad idea to enable the Power Up timer in your Configuration fuses as well.

@ __CONFIG _XT_OSC & _PWRTE_ON & _WDT_OFF & _CP_OFF

I've never really had problems with outputs coming on at power-up. They always seem to be in INPUT mode.

But assuming you did need to set them manually, wouldn't the Power-ON timer just make it that much longer till they actually got set to output.

Just a thought.
<br>

mister_e
- 21st May 2007, 23:41
If the hardware don't care about the 'Tri-State' or 'High impedance' level, yes...about 72mSec longer.

Unfortunately there's no Brown-Out detect on this one, we have no schematic and we can't measure the PSU behaviours and rise time from where we are.. hard to have the ultimate solution.

Maybe i misunderstood the original problem...

I now have everything working momentary now but at times when I apply power some if not all relays activate.

Darrel Taylor
- 22nd May 2007, 00:39
If the hardware don't care about the 'Tri-State' or 'High impedance' level ....

Right, and that could be a problem.

Here's an idea for Taz.
Try running this program ...
STOP
Yes, that's the whole program.

It'll leave the pins in Hi-Z mode.

Then see if any relays come on?
<br>