Programming pointers

todster

Active Member
I'm going to use a board to control my wood/oil furnace. It won't replace my $$$$ AB plc until I know it will work safely and I have learned as many quirks and techniques as possible about this board first.
The program will work like such:
Check duct temp (T5) for hot and overheat (IP8 N/C). If either is on then force blower to high. If neither is on then proceed. If room temp (T1) is below setpoint then on until above setpoint. Do this every 500 loops or on 1st scan. Check to see if oil is selected or wood is selected. If heat is enabled and oil on then turn oil on else goto wood. Check duct temp and determine blower speed. I forgot to add in a limit switch function to turn oil or wood off well before the overheat is reached. I use excel because it makes it easier for me to write the code in blocks then paste together when I'm ready.
I saw someone had a program to compile code with comments but can't remember where I saw it. If you remember it, I would appreciate a link to it. Here's the code I have so far. Any and all thoughts are appreciated. If I can condense it or sweeten it or add additional safety features not considered, that would be great. Thanks.


START
GOTO INIT
INIT:
TSTEQ IP8 0
GOTO SAFETY
INC RAM8
TSTEQ RAM8 500
GOTO TSTAT
TSTEQ RAM8 0
GOTO TSTAT
NOP
GOTO OVERHEAT
NOP

TSTAT:
TSTLE T1 UROM1
ANDB RAM1 32766 RAM1
ORB RAM1 1 RAM1
TSTGE T1 UROM2
ANDB RAM1 32766 RAM1
SET RAM8 1
GOTO INIT
NOP

OVERHEAT:
TSTLE T5 1650
ORB RAM1 1024 RAM1
ANDB RAM1 31743 RAM1
ANDB RAM1 1024
BZ SAFETY
TSTEQ IP8 1
ORB RAM1 2048 RAM1
ANDB RAM1 30719 RAM1
ANDB RAM1 3072
BZ SAFETY
ANDB RAM1 1
BNZ HEAT
GOTO INIT
NOP

Fan_spd:
TSTLE T5 1200
ANDB RAM1 32511 RAM1
ORB RAM1 256 RAM1
TSTLE T5 1400
ANDB RAM1 32255 RAM1
ORB RAM1 512 RAM1
ANDB RAM1 768
BNZ HI_SPD
ANDB RAM1 256
BNZ LO_SPD
ANDB RAM1 256
BZ FAN_OFF
GOTO INIT
NOP

FAN_OFF:
SET OP3 0
SET OP4 0
GOTO INIT
SET VAR1 RAM1
NOP

SAFETY:
SET OP1 0
SET OP2 0
SET OP3 0
SET OP4 1
GOTO INIT
NOP

LO_SPD:
SET OP3 1
SET OP4 0
SET VAR1 RAM1
GOTO INIT
NOP

HI_SPD:
SET OP3 0
SET OP4 1
SET VAR1 RAM1
GOTO INIT
XNOP

HEAT:
ANDB RAM1 3
BNZ OIL
SET OP1 0
ANDB RAM1 5
BNZ WOOD
SET OP2 0
GOTO FAN_SPD
NOP

OIL:
ANDB RAM1 3
SET OP1 1
SET OP2 0
GOTO FAN_SPD
NOP

WOOD:
ANDB RAM1 5
SET OP2 1
SET OP1 0
GOTO FAN_SPD
END
 
What is the purpose of these two lines:
GOTO INIT
INIT:

If you plan to initialize a set of variables, you can do it right after START,
after initialization of variables, you will have a label BEGIN: or MYBEGIN:,
then in your logic, everywhere needd to go to very beginning, just goto BEGIN.
This will have an advantage that before you start any logic, you check what is the state of your setup, setup the condition for all logic to run.
During running, you don't do any initializaiton any more. When power lost and recover from where it was, to initialize the conditional variable is important.

Another thing is that you did not use any subroutine. If you have code that you could reuse in your logic, put them in sub can reduce the program size.
 
No purpose for the "INIT" other than lazy. I did everything in modules in excel. I felt it was easier to insert between than it would be to rearrange everything. As far as the subs, I didn't think of it like that. Thanks for pointing that out.
My init is begin. I thught because was a Label, start would need a pointer to know where to go next..
 
Is there a better way to toggle the bits to off rather than using ANDB? XORB will only toggle the bits on or off and unless I add extra code to test before toggle. Unless it's my birthday, I hate surprises. At least thats how I understand the XOR function. If power goes out, I want the system to go through a full check. I need to add some code to skip areas if it is already set. Not that I need this super fast but I should learn how to code more efficiently and neater which i why I'm open for suggestions and criticism.
 
XORB will only toggle the bit you like to toggle. Since bitwise XOR only changes the bit that is different to mask 1, same to mask 0, so for example
010101010101011111
000000000000000010
XORB =
010101010101011101

or
010101010101011111
000000000000100000
XORB =
010101010101111111
You define what each bit actual representing in your code.

That is different from ANDB
ANDB is probably best to use to check if the bit is set
010101010101011111
000000000000000010
ANDB =
000000000000000010

or
010101010101011111
000000000000100000
ANDB =
000000000000000000

Correct me if I am wrong.
 
Your right. My concern with toggling is I would need to know its state before I toggle it wouldn't I? Say for example if I want to set the bit to 1, I cant use a mask with 1 if it is already a 1 because that would turn it off. If it's already a 1, I would need to skip this step and come back when I need to toggle it to a zero. So simply using ORB and ANDB saves me a few lines or doesn't it? I work night shift so have been up all night and day and am low on active brain cells at the moment.
What you're getting at is test for state then skip step if toggle not needed?
 
Normally in a program, the coder knows in which condition he will toggle a bit. You may use ANDB to test a bit, but toggle the bit, XORB is easiest. If you just need to set a bit no matter what, you could use ORB.
 
Back
Top