A/D conversion using the pic 16f877

Thread Starter

jwebbo

Joined Jul 23, 2010
3
Hi everyone,i am seeking your help.
I am trying to program the PIC 16f877 microcontroller to accept multiple analog inputs and give readouts on the 7-seg display. the conversion is of a 10 bit resolution. my problems are that i cannot seem to get the pic to correctly display the adresh and adresl registers simultaneously. when i display the adresl anone,it is ok because i have tested it using the leds.CAN someone please help me with the codes to display both registers. in addition,i do not know how to measure multiple inputs.
lastly,when i send my codes to the lcd, nothing is displayed. can anyone help me with the correct assembly codes for this pic (16f877).
IF ANYONE HAS CODES I WOULD BE VERY GRATEFUL IF YOU POST THEM.

THANKS!
 

hgmjr

Joined Jan 28, 2005
9,029
Greetings jwebbo,

I have pasted the code that you sent me via email. I am not in the habit of assisting members outside of the pervue of the forum. It defeats the purpose of the forum which is to share information among the membership at large. It also robs you of insights into your problem supplied from other members.

Here is my codes. note that this code is working on my 16f877 pic.the codes only displays the values of the adresl register(0-255) although it is a 10 bit conversion. i do not know to how to combine the two registers.please help with this.

also the adc is only responding to inputs to RAO/ANO.i have tried changing the adcon0 register's bits 5-3 to convert various channels inputs but it has not worked.

how may i display multiple inputs on the sev seg display one after the other

Rich (BB code):
;project to do analog to digital conversion
;done by jwebbo
;2010-07-13
 
;declearations
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 26
r2     equ 21
r3     equ 22
r4     equ 23
r5     equ 24
r6     equ 25
r7     equ 27
r8     equ 28
r9     equ 29
r10    equ 30
r11    equ 31
r12    equ 32
r13    equ 33
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
 
init       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 adresh   ;clear a/d result high register.
          clrf adresl
 
     bsf   status,5
     movlw b'11111111'
     movwf trisa
     movlw b'00000000'
     movwf trisb
     movlw b'00000000'
     movwf trisd
     movlw b'00000000'
     movwf trise
     bcf   status,5
     movlw b'10000010'
     movwf adcon1
     movlw b'01000011'
     movwf adcon0
     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,1
     goto loop1
     retlw 00
 
DELAY      movlw   d'1'
          movwf   r11
LOOP3      movlw   d'1'
          movwf   r12
LOOP2      movlw   d'250'
          movwf   r13
LOOP1      NOP
          DECFSZ  r13
          GOTO    LOOP1
          DECFSZ  r12
          GOTO    LOOP2
          DECFSZ  r11
          GOTO    LOOP3
          RETLW   00
 
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
                clrf porte
 
       ;-----------Initiate a/d conversion----------------
main    bcf adcon0,2
       bsf adcon0,2    ;this instruction starts the a/d conversion progress.
       call delay1     ;waiting for the acquisition time
 
       ;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 portd,0
                  clrf r1
          clrf r2
          clrf r3
          clrf r4
          clrf r5
          clrf r6
 
   movf adresl,w
   movwf r1
con_1   movf r1,w
   movwf r2    ; will contain 10s and 1s
   incf r3,f
   movlw d'100'
   bcf status,0
   subwf r1,f
   btfss status,0
   goto next
   goto con_1
next
   decf r3,f
con_2
   movf r2,w
   movwf r4
   incf r5,f
   movlw d'10'
   bcf status,0
   subwf r2,f
   btfss status,0
   goto next_1
   goto con_2
 
 next_1
       decf r5,f
       goto display
 
display
 
                 movf r3,w
         call sevseg
         movwf portb
         bsf portd,1
         call DELAY
         bcf portd,1
                 movf r5,w
         call sevseg
         movwf portb
         bsf portd,2
         call DELAY
         bcf portd,2
 
             movf r4,w
         call sevseg
         movwf portb
         bsf portd,3
         call DELAY
         bcf portd,3
 
 
         movlw d'0'       ;these codes displays '0' on the leftmost digit on the sevseg display.
         call sevseg
         movwf portb
         bsf portd,0
         call DELAY
         bcf  portd,0
         goto main
         end


hgmjr
 

Thread Starter

jwebbo

Joined Jul 23, 2010
3
Attention to SGT WOOKIE or ANYONE ELSE.

thanks for your response in advance.
Here is my codes. note that this code is working on my 16f877 pic.the codes only displays the values of the adresl register(0-255) although it is a 10 bit conversion. i do not know to how to combine the two registers.please help with this.
also the adc is only responding to inputs to RAO/ANO.i have tried changing the adcon0 register's bits 5-3 to convert various channels inputs but it has not worked.

how may i display multiple inputs on the sev seg display one after the other.
Rich (BB code):
;project to do analog to digital conversion
;done by jwebbo
;2010-07-13
;declearations
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 26
r2 equ 21
r3 equ 22
r4 equ 23
r5 equ 24
r6 equ 25
r7 equ 27
r8 equ 28
r9 equ 29
r10 equ 30
r11 equ 31
r12 equ 32
r13 equ 33
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

init 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 adresh ;clear a/d result high register.
clrf adresl

bsf status,5
movlw b'11111111'
movwf trisa
movlw b'00000000'
movwf trisb
movlw b'00000000'
movwf trisd
movlw b'00000000'
movwf trise
bcf status,5
movlw b'10000010'
movwf adcon1
movlw b'01000011'
movwf adcon0
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,1
goto loop1
retlw 00

DELAY movlw d'1'
movwf r11
LOOP3 movlw d'1'
movwf r12
LOOP2 movlw d'250'
movwf r13
LOOP1 NOP
DECFSZ r13
GOTO LOOP1
DECFSZ r12
GOTO LOOP2
DECFSZ r11
GOTO LOOP3
RETLW 00
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
clrf porte

;-----------Initiate a/d conversion----------------
main bcf adcon0,2
bsf adcon0,2 ;this instruction starts the a/d conversion progress.
call delay1 ;waiting for the acquisition time

;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 portd,0
clrf r1
clrf r2
clrf r3
clrf r4
clrf r5
clrf r6

movf adresl,w
movwf r1
con_1 movf r1,w
movwf r2 ; will contain 10s and 1s
incf r3,f
movlw d'100'
bcf status,0
subwf r1,f
btfss status,0
goto next
goto con_1
next
decf r3,f
con_2
movf r2,w
movwf r4
incf r5,f
movlw d'10'
bcf status,0
subwf r2,f
btfss status,0
goto next_1
goto con_2

next_1
decf r5,f
goto display

display
movf r3,w
call sevseg
movwf portb
bsf portd,1
call DELAY
bcf portd,1
movf r5,w
call sevseg
movwf portb
bsf portd,2
call DELAY
bcf portd,2

movf r4,w
call sevseg
movwf portb
bsf portd,3
call DELAY
bcf portd,3


movlw d'0' ;these codes displays '0' on the leftmost digit on the sevseg display.
call sevseg
movwf portb
bsf portd,0
call DELAY
bcf portd,0
goto main
end
 

Thread Starter

jwebbo

Joined Jul 23, 2010
3
Hey everyone myself and others have managed to solve the problem. here is the code. ENJOY!!!


;project to do analog to digital conversion
;done by jwebbo
;2010-07-13
;declearations
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
init
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
LOOP1 NOP
DECFSZ r13
GOTO LOOP1
DECFSZ r12,f
GOTO LOOP2
DECFSZ r11
GOTO LOOP3
RETLW 00
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----------------
main
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
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
end
 
Top