Q(uick)BASIC Statement: OUT
Quick View
OUT
A device I/O statement that sends a byte to a machine I/O port
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- INP(port%)
- OUT port%, data%
Description/Parameter(s)
port% | A number in the range 0 through 65,535 that identifies the port. |
data% | A numeric expression in the range 0 through 255 to send to the port. |
Example
x% = INP(&H3FC) 'Read COM1 Modem Control Register.
OUT &H3FC, (x% XOR 1) 'Change Data Terminal Ready bit.
See also:
Syntax
- OUT port, data
Description/Parameter(s)
Argument | Description |
port | The number of the port. The number must be an integer expression in the range 0-65,535. |
data | The data to be sent to the port. It must be an integer expression in the range 0-255. |
The OUT and INP statements give a BASIC program direct control over the hardware in a system through the I/O ports. These statements must be used carefully because they directly manipulate the hardware.
Example
OUT_EX.BAS is a program file in the subdirectory ADVR_EX that uses the OUT and INP statements to control the timer and speaker to produce a note. To look at the program in the View window and, optionally, to run it, use the File menu's Open Program command.
Syntax
- OUT port, data%
Description/Parameter(s)
port | A numeric expression with an integer value between 0 and 65,535, inclusive, that identifies the destination hardware I/O port. |
data% | A numeric expression with an integer value between 0 and 255, inclusive, that is the data to be sent to the port. |
- The OUT statement is not available in OS/2 protected mode.
Usage Notes
- The OUT statement complements the INP function, which returns the byte read from a hardware I/O port.
- OUT and INP give a BASIC program direct control over the system hardware through the I/O ports. OUT and INP must be used carefully because they directly manipulate the system hardware.
Example
This example uses the OUT statement and the INP function to control the timer and speaker to produce a note.
DECLARE SUB Sounds (Freq!, Length!)
'Play a scale using speaker and timer.
CONST WHOLE = 5000!, QRTR = WHOLE / 4!
CONST C = 523!, D = 587.33, E = 659.26, F = 698.46, G = 783.99, A = 880!
CONST B = 987.77, C1 = 1046.5
CALL Sounds(C, QRTR): CALL Sounds(D, QRTR)
CALL Sounds(E, QRTR): CALL Sounds(F, QRTR)
CALL Sounds(G, QRTR): CALL Sounds(A, QRTR)
CALL Sounds(B, QRTR): CALL Sounds(C1, WHOLE)
SUB Sounds (Freq!, Length!) STATIC
'Ports 66, 67, and 97 control timer and speaker.
'Divide clock frequency by sound frequency
'to get number of "clicks" clock must produce.
Clicks% = CINT(1193280! / Freq!)
LoByte% = Clicks% AND &HFF
HiByte% = Clicks% \ 256
'Tell timer that data is coming.
OUT 67, 182
'Send count to timer.
OUT 66, LoByte%
OUT 66, HiByte%
'Turn speaker on by setting bits 0 and 1 of PPI chip.
SpkrOn% = INP(97) OR &H3
OUT 97, SpkrOn%
'Leave speaker on.
FOR I! = 1 TO Length!: NEXT I!
'Turn speaker off.
SpkrOff% = INP(97) AND &HFC
OUT 97, SpkrOff%
END SUB