Engineering Education Scheme
Barrier Controller Forth Code
( Barrier Controller James Bulpin 1995 )
( ============================================== )
HEX
0 VARIABLE MEM
( Low level operations )
: MS 0 DO 5 0 DO LOOP LOOP ; ( n millisecond delay loop )
( Output operations )
: DRIVEBOLTOFF PE C@ FB AND PE C! ; ( Turns solenoid current off )
: DRIVEBOLTON PE C@ FB AND 04 OR PE C! ; ( Turns solenoid current on )
: UNLOCKDIR PE C@ F7 AND PE C! ; ( Changes lock direction bit to unlock )
: LOCKDIR PE C@ F7 AND 08 OR PE C! ; ( Changes lock direction bit to lock )
: LOWTM PE C@ BF AND PE C! ; ( Takes timer cancel line low )
: HIGHTM PE C@ BF AND 40 OR PE C! ; ( Takes timer cancel line high )
: LOWTMST PE C@ DF AND PE C! ; ( Takes timer start line low )
: HIGHTMST PE C@ DF AND 20 OR PE C! ; ( Takes timer start line high )
: MOTOROFF PE C@ FE AND PE C! ; ( Turns motor off )
: MOTORON PE C@ FE AND 01 OR PE C! ; ( Turns motor on )
: GOCLOSE PE C@ FD AND PE C! ; ( Sets direction bit to close )
: GOOPEN PE C@ FD AND 02 OR PE C! ; ( Sets direction bit to open )
: BEACONON PE C@ EF AND 10 OR PE C! ; ( Turns beacon on )
: BEACONOFF PE C@ EF AND PE C! ; ( Turns beacon off )
( Input operations )
: ?KEYSWITCH PA C@ 02 AND 02 = NOT ; ( True flag if key switch operated )
: ?TIMER PA C@ 04 AND 04 = ; ( True flag if timer pulse high )
: ?RXA PB C@ 01 AND 01 = NOT ; ( True flag if button A pressed )
: ?RXB PB C@ 02 AND 02 = NOT ; ( True flag if button B pressed )
: ?CLOSED PB C@ 04 AND 04 = NOT ; ( True flag if barrier fully closed )
: ?OPEN PB C@ 08 AND 08 = NOT ; ( True flag if barrier fully open )
: ?ESTOP PB C@ 40 AND 40 = NOT ; ( True flag if emergency stop pressed )
: ?IR PB C@ 80 AND 80 = NOT ; ( True flag if ir beam broken )
( Timer operations )
: STARTTM LOWTMST 500 MS HIGHTMST ; ( Pulses timer start low for 500 milliseconds )
: CANCELTM LOWTM 500 MS HIGHTM ; ( Pulses timer cancel low for 500 milliseconds )
( Lock operations )
: PULSEBOLT DRIVEBOLTON 500 MS DRIVEBOLTOFF ; ( Pulse solenoid for 500 milliseconds )
: UNLOCKBOLT UNLOCKDIR PULSEBOLT ; ( Unlocks bolt )
: LOCKBOLT LOCKDIR PULSEBOLT ; ( Locks bolt )
: LOCKINIT ?CLOSED IF LOCKBOLT ELSE UNLOCKBOLT THEN ;
( Locks bolt if fully closed else unlocks )
( Memory operations )
: INITMEM 1 MEM ! ; ( Initialises memory to open )
: CHNGMEM MEM @ NOT MEM ! ; ( Reverses memory )
( Initiation operations )
: SAFE MOTOROFF GOOPEN BEACONOFF HIGHTM HIGHTMST ; ( Sets outputs to safe values )
: SETUP SAFE INITMEM ; ( Calls initialisation words )
: INITIAL SETUP LOCKINIT ; ( Defines steps in ititialisation )
( Drive flowchart routines )
: CLSDCHK ?CLOSED IF LOCK THEN ; ( Lock if fully closed )
: OPENCHK ?OPEN IF STARTTM THEN ; ( Start timer if fully open )
: CHECK OPENCHK CLSDCHK ; ( Do fully open or closed checks )
: CHKOPEN ?ESTOP ?KEYSWITCH OR ?RXA OR ?OPEN OR ; ( Check for stop input while opening )
: CHKCLOSE ?ESTOP ?IR OR ?KEYSWITCH OR ?RXA OR ?CLOSED OR ;
( Check for stop input while closing )
: STOP MOTOROFF BEACONOFF ; ( Stop motor and trun off beacon )
: CLOSE GOCLOSE MOTORON BEACONOFF ; ( Start or continue closing )
: OPEN GOOPEN MOTORON BEACONON ; ( Start or continue opening )
: -CLOSE UNLOCK BEGIN CHKCLOSE WHILE CLOSE REPEAT ; ( Close and check inputs )
: -OPEN UNLOCK BEGIN CHKOPEN WHILE OPEN REPEAT ; ( Open and check inputs )
: MOVE MEM @ IF -OPEN ELSE -CLOSE THEN STOP ; ( Move barrier )
: DRIVE MOVE CHECK ; ( Move and check barrier )
( Main routine operations )
: ?COMMAND BEGIN ?RXA ?RXB OR ?TIMER OR ?KEYSWITCH OR UNTIL ;
( Waits for key-fob, timer or keyswitch input )
: RUN DRIVE CHNGMEM ; ( Runs drive routine and changes memory )
: ACTION ?RXB IF CANCELTM ELSE RUN THEN ; ( Checks which button, if B, cancels timer else runs motor )
: ROUTINE BEGIN ?COMMAND ACTION AGAIN ; ( Infinite loop scanning inputs and performing action)
: PROG INITIAL ROUTINE ; ( Main word, initiates and runs routines )
|