
; 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     CCAP0H, 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,#062h        ; Place the sonar at 90 left
        MOV     CCAP0H, PWMCNT      ; LOAD THE PWM REGISTER WITH THE PW VALUE
        SJMP    MAIN_LOOP

K2:     CJNE    A,#'2',K3           ; IF KEY='2'
        MOV     PWMCNT,#056h        ; Place the sonar at 60 left
        MOV     CCAP0H, PWMCNT      ; LOAD THE PWM REGISTER WITH THE PW VALUE
        SJMP    MAIN_LOOP

K3:     CJNE    A,#'3',K4           ; IF KEY='3'
        MOV     PWMCNT,#048h        ; Place the sonar at 30 left
        MOV     CCAP0H, PWMCNT      ; LOAD THE PWM REGISTER WITH THE PW VALUE
        SJMP    MAIN_LOOP

K4:     CJNE    A,#'4',K5           ; IF KEY='4'
        MOV     PWMCNT,#03bh        ; Place the sonar at 0
        MOV     CCAP0H, PWMCNT      ; LOAD THE PWM REGISTER WITH THE PW VALUE
        SJMP    MAIN_LOOP

K5:     CJNE    A,#'5',K6           ; IF KEY='5'
        MOV     PWMCNT,#02dh        ; Place the sonar at 30 right
        MOV     CCAP0H, PWMCNT      ; LOAD THE PWM REGISTER WITH THE PW VALUE
        SJMP    MAIN_LOOP

K6:     CJNE    A,#'6',K7           ; IF KEY='6'
        MOV     PWMCNT,#021h        ; Place the sonar at 60 right
        MOV     CCAP0H, PWMCNT      ; LOAD THE PWM REGISTER WITH THE PW VALUE
        SJMP    MAIN_LOOP

K7:     CJNE    A,#'7',MAIN_LOOP    ; IF KEY='7'
        MOV     PWMCNT,#015h        ; Place the sonar at 90 right
        MOV     CCAP0H, PWMCNT      ; LOAD THE PWM REGISTER WITH THE PW VALUE
        SJMP    MAIN_LOOP

PCA_INIT:
        MOV     CMOD,#006H          ; PCA FREQUENCY = 1/Timer 0 overflow
        MOV     CCAPM0,#42H         ; PUT PCA MODULE 0 INTO PWM MODE
        MOV     CCAP0H, PWMCNT      ; INITIALIZE DUTY CYCLE
        SETB    CR                  ; START THE PCA CLOCK
        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
    
