Thu May 16, 2013 5:27 pm
;SUCCESSIVE APPROXIMATION ADC
TITLE "SAR ADC"
list p =16f877
include <p16f877.inc>
BANK0 macro
bcf STATUS,RP0
bcf STATUS,RP1
endm
BANK1 macro
bsf STATUS,RP0
bcf STATUS,RP1
endm
BANK2 macro
bcf STATUS,RP0
bsf STATUS,RP1
endm
BANK3 macro
bsf STATUS,RP0
bsf STATUS,RP1
endm
COUNTER EQU 0x20
RESULT EQU 0x21
MASK EQU 0x22
DELAY_TIME EQU 0x23
COMP EQU 4
TEMP EQU 0x24
; ******
ORG 0
NOP
GOTO START
;****
; MAIN ROUTINE
ORG 0x20
START CALL INIT ;INITIAbISE PORTS
REPEAT MOVLW 8 ;COUNTER = 0
MOVWF COUNTER;
MOVLW 0x80 ;SET MASK.
MOVWF MASK;
MOVWF RESULT ;SET INIT RESULT
NEXT MOVF RESULT,W ;GET CJRRENT RESULT VALUE
IORWF MASK,W ;SET NEXT MSB
MOVWF PORTD ;AND OUTPUT
CALL DELAY ;ALLOW TIME TO SETTLE
BTFSS PORTA,COMP ;TEST COMP OUTPUT
CALL SETBIT ;IF SET, CALL SETBIT
BTFSC PORTA,COMP ;TEST COMP BIT
CALL CLEARBIT ;ELSE CLEAR BIT
BCF STATUS,C ;CLEAR CARRY FLAG
RRF MASK ;SET NEXT MSB
MOVLW 1 ;SET DECREb€NT
SUBWF COUNTER ;AND DECREMENT
BTFSS STATUS,Z ;TEST FOR LAST BIT
GOTO NEXT ;AND DO NEXT BIT IF NOT
GOTO REPEAT ;ELSE REPEAT
;****
SETBIT MOVWF RESULT
RETURN
; CLEAR BIT
CLEARBIT
COMF MASK,W; GET CURRENT MASK AND INVERT
ANDWF RESULT; AND CLEAR CURRENT BIT
RETURN
; PORT INITIALISATION
INIT BANK1 ; set to bank1
CLRF TRISD; SET PORT D TO OUTPUT
MOVLW 06; MAKE PORT A DIGITAL
MOVWF ADCON1
BSF TRISA,4 ; AND BIT A4 to INPUT
BANK0; BACK TO BANK 0
RETURN
; DELAY ROUTIN
DELAY MOVWF TEMP ; SAVE W
MOVLW 0x80; set delay time
MOVWF DELAY_TIME
WAIT INCFSZ DELAY_TIME ; COUNT DOWN
GOTO WAIT
MOVF TEMP,W
RETURN
END
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.