Problem in ADC PIC16f877a

Thread Starter

relativ

Joined Oct 7, 2012
5
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 :)

Rich (BB code):
INCLUDE "P16F877A.INC"
__CONFIG  0X3F7A

BUFFER0 EQU 0X20 
BUFFER1 EQU 0X21 
BUFFER2 EQU 0X22  

ORG  0X00
GOTO MAIN
  
MAIN:
    BSF     STATUS, RP0
    CLRF    TRISB
    CLRF    TRISC
    CLRF    TRISD   
    BCF     STATUS, RP0
    CLRF PORTD 
    CLRF PORTB 
    CLRF PORTC 

    MOVLW 0X89
    MOVWF ADCON0
    BSF     STATUS, RP0
    MOVLW 0X80
    MOVWF ADCON1
AGAIN:
    CALL  TACQ 
    BSF ADCON0,GO   ;GO/DONE--> BIT 2 OF ADCON0 SOC SIGNAL
A:
	BTFSC ADCON0,2 ; WAIT FOR END OF CONVERSION SIGNAL
	GOTO A    
	MOVLW ADRESL
	MOVWF PORTB
	MOVLW ADRESH
	MOVWF PORTC  
        CLRF PORTD  
        CALL ONESEC
        MOVLW 0XFF
        MOVWF PORTD
        CALL ONESEC
        GOTO AGAIN



; DELAY 0.1964 SEC * 5 = 1 SEC at 4 MHz
ONESEC:
   MOVLW 0X05
   MOVWF BUFFER2
LOOP3: 
   MOVLW 0XFF
   MOVWF BUFFER1
LOOP2:
   MOVLW 0XFF
   MOVWF BUFFER0
LOOP1:
   DECFSZ BUFFER0,F
   GOTO LOOP1
   NOP
   DECFSZ BUFFER1,F
   GOTO LOOP2
   DECFSZ BUFFER2,F
   GOTO LOOP3
RETURN

TACQ: ; DELAY 20USEC
   MOVLW 0X07       
   MOVWF BUFFER2     
LOOP4: 
   DECFSZ BUFFER2,F  
   GOTO LOOP4    
   RETURN        


END
Btw, I am toggling portD to see what is going on ,but this piece of code unreachable :)
 

t06afre

Joined May 11, 2009
5,934
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
Rich (BB code):
AGAIN:
    CALL  TACQ 
    BSF ADCON0,GO   ;GO/DONE--> BIT 2 OF ADCON0 SOC SIGNAL
A:
BTFSC ADCON0,2 ; WAIT FOR END OF CONVERSION SIGNAL
GOTO A
 

Thread Starter

relativ

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

Rich (BB code):
AGAIN:
    BCF     STATUS, RP0
    CALL  TACQ 
    BSF ADCON0,GO   ;GO/DONE--> BIT 2 OF ADCON0 SOC SIGNAL
A:
BTFSC ADCON0,2 ; WAIT FOR END OF CONVERSION SIGNAL
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 :)
 

Thread Starter

relativ

Joined Oct 7, 2012
5
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 :)

Rich (BB code):
    BSF     STATUS, RP0  ; Now I am in bank 1 
    MOVLW ADRESL               ;deal with ADRESL that is in bank 1
    BCF     STATUS, RP0        ; switch back to bank 0 to deal with PORTB,PORTC,PORTD and ADRESH
    MOVWF PORTB
    MOVLW ADRESH
    MOVWF PORTC  
    CLRF PORTD  
    CALL ONESEC
    MOVLW 0XFF
    MOVWF PORTD
    CALL ONESEC
    GOTO AGAIN
I wonder if I am coding write or It should be something other than that :)
 

Thread Starter

relativ

Joined Oct 7, 2012
5
This is the final code if anyone needs it :)
Rich (BB code):
INCLUDE "P16F877A.INC"
__CONFIG  0X3F7A

; AD Conversion Using Polling Method.
ORG  0X00
GOTO MAIN
  
MAIN:
    BSF  STATUS, RP0 ;select bank 1
    CLRF TRISB       ;set PORTB as output
    CLRF TRISC       ;set PORTC as output
    BCF  STATUS, RP0 ;select bank 0	 ;PORTB = 0      
    CLRF PORTB       ;PORTB = 0
    CLRF PORTC       ;PORTC = 0
    MOVLW 0X89       ;Tad=64Tosc ,channel 1, turn ADC module on
    MOVWF ADCON0    
    BSF   STATUS, RP0;select bank 1
    MOVLW 0X80       ;Right justified o/p ,All pins are analog,Vref+=5 volts,Vref-= 0 volts
    MOVWF ADCON1   
AGAIN:
    BCF   STATUS, RP0;select bank 0
    CALL  TACQ       ;Wait for acquisition time about 20 uSec
    BSF ADCON0,GO    ;G0=1, Start of conversion signal
A:
	BTFSC ADCON0,2   ;WAIT FOR END OF CONVERSION SIGNAL
	GOTO A           ;If conversion is not finished , test again.
    BSF  STATUS, RP0 ;select bank 1  
    MOVF ADRESL,w    ;Move 1st 8 bits of the result to WREG
    BCF  STATUS, RP0 ;select bank 0 
	MOVWF PORTB      ;Output the 1st 8 bits to PORTB
	MOVF ADRESH,w    ;Move last 2 bits of the result to WREG
	MOVWF PORTC      ;Output the last 2 bits to PORTC
    GOTO AGAIN       ;keep doing sampling process.



TACQ: ; DELAY 20USEC
   MOVLW 0X07       
   MOVWF BUFFER2     
LOOP1: 
   DECFSZ BUFFER2,F  
   GOTO LOOP4    
   RETURN        


END
 
Top