
WRA             EQU P3.4
INTR            EQU P3.5
RDA             EQU P3.3

TL              EQU 07Bh        ; Endereços das variaveis
TH              EQU 07Ch
TP              EQU 07Dh
V0              EQU 07Eh
V1              EQU 07Fh        ; DONT USE OVER THIS - SFR ADDRESS START


;P3.4 <-->WR(to start convert analog - binary (0804)we must have WR 0->1)
;P3.5 <-->INTR (=0 to and convert) 
;P3.3 <-->RD (RD 1->0 to read data from ADC)
;P2 <-->D0-D7 (out put) 
;-----------------------------------------------------------;

START:  MOV PCON,#0
        MOV TMOD,#00100010B ;Set Timer1 Mode for 8bit autoreload (T1M1=1)
        MOV TL1,#255        ;Set load/reload
        MOV TH1,#255        ;
        SETB TR1
        MOV SCON,#01000010B
        MOV P2,#255         ; to receive data from ADC
        MOV P1,#00000000b   ; Mux Input 0
        MOV R0,#080h
        MOV R2,#8
        MOV R3,#9
        
READ0:  CALL READ           ; Read ADC
        MOV P1,A
        CLR C               ; Midi adjust (MSB must be 0)
        RRC A               ; Midi adjust
        MOV TP,@R0
        CJNE A,TP,SEND0
        JMP INCI            ; EQUAL read next
SEND0:  MOV @R0,A
        CALL SEND
        
        
INCI:   MOV A,R3            ; INCREMENT FOR NEXT READ
        MOV R2,A            ; V
        MOV R0,A            ; V
        MOV A,#080h
        ORL A,R0
        MOV R0,A
        INC R3              ; V
        CJNE R3,#20,ISO     ; #N = NUMBER OF VALUES TO BE READ
        MOV R3,#8           ; V
ISO:    JMP READ0           ; START OVER




READ:   MOV P1,R2           ; Set mux Input
        MOV TL,#255
        MOV TH,#5
        CALL DELAY
        CLR WRA
        SETB WRA            ; dat WR = 1 to start convert
        JB INTR,$           ; wait to end convert 
        CLR RDA             ; end convert and allow read data from ADC
        MOV A,P2            ; read data onto A register
        MOV P1,R3           ; Set next input
        SETB RDA
        ;MOV P1,R3
        RET
        
SEND:   JNB TI,$            ; If diff send midi
        CLR TI
        MOV SBUF,#0B0H      ;Control Change Channel 0
        JNB TI,$
        CLR TI
        MOV SBUF,R2         ;Controller Number
        JNB TI,$
        CLR TI
        MOV SBUF,A          ;Controller Value
        RET
        
        




;------------------------------
Delay:  DJNZ TL,$
        MOV TL,#255
        DJNZ TH,Delay
        ;MOV TH,#255
        ;DJNZ TS,DTL
        RET


END
