Link here

DC Relay Forth Demo


This demonstration program turns each relay on in sequence and then turns them off in sequence. Next it turns all of the relays on, and then turns them all off. This example serves as a good starting point for your instrumentation projects.

DC Relay Forth Demo

\ *********************************************************************
\ FILE NAME:   DCRELAY.4TH
\ ---------------------------------------------------------------------
\ AUTHOR:      DAVID J. SIU
\              BEN MORSE
\ DATE:        5/6/2009
\ VERSION:     1.1
\ ---------------------------------------------------------------------
\ This is the driver code for the DC Relay Module.
\ This code:
\     -Initializes the DC Relay Module.
\     -Turns a relay on or off.
\     -Returns the state of the relays.
\ ---------------------------------------------------------------------
\ Important user words:
\ Init_DC_Relay:         Initializes the DC Relay Module.
\ Control_DC_Relay:      Turns the specified relay on or off.
\ Read_DC_Relay_Status:  Returns the state of the relays.
\ ---------------------------------------------------------------------
\ Notes:
\ CRYDOM RELAYS ARE ACTIVE LOW!  WRITING A 1 TO THE RELAY TURNS IT OFF WHILE
\ WRITING A 0 TO THE RELAY TURNS IT ON!
\
\ Disclaimer: THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT
\             ANY WARRANTIES OR REPRESENTATIONS EXPRESS OR IMPLIED,
\             INCLUDING, BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES
\             OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
\
\ *********************************************************************
 
BASE @ HEX
FIND WHICH.MAP          \ WHICH.MAP does not exist in the V6.xx kernel.
IFTRUE                  \ Do this only for page-swapping platforms, ie V4.xx
  EXECUTE  0=           ( -- standard.map? ) \ run which.map
  IFTRUE 4 PAGE.TO.RAM  \ if in standard.map, transfer to download map
         5 PAGE.TO.RAM
         6 PAGE.TO.RAM
         DOWNLOAD.MAP
  ENDIFTRUE             \ nesting is allowed if ends are sequential
ENDIFTRUE
 
FIND USE.PAGE   \ only in V4.xx kernels
IFTRUE
    XDROP         ( -- ) \ drop xcfa
    4 USE.PAGE    \ comment this out if memory map is already set up
ENDIFTRUE
 
\ the following line includes a do-nothing file if kernel V6.xx is used
\ (ie, if IO.C@, IO.C!, etc. are already defined):
#include "..\..\libraries\include\QED_QCard_IO.4th"
 
F WIDTH !                              \ Set width of names stored in dictionary
 
ANEW DCR.CODE                           \ Forget marker for easy re-loading.
 
\ Constants
 
\ Relays are active low (i.e. writing a 0 to the relay turns it on).
0  CONSTANT RELAY_ON
1  CONSTANT RELAY_OFF
 
1  CONSTANT RELAY_CONTROL_LINES
F  CONSTANT ALL_RELAYS
05  CONSTANT DIRECTION_OFFSET
00  CONSTANT RELAY_CONTROL_OFFSET
 
: Init_DC_Relay ( byte -- | byte = module num. Valid module numbers are 0-7 )
\ Initializes the DC Relay Module by configuring the DC relay control lines
\ of the CPLD to outputs.  The module number depends on the module select
\ jumpers.  See Table 1 for the jumper settings and associated addresses.
locals{ &module }
 
  \ Disconnect all relays before initializing control lines to outputs.
  \ Relays are active low (i.e. writing a 0 to the relay turns it on).
  ALL_RELAYS RELAY_CONTROL_OFFSET &module IO.C!
  RELAY_CONTROL_LINES DIRECTION_OFFSET &module IO.C!
;
 
: Control_DC_Relay ( byte1\byte2\byte3 -- )
\ Sets the relay number to the appropriate state (on or off).
\ byte1 = Module Number.  Valid module numbers are 0-7.
\ byte2 = Relay Number.  Valid relay numbers are 0-2.
\ byte3 = Relay State.  Valid relay states are RELAY_ON or RELAY_OFF
locals{ &state &relay_num &module }
 
  &state
  IF              \ turn relay off
    &state &relay_num SCALE
    RELAY_CONTROL_OFFSET &module IO.SET.BITS
  ELSE                \ turn relay on
    1 &relay_num SCALE
    RELAY_CONTROL_OFFSET &module IO.CLEAR.BITS
  ENDIF
;
 
 
: Read_DC_Relay_Status ( byte -- | byte = module_number )
\ Reads the current state of the solid state opto isolated DC Relays (SSRS).
\ Valid module numbers are 0-7.
\ Returns a character whose three least significant bits represents the
\ three relays.  For example, if 1 is returned (001 in binary), then Relay 0
\ is off and the other relays are on.  If 6 is returned (110 in binary),
\ then relays 1 and 2 are off and 0 is on.  The 5 most significant bits do
\ not matter.
 
  RELAY_CONTROL_OFFSET SWAP IO.C@
;
 
\ Toplevel word which controls your instrument
: TEST.MOD ( byte -- \ byte = module.no )
 
LOCALS{ &module.no }
 
&module.no Init_DC_Relay        \ initialize the dc_relay
CR ." press enter to turn on the first relay."
KEY  DROP
&module.no 0 RELAY_ON Control_DC_Relay \ turn on 1st relay
CR ." press enter to turn on the second relay."
KEY  DROP
&module.no 1 RELAY_ON Control_DC_Relay \ turn on 2nd relay
CR ." press enter to turn on the third relay."
KEY  DROP
&module.no 2 RELAY_ON Control_DC_Relay \ turn on 3rd relay
CR ." press enter to turn off the first relay."
KEY  DROP
&module.no 0 RELAY_OFF Control_DC_Relay \ turn off 1st relay
CR ." press enter to turn off the second relay."
KEY  DROP
&module.no 1 RELAY_OFF Control_DC_Relay \ turn off 2nd relay
CR ." press enter to turn off the third relay."
KEY  DROP
&module.no 2 RELAY_OFF Control_DC_Relay \ turn off 3rd relay
CR ." press enter to turn on all three relays."
KEY  DROP
ALL_RELAYS RELAY_CONTROL_OFFSET &module.no IO.CLEAR.BITS
CR ." press enter to turn off all three relays."
KEY  DROP
ALL_RELAYS RELAY_CONTROL_OFFSET &module.no IO.SET.BITS
;
 
FIND WHICH.MAP
IFTRUE EXECUTE
        IFTRUE 4 PAGE.TO.FLASH
               5 PAGE.TO.FLASH
               6 PAGE.TO.FLASH STANDARD.MAP
        ENDIFTRUE
ENDIFTRUE
BASE !  SAVE



For more usage examples, see the C version of this program.

See also → DC Relay C Demo
DC Relay Wildcard Users Guide

 
This page is about: Embedded DC Relay Example Program, Solid State Relay Control, Forth Embedded Language – Simple example controls DC loads with Solid State Relays and embedded controller. DC Load, Solid State Relay, Fourth program example, DC Relay Control
 
 
Navigation