|
LIST P=16F877
INCLUDE <C:\WORK\P16F877.INC>
TEMP EQU 70H
FLAG1 EQU 71H
OPTION_BACK EQU 72H
ADH EQU 73H
ADL EQU 74H
COUNT EQU 75H
W_STACK EQU 76H
ST_STACK EQU 77H
ORG 0x0000
GOTO MAIN
ORG 0x0004
BTFSC PIR1,ADIF
GOTO ADS
RETFIE
ADS BTFSS ADCON0,GO
GOTO AD
RETFIE
ORG 0x0020
AD CALL PUSH
BCF PIR1,ADIF
BTFSS TEMP,2 ;TEST TEMP-4? YES,THEN CLEAR THE ADH,ADL. OR WILL GO TO AD CONVERTER
GOTO AD1
CLRF ADH
CLRF ADL
AD1 bsf status,rp0 ;select bank1
MOVF ADRESL,0
ADDWF ADL
BTFSC STATUS,C
INCF ADH
BCF STATUS,RP0 ;SELECT BANK0
MOVF ADRESH,0
ADDWF ADH
DECFSZ TEMP ;temp-1=0 ? no,then go to next ad,but yes will set the finished flag
GOTO AD2
MOVLW 0x04 ;initial temp again.
MOVWF TEMP
BSF FLAG1,1 ;set the AD finished flag
CALL POP
RETFIE
AD2 CALL DELAY ;CALL THE DELAY TIME ,FOR DELAY 2uS.
BSF ADCON0,GO ;START THE NEXT AD
CALL POP
RETFIE
PUSH MOVWF W_STACK
MOVF STATUS,0
MOVWF ST_STACK
BCF STATUS,RP0
RETURN
POP MOVF ST_STACK,0
MOVWF STATUS
MOVF W_STACK,0
RETURN
ADINI BCF STATUS,RP0
MOVLW B'10000001' ;AD INITIAL,OPEN THE AD
MOVWF ADCON0
BSF STATUS,RP0 ;SELECT BANK1
MOVLW B'10001110'
MOVWF ADCON1 ;SETUP ADCON1
BSF PIE1,ADIE
MOVLW B'10000111'
MOVWF OPTION_REG
BCF STATUS,RP0 ;SELECT BANK0
BCF PIR1,ADIF ; AD FLAG
BSF INTCON,GIE ;OPEN ALL THE INTRUPT
BSF INTCON,PEIE
RETURN
;***************************************************AD DISPLAY
ADOPR BCF FLAG1,1
MOVLW 0x04 ;SET UP THE LOOP NUMBER
MOVWF TEMP
CALL DELAY LACE DELAY BEFORE AD
BSF ADCON0,GO ;START AD CONVERTER
WAIT BTFSS FLAG1,1
GOTO WAIT
MOVLW 0x02
MOVWF COUNT
RER BCF STATUS,C
RRF ADH ;THE AD RESULT IN ADH AND ADL
RRF ADL
DECFSZ COUNT
GOTO RER
RETURN
;THE FOUR CLUASE ABOVE IS FOR NEXT CHECK LOOP,IT IS MAY PLACE IN THE MAIN LOOP.
;************************************************************************************
DELAY BCF INTCON,T0IF
CLRF TMR0
DEL BTFSS INTCON,T0IF
GOTO DEL
RETURN
MAIN CALL ADINI
CALL ADOPR
BANKSEL TRISB
MOVLW 0x00
movwf TRISB
MOVWF TRISC
BANKSEL PORTB
MOVF ADL,0
MOVWF PORTB
MOVF ADH,0
MOVWF PORTC
LOOP GOTO LOOP
END |
|