;FILENAME:   STLAB022.ASM
;DATE:       March 16, 1996
;TITLE:      DC motor starting and stopping control
;HARDWARE:   MLAB-121

; JAZZ-31 utilities subroutines
SCANK   EQU     0099H   ;scan display & KB 1 LOOP
UBEEP   EQU     00A5H   ;BEEP
DM_SEC  EQU     003FH   ;Delay 1/1000 second
; Pin Interface
DCM_CTL EQU     P1.7    ;DC motor control
SENSOR  EQU     P3.2    ;sensor status
; Variable
DISBUF  EQU     032H    ;display buffer
CNTR    EQU     20H     ;counter
CYCLE   EQU     22H     ;cycle

;Program 2:  Rotation speed control

        ORG     8100H
START:
        LCALL   INIT_DISPLAY    ;initial display buffer
        MOV     CNTR,#15H
LOOP:
        MOV     CYCLE,#40H
        LCALL   SCANK
        CJNE    A,#0FFH,NXTKEY1
        SJMP    LOOP02          ;without press
NXTKEY1:
        CJNE    A,#0FH,NXTKEY2  ;key 'f' press
        LCALL   UBEEP
        MOV     A,CNTR
        CJNE    A,#10H,LOOP01   ;is equal lower limit?
        SJMP    LOOP02          ;jump, if equal
LOOP01:
        JC      LOOP02          ;also jump if below limit
        SUBB    A,#5
        MOV     CNTR,A
LOOP02:
        LCALL   DRIVE_MOTOR
        SJMP    LOOP
NXTKEY2:
        CJNE    A,#0BH,LOOP02   ;key 'b' press
        LCALL   UBEEP
        MOV     A,CNTR
        CJNE    A,#2EH,LOOP21   ;is equal upper limit?
        SJMP    LOOP02          ;jump, if equal
LOOP21:
        JNC     LOOP02          ;also jump if greater
        ADD     A,#5
        MOV     CNTR,A
        SJMP    LOOP02
;
; Drive motor with Pulse Width Mode
;
DRIVE_MOTOR:
        SETB    DCM_CTL         ;stop motor
        LCALL   DELAY1
        MOV     A,CYCLE
        SUBB    A,CNTR
        MOV     CYCLE,A
        CLR     DCM_CTL         ;start motor
        LCALL   DELAY2
        RET
;
; Delay routines
;
DELAY1:
        MOV    R2,#00
        MOV    R3,CNTR
        LCALL  DM_SEC           ;delay cntr/1000 sec
        RET
DELAY2:
        MOV    R2,#00
        MOV    R3,CYCLE
        LCALL  DM_SEC           ;delay cycle/1000 sec
        RET
;
; Initial display buffer
;
INIT_DISPLAY:
        MOV     DISBUF+0,#5EH   ;d
        MOV     DISBUF+1,#58H   ;c
        MOV     DISBUF+2,#00H
        MOV     DISBUF+3,#08H   ;_
        MOV     DISBUF+4,#00H
        MOV     DISBUF+5,#00H
        MOV     DISBUF+6,#00H
        MOV     DISBUF+7,#00H
        RET
;
        END

