PIC16F819 - A/D module problem

Status
Not open for further replies.

nickagian

Member level 4
Joined
Mar 19, 2009
Messages
71
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Location
Zurich, Switzerland
Activity points
2,001
Hi there! I'm using the PIC16F819 MCU for some home projects. I'm still in my first steps and the ADC keeps confusing me.

The problem is that after doing the conversions, the ADRESL register is always read as h'FF' no matter what input there is at the analog channel I'm using. I have checked -among others- to connect the input to the ground but the result remains the same, the ADRESL is always h'FF'

Does anyone have any idea of what's wrong?

I thought that it may be a problem of the Tad, because it seems to me as if the result is not properly shifted to the result registers. However, I'm using the RC clock for the ADC and as it is stated in the datasheet that by using the RC clock the required Tad timings are achieved.

Below is my ADC_RUN function, which I call for the conversion

Code:
ADC_RUN
	BSF		STATUS,	RP0			; Select Bank1
	;**BSF 	PIE1, ADIE 			; Enable A/D interrupts.
	BCF		PIE1, ADIE			; Disable A/D interrupts.
	CLRF	ADRESL

	BCF		STATUS, RP0 		; Select Bank0
	MOVLW	0xC1 				; RC Clock, A/D is on, Channel 0 is selected
	MOVWF 	ADCON0				; Sampling time started
	CLRF	ADRESH	
	
	CALL	Delay100
	
	;
	; Ensure that the required sampling time for the selected input
	; channel has elapsed. Then the conversion may be started.
	;

	BSF 	ADCON0, GO			; Start A/D Conversion

	; The ADIF bit will be set and the GO/DONE
	; bit is cleared upon completion of the
	; A/D Conversion.
 ADC_END_WAIT
	BTFSC	ADCON0, GO			; Check the GO/DONE bit
	GOTO	ADC_END_WAIT		; ADC conversion not completed yet
	BCF 	PIR1, ADIF 			; Clear A/D interrupt flag bit
	RETURN

Moreover, below is the part of the code that I use for the initialization of the ADC

Code:
; Port Initialisations
	BSF			STATUS,	RP0		; Select Bank1
	CLRF		LCD_TRIS		; configure PORTB as all outputs
	MOVLW		0x21
	MOVWF		TRISA			; Configure  PORTA : only RA0 is input
	
	; Configuration for the A/D inputs : 
	; RA0 is set as analog, RA1-RA4 set as digital,
	; VREF+ = VDD, VREF- = VSS
	; and LSB of result put in ADRESL
	MOVLW		0x8E
	MOVWF		ADCON1
	
	BCF			STATUS,	RP0		; Select Bank

The result is displayed in a LCD (the LCD and the functions displaying to it are working correctly)

Thanks for your help!
 

Hi,

You might find this little multicalc utility handy for selecting the adc parameters.
**broken link removed**

have only looked at your code, but you don't show the bit where you load ADRESL - you are selecting bank1 when you read it ?
 

wp100 said:
Hi,

You might find this little multicalc utility handy for selecting the adc parameters.
**broken link removed**

have only looked at your code, but you don't show the bit where you load ADRESL - you are selecting bank1 when you read it ?

Thanks for this utility. I think it might be very handy in other occasions.

And here is the code for loading ADRESL (and yes, I select bank1 before reading ADRESL. I did this error in the beginning but already corrected it)

Code:
BSF			STATUS, RP0 	; Select Bank1
	MOVF		ADRESL, W		; Move low byte of A/D result to W
	MOVWF		binary+1		; and then to binary+1
	BCF			STATUS, RP0 	; Select Bank0
	MOVF		ADRESH,	W		; Move high byte of A/D result to W
	MOVWF		binary			; and then to binary

Afterwards I take the binary and binary+1 registers, convert them to BCD and display them to the LCD.

Maybe I will check and validate the ADRESH-ADRESL with the output ports and not just the LCD, although I am pretty sure that the LCD is working correctly (I have tested it with other numbers and everything was ok)
 

Hi,

Its a while since I played around with the 16F banks, but when you write to the binary+1 file that is in bank1 and binary is in bank0 - is that intended ?

When you read them back are you banking them correctly ?
 

    nickagian

    Points: 2
    Helpful Answer Positive Rating
wp100 said:
Hi,

Its a while since I played around with the 16F banks, but when you write to the binary+1 file that is in bank1 and binary is in bank0 - is that intended ?

When you read them back are you banking them correctly ?

Oh, I am ashamed now... you have right... I change to bank1 to read ADRESL but then store the result to binary+1, without changing to the correct bank of it.

Thank so much wp100! I really appreciate your help.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…