
; NOTE:  THIS IS A NON-BOOTUP PROGRAM, IT DOESN'T HAVE LINES TO CLEAR THE WDT
; SINCE THAT'S BEEN TAKEN CARE OF BY THE SMALL-C MONITOR.

        $MOD51GB

PWMCNT  EQU     R2                  ; use R2 as the PMW duty cycle counter 

        ORG     8000H

        MOV     P1,#00FFH           ; IN ORDER FOR THE PWM SIGNAL TO TAKE EFFECT,
                                    ; ALL OUTPUT BITS ON PORT 1 MUST BE SET HIGH

        MOV     PWMCNT,#03BH         ; START THE PW VALUE WITH 01
        MOV     CCAP1H, PWMCNT      ; LOAD THE PWM REGISTER WITH THE PW VALUE
        CALL    PCA_INIT            ; INITIALIZE THE PWM

MAIN_LOOP:

        CALL    CIN                 ; WAIT FOR A KEY TO BE PRESSED
        CJNE    A,#'1',K2           ; IF KEY='1'
        mov     PWMCNT,#031h        ; Turn wheels full left
        MOV     CCAP1H, PWMCNT      ; LOAD THE PWM REGISTER WITH THE PW VALUE
        SJMP    MAIN_LOOP

K2:     CJNE    A,#'2',K3           ; IF KEY='2'
        mov     PWMCNT,#03bh        ; center wheels
        MOV     CCAP1H, PWMCNT      ; LOAD THE PWM REGISTER WITH THE PW VALUE
        SJMP    MAIN_LOOP

K3:     CJNE    A,#'3',KQ           ; IF KEY='3'
        mov     PWMCNT,#04bh        ; Turn the wheels full right
        MOV     CCAP1H, PWMCNT      ; LOAD THE PWM REGISTER WITH THE PW VALUE
        SJMP    MAIN_LOOP

KQ:     CJNE    A,#'q',KW           ; IF KEY='q'
        inc     PWMCNT              ; tick wheels to left
        MOV     CCAP1H, PWMCNT      ; LOAD THE PWM R
        SJMP    MAIN_LOOP

KW:     CJNE    A,#'w',MAIN_LOOP
        dec     PWMCNT
        MOV     CCAP1H,PWMCNT
        SJMP    MAIN_LOOP

PCA_INIT:
        MOV     CMOD,#006h
        MOV     CCAPM1,#42h
        MOV     CCAP1H,PWMCNT
        SETB    CR
        RET

CIN:
        JNB     RI,$             ; WAIT FOR A BYTE
        CLR     RI               ; CLEAR THE RECEIVE FLAG ONCE RECIEVED
        MOV     A,SBUF           ; GET THE RECEIVED BYTE INTO THE A REGISTER
        RET


        END
    
