2 - Forth Interest Group
2 - Forth Interest Group
2 - Forth Interest Group
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
: >ON ( b --1 #PORT bset , \ turn ON device<br />
: >OFF ( b --1 #PORT breset ; \ turn OFF device<br />
: TOGGLE ( b -- ) #PORT btoggle ; \ TOGGLE device<br />
: KILL ( -- ) 00 #PORT pc ! ; \ turn OFF all devices<br />
: ALL-ON ( -- ) $FF #PORT pc! ; \ turn ON all devices<br />
: ON? ( b - - f ) #PORT pc@ and O ; \ get ON status of device<br />
: OFF? ( b - - f ) #PORT pc@ and 0= ; \ get OFF status of device<br />
: WRITE (b--) #PORT pc! ; \ WRITE byte to port<br />
: READ (--b) #PORT pc@ . ; \ READ byte at port<br />
: BINARY ( -- ) 2 base ! \ change base to binary<br />
: TOGGLE ( b --- ) #PORT BTOGGLE ;<br />
The word TOGGLE can be used to change the status of any<br />
device on the port. If the device is on, a TOGGLE command<br />
will turn it off. If the device is off, a TOGGLE command will<br />
turn it on. This command can be useful to create digital<br />
pulses. It will take the present condition of port bit and<br />
invert it for a selected time, then toggle it again to the<br />
original condition.<br />
FAN TOGGLE<br />
If fan is on, the command will turn it off.<br />
If fan is off, the command will turn it on.<br />
The words ALL-ON and KILL will control the states of<br />
all devices:<br />
: KILL ( --- ) 00 #PORT PC! ;<br />
: ALL-ON ( --- 1 $FF #PORT PC! ;<br />
The word KILL can be used to shut down all devices<br />
on the port. It can also be used at the beginning of the<br />
program to clear the port to a known condition.<br />
ALL-ON<br />
will turn on all devices<br />
KILL<br />
will turn off all devices.<br />
We can now make some words that will check the<br />
status of each device. After a status check of a device, a<br />
branch can occur depending on the flag value.<br />
: ON? ( b --- f ) #PORT PC@ AND O ;<br />
: OFF? ( b --- f ) #PORT PC@ AND O= ;<br />
FAN ON?<br />
Will return a true flag if the device is on, or a false flag if<br />
it is off.<br />
July 1995 August<br />
FAN OFF?<br />
Will return a true flag if the device is off, or a false flag if<br />
it is on.<br />
To turn on or off any combination of devices, we can<br />
use the following code:<br />
: BINARY ( --- ) 2 BASE ! ;<br />
BINARY<br />
11110000 WRITE<br />
The output port will match the binary byte. A "1" will cause<br />
the LED to be on. A "0" will cause the LED to be off.<br />
READ<br />
will show the status of the port (e.g., 11 110000).<br />
Note: Any error will cause BASE to go back to DECIMAL.<br />
Here are some other commands we can try. On the<br />
same line, type:<br />
MOTOR TOGGLE 2 SECONDS MANY<br />
The MOTOR LED will blink on and off every two seconds.<br />
To end the cycle, hit any key.<br />
MOTOR >ON 2 SECONDS MOTOR >OFF<br />
2 SECONDS 10 TIMES<br />
The MOTOR LED will come on for two seconds and then<br />
off for two seconds. This will be repeated ten times.<br />
To speed it up, type:<br />
MOTOR >ON 10 0 MS MOTOR >OFF<br />
100 MS 10 TIMES.<br />
The on/off time has been changed to 100 milliseconds.<br />
To build more complex control structures, we can<br />
incorporate multiple devices in the control byte. In our<br />
imaginary factory, we have a large mixing tank that needs<br />
to be cleaned out at the end of the day. To do this, we open<br />
up the VALVE at the bottom of the tank and PUMP water into<br />
it. After the tank is flushed out, we turn off the PUMP and<br />
22 <strong>Forth</strong> Dimensions