' *** OUT_EX.BAS -- OUT statement programming example ***
' This program uses OUT and INP to play a musical scale by
' controlling the timer and speaker.
'
' NOTE: This program contains hardware-specific instructions.
'       It works correctly on IBM PC, PC/XT, and PC/AT computers.
'
DECLARE SUB Sounds (Freq!, Length!, Note$)
CLS    'Clear screen
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, "Do"): CALL Sounds(D, QRTR, "Re")
CALL Sounds(E, QRTR, "Mi"): CALL Sounds(F, QRTR, "Fa")
CALL Sounds(G, QRTR, "Sol"): CALL Sounds(A, QRTR, "La")
CALL Sounds(B, QRTR, "Ti"): CALL Sounds(C1, WHOLE, "Do")

SUB Sounds (Freq!, Length!, Note$) 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%
'Print note in scale
    PRINT Note$
'Leave speaker on
    FOR I! = 1 TO Length!: NEXT I!
'Turn speaker off.
    SpkrOff% = INP(97) AND &HFC
    OUT 97, SpkrOff%
END SUB

