;**************************************************************************
;***                                                                    ***
;*** Everyday Electronics Micro-Lab                                     ***
;***                                                                    ***
;***    Name:           Demonstration 4                                 ***
;***    Version:        1.0                                             ***
;***    Author:         Geoff MacDonald                                 ***
;***    Date:           13/01/1993                                      ***
;***    Last Update:                                                    ***
;***                                                                    ***
;**************************************************************************

                ;Read a temperature via the analogue input and switch on
                ;a lamp if the temperature falls below an adjustable level.

D_DEMO4:        JSR     CLEAR           ;Clear display
                LDA     #$90            ;Set initial required temp. set-point
                STA     $90
                LDA     #$FF            ;Initialise 65C22
                STA     VIA+2
DEMO4A:         LDA     $90             ;Show required temp. set-point
                LSR     A
                LSR     A
                LSR     A
                LSR     A
                TAX
                LDY     #0
                LDA     #$7C
                JSR     POSCHAR
                LDX     #0              ;Write set point in Hex.
                LDY     #0
                LDA     #0
                JSR     CURSOR
                LDA     $90
                JSR     HEXOUT
DEMO4I:         LDX     #0              ;Position cursor at left side
                LDY     #1              ;of bottom row of screen
                LDA     #0              ;with cursor switched off
                JSR     CURSOR
                STA     $FFFF           ;Initiate A/D conversion
                LDY     #10
DEMO4B:         DEY
                BNE     DEMO4B
                LDA     AIN             ;Display current temp. as a bargraph...
                PHA                     ;Save temperature
                LSR     A               ;Get bargraph length...
                LSR     A
                LSR     A
                LSR     A
                TAX                     ;...in IX
                LDY     #$10            ;Get display line length in IY
DEMO4C:         LDA     #$FF            ;Display a solid block...
                JSR     OPCHAR
                DEY
                DEX
                BPL     DEMO4C          ;...the required number of times
DEMO4H:         LDA     #$20            ;Clear the rest of the line
                JSR     OPCHAR          ;   by writing space characters
                DEY
                BNE     DEMO4H
                PLA                     ;Restore temperature reading
                CMP     $90             ;Compare it with the set-point
                BCC     DEMO4D
                LDX     #$00            ;Too hot - turn lamp off
                BEQ     DEMO4E
DEMO4D:         LDX     #$01            ;Too cold - turn lamp on
DEMO4E:         STX     VIA
                LDA     KBSIZE          ;Key in buffer?
                BEQ     DEMO4I          ;No - get next temp. reading
                LDA     $90             ;Clear temperature set-point char.
                LSR     A
                LSR     A
                LSR     A
                LSR     A
                TAX
                LDY     #0
                LDA     #$20
                JSR     POSCHAR
                JSR     KBLAST          ;Get ASCII value of key
                CMP     #$30            ;Key = "0"?
                BNE     DEMO4F
                LDX     $90             ;Yes - set-point > 0?
                BEQ     DEMO4J
                DEC     $90             ;Yes - decrement set-point
                JMP     DEMO4J
DEMO4F:         CMP     #$31            ;Key = "1"?
                BNE     DEMO4G
                LDX     $90             ;Yes - setpoint < 255?
                CPX     #$FF
                BEQ     DEMO4J
                INC     $90             ;Yes - increment set-point
DEMO4J:         JMP     DEMO4A
DEMO4G:         CMP     #$1B            ;Key = "ESC"?
                BNE     DEMO4J
                BRK                     ;Yes - terminate
