Hey everyone, i am new to the Pic 16f877 microcontroller and am having trouble doing analogue to digital conversion. can any one help me or even give me the codes. i would be very grateful.

6 Years
Discussion Span
Last Post by jwebbo

hey everyone, myself and others have managed to solve the problem.

;project to do analog to digital conversion
;done by jwebbo
porta equ 05
portb equ 06
trisa equ 85
trisb equ 86
portd equ 08
trisd equ 88
porte equ 09
trise equ 89 
status equ 03
pclath equ 0A
disreg equ 20
pcl equ 02
r1	equ 20 
r2  equ 21
r3  equ 22
r4  equ 23
r5  equ 24
r6  equ 25
r7  equ 26
r8  equ 27
r9  equ 28
r10 equ 29
r11 equ 30
r12 equ 31
r13 equ 32
r14 equ 33
r15	equ 34
r16 equ 35
r17	equ 36

adresh equ 1E
adresl equ 9E
adcon0 equ 1F
adcon1 equ 9F
pir1 equ 0C
pie1 equ 8C
intcon equ 0B

org 00
clrf status
movlw 00
movwf pclath
goto start

clrf r1
clrf r2
clrf r3
clrf r4
clrf r5
clrf r6
clrf r7
clrf r8
clrf r9
clrf r10
clrf r11
clrf r12
clrf r13
clrf r15
clrf r16
clrf r17
clrf r14
clrf porte
clrf adresh ;clear a/d result high register.
clrf porta
clrf pir1
movlw b'01000011'
movwf adcon0
bsf status,5
movlw b'11111111'
movwf trisa
movlw b'00000000'
movwf trisb
movlw b'00000000'
movwf trisd
movlw b'00000000'
movwf trise
movlw b'10000010' 
movwf adcon1
clrf  adresl
bcf status,5
retlw 00 

delay1 movlw d'24' ;this delay loop is to allow for a correct reading (wait acquisition time).The delay is 24 micro seconds.
movwf r10
loop1 decfsz r10,f
goto loop1
retlw 00

DELAY movlw d'1'
movwf r11
LOOP3 movlw d'1'
movwf r12
LOOP2 movlw d'250'
movwf r13
DECFSZ r12,f
sevseg addwf pcl,f
retlw 3F
retlw 06
retlw 5B
retlw 4F
retlw 66
retlw 6D
retlw 7D
retlw 07
retlw 7F
retlw 6F
start call init

;-----------Initiate a/d conversion----------------
bcf porta,0

;bsf  pie1,6  ;clearing ADIE bit
;bsf status,5
;bsf pir1,6   ;clearing ADIF bit
;bcf status,5
;bsf  intcon,6 ;setting PEIE bit
;bsf  intcon,7 ;setting GIE bit  wwwwwwwwwwwwwwwwwwwwwwwww
call delay1 ;waiting for the acquisition time
bsf adcon0,2 ;this instruction starts the a/d conversion progress.
;once the conversion is completed,the go/done bit is automatically cleared and we need to check for the this.

test btfsc adcon0,2
goto test

clrf r1
clrf r2
clrf r3
clrf r4
clrf r5
clrf r6
clrf r7
clrf r8
bsf status,5
movf adresl,w
bcf   status,5

	     movwf    r1          ; r1 contains the lower bytes
	     movf     adresh,w
	     movwf    r2          ; r2 contains high bytes
	     incf     r2          ; to correct for error (adds 256 to result)
test_1	 movf     r1,w
	     movwf    r3		; holds the remainder of the subtraction
	     movlw    d'100'
	     subwf    r1,f
	     incf     r4        ; contains hundreds
	     btfsc    status,0  ;  
	     goto     test_1	     
	     decfsz   r2
	     goto     test_1
	     decf     r4             
	     bcf      status,0
test_2   movf     r4,w
	     movwf    r5       ; contains hundreds
	     movlw    d'10'
	     subwf    r4,f
	     incf     r6
	     btfss    status,0
	     goto     forward
	     goto     test_2
forward  decf      r6       ;   contains thousands	     
	     bcf       status,0
test_3   movf     r3,w
         movwf    r7         ; contains ones
         movlw    d'10'
         subwf    r3,f
         incf     r8
         btfss    status,0
         goto     forward_2
         goto     test_3
forward_2 decf    r8         ; contains tens              
          bcf     status,0
         ;goto     display
         movf     r6,w	     
	     call     sevseg
	     movwf    portb
	     bsf      portd,0
	     call     DELAY
	     bcf      portd,0
	     movf     r5,w        
	     call     sevseg
	     movwf    portb
	     bsf      portd,1
	     call     DELAY
	     bcf      portd,1
	     movf     r8,w        
	     call     sevseg
	     movwf    portb
	     bsf      portd,2
	     call     DELAY
	     bcf      portd,2
	     movf     r7,w        
	     call     sevseg
	     movwf    portb
	     bsf      portd,3
	     call     DELAY
	     bcf      portd,3

goto     main
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.