Problem in ADC PIC16f877a

Discussion in 'Embedded Systems and Microcontrollers' started by relativ, Oct 7, 2012.

  1. relativ

    Thread Starter New Member

    Oct 7, 2012
    5
    0
    Simple I would like to test ADC operation with PIC 16f877a in assembly language
    I am taking the ADC reading out at port C and port B.
    The problem is : the DONE bit cannot be clear so that I can take my readings.It does not stop polling in Proteus simulation.

    here is my code and thanks in advance :)

    Code ( (Unknown Language)):
    1.  
    2.  
    3. INCLUDE "P16F877A.INC"
    4. __CONFIG  0X3F7A
    5.  
    6. BUFFER0 EQU 0X20
    7. BUFFER1 EQU 0X21
    8. BUFFER2 EQU 0X22  
    9.  
    10. ORG  0X00
    11. GOTO MAIN
    12.  
    13. MAIN:
    14.     BSF     STATUS, RP0
    15.     CLRF    TRISB
    16.     CLRF    TRISC
    17.     CLRF    TRISD  
    18.     BCF     STATUS, RP0
    19.     CLRF PORTD
    20.     CLRF PORTB
    21.     CLRF PORTC
    22.  
    23.     MOVLW 0X89
    24.     MOVWF ADCON0
    25.     BSF     STATUS, RP0
    26.     MOVLW 0X80
    27.     MOVWF ADCON1
    28. AGAIN:
    29.     CALL  TACQ
    30.     BSF ADCON0,GO   ;GO/DONE--> BIT 2 OF ADCON0 SOC SIGNAL
    31. A:
    32.     BTFSC ADCON0,2 ; WAIT FOR END OF CONVERSION SIGNAL
    33.     GOTO A    
    34.     MOVLW ADRESL
    35.     MOVWF PORTB
    36.     MOVLW ADRESH
    37.     MOVWF PORTC  
    38.         CLRF PORTD  
    39.         CALL ONESEC
    40.         MOVLW 0XFF
    41.         MOVWF PORTD
    42.         CALL ONESEC
    43.         GOTO AGAIN
    44.  
    45.  
    46.  
    47. ; DELAY 0.1964 SEC * 5 = 1 SEC at 4 MHz
    48. ONESEC:
    49.    MOVLW 0X05
    50.    MOVWF BUFFER2
    51. LOOP3:
    52.    MOVLW 0XFF
    53.    MOVWF BUFFER1
    54. LOOP2:
    55.    MOVLW 0XFF
    56.    MOVWF BUFFER0
    57. LOOP1:
    58.    DECFSZ BUFFER0,F
    59.    GOTO LOOP1
    60.    NOP
    61.    DECFSZ BUFFER1,F
    62.    GOTO LOOP2
    63.    DECFSZ BUFFER2,F
    64.    GOTO LOOP3
    65. RETURN
    66.  
    67. TACQ: ; DELAY 20USEC
    68.    MOVLW 0X07      
    69.    MOVWF BUFFER2    
    70. LOOP4:
    71.    DECFSZ BUFFER2,F  
    72.    GOTO LOOP4    
    73.    RETURN        
    74.  
    75.  
    76. END
    77.  
    78.  
    Btw, I am toggling portD to see what is going on ,but this piece of code unreachable :)
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I think this is due to that ADCON0 is in bank0 and ADCON1 is in bank1. And from your code it looks like you are in bank1 then you do this
    Code ( (Unknown Language)):
    1. AGAIN:
    2.     CALL  TACQ
    3.     BSF ADCON0,GO   ;GO/DONE--> BIT 2 OF ADCON0 SOC SIGNAL
    4. A:
    5. BTFSC ADCON0,2 ; WAIT FOR END OF CONVERSION SIGNAL
    6. GOTO A
     
    relativ likes this.
  3. relativ

    Thread Starter New Member

    Oct 7, 2012
    5
    0
    @t06afre:
    Thank u very much :)
    It works partially ,I forgot to select bank 0 before dealing with ADCON0.
    I edited it to become:

    Code ( (Unknown Language)):
    1. AGAIN:
    2.     BCF     STATUS, RP0
    3.     CALL  TACQ
    4.     BSF ADCON0,GO   ;GO/DONE--> BIT 2 OF ADCON0 SOC SIGNAL
    5. A:
    6. BTFSC ADCON0,2 ; WAIT FOR END OF CONVERSION SIGNAL
    7. GOTO A
    Now the Toggling part is working fine and I think that the sampling process is OK

    please check the way I use to output the data.
    The output stuck on portc=0x1E and portb=0x9E which is un logic :)
    It does not change with the analog input change :)
     
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I think you have some more bank trouble here. ADRESH is in bank0 and ADRESL is in bank1. Kind of awkward if you ask me ;)
     
    relativ likes this.
  5. relativ

    Thread Starter New Member

    Oct 7, 2012
    5
    0
    I am afraid that just after posting my second reply, I have tried to check banks in ADRESL and ADRESH :D
    I did it like that ,but nothing changes :)

    Code ( (Unknown Language)):
    1.  
    2.     BSF     STATUS, RP0  ; Now I am in bank 1
    3.     MOVLW ADRESL               ;deal with ADRESL that is in bank 1
    4.     BCF     STATUS, RP0        ; switch back to bank 0 to deal with PORTB,PORTC,PORTD and ADRESH
    5.     MOVWF PORTB
    6.     MOVLW ADRESH
    7.     MOVWF PORTC  
    8.     CLRF PORTD  
    9.     CALL ONESEC
    10.     MOVLW 0XFF
    11.     MOVWF PORTD
    12.     CALL ONESEC
    13.     GOTO AGAIN
    I wonder if I am coding write or It should be something other than that :)
     
  6. nigelwright7557

    Senior Member

    May 10, 2008
    487
    71
    You are using movlw adresh when it should be movf adresh,w
    You are doing the same to other registers in a few places.
     
    relativ likes this.
  7. relativ

    Thread Starter New Member

    Oct 7, 2012
    5
    0
    @nigelwright7557 :
    It works :D
    u r marvelous :D
     
  8. relativ

    Thread Starter New Member

    Oct 7, 2012
    5
    0
    This is the final code if anyone needs it :)
    Code ( (Unknown Language)):
    1.  
    2. INCLUDE "P16F877A.INC"
    3. __CONFIG  0X3F7A
    4.  
    5. ; AD Conversion Using Polling Method.
    6. ORG  0X00
    7. GOTO MAIN
    8.  
    9. MAIN:
    10.     BSF  STATUS, RP0 ;select bank 1
    11.     CLRF TRISB       ;set PORTB as output
    12.     CLRF TRISC       ;set PORTC as output
    13.     BCF  STATUS, RP0 ;select bank 0  ;PORTB = 0      
    14.     CLRF PORTB       ;PORTB = 0
    15.     CLRF PORTC       ;PORTC = 0
    16.     MOVLW 0X89       ;Tad=64Tosc ,channel 1, turn ADC module on
    17.     MOVWF ADCON0    
    18.     BSF   STATUS, RP0;select bank 1
    19.     MOVLW 0X80       ;Right justified o/p ,All pins are analog,Vref+=5 volts,Vref-= 0 volts
    20.     MOVWF ADCON1  
    21. AGAIN:
    22.     BCF   STATUS, RP0;select bank 0
    23.     CALL  TACQ       ;Wait for acquisition time about 20 uSec
    24.     BSF ADCON0,GO    ;G0=1, Start of conversion signal
    25. A:
    26.     BTFSC ADCON0,2   ;WAIT FOR END OF CONVERSION SIGNAL
    27.     GOTO A           ;If conversion is not finished , test again.
    28.     BSF  STATUS, RP0 ;select bank 1  
    29.     MOVF ADRESL,w    ;Move 1st 8 bits of the result to WREG
    30.     BCF  STATUS, RP0 ;select bank 0
    31.     MOVWF PORTB      ;Output the 1st 8 bits to PORTB
    32.     MOVF ADRESH,w    ;Move last 2 bits of the result to WREG
    33.     MOVWF PORTC      ;Output the last 2 bits to PORTC
    34.     GOTO AGAIN       ;keep doing sampling process.
    35.  
    36.  
    37.  
    38. TACQ: ; DELAY 20USEC
    39.    MOVLW 0X07      
    40.    MOVWF BUFFER2    
    41. LOOP1:
    42.    DECFSZ BUFFER2,F  
    43.    GOTO LOOP4    
    44.    RETURN        
    45.  
    46.  
    47. END
     
Loading...