Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

AD output question.

Status
Not open for further replies.

RobertD

New Member
Hi folks, I finally have the AD working and I am measuring the output from 0-5v. I was under the opinion that the output would be linear, that 0v give 00000000, and that 5v gives 1111111111. Which it does. It's the intermediary measures that bug me. I was thinking that 2.5v would be approximately half way down the scale which would be around 506 which is the mid point between 0 and 1024. But I'm getting:

102/0.1v
700/0.5v
918/1.2v
984/2.0v
996/2.24v
1020/4.25v
1023/5.0v

This is not a linear progression.
 
Have you inadvertently left the pin as output (and high). If you have, the digital output fights your analogue signal and you get the non linear result you are seeing. Ensure that the pin you are using has a 1 in the corresponding tris register.

Edit, one more thing is the acquisition time. You need a delay between selecting the analogue pin and setting the go bit.

Mike.
 
Last edited:
So it is supposed to be linear, good, I have to find out what goes on.
I have this set up:

Code:
	movlw	B'00000010'	;all RA ports all outputs except A/D (RA1) input
	movwf	ANSEL
	movwf	TRISA

And this in the AD:

	banksel	TRISA 		;bank 1
	movlw 	b'11000000'     ;right justify result
	movwf 	ADCON1 
	banksel	PORTA		;bank 0
	movlw 	B'01001001'
	movwf 	ADCON0		; channel 0, FOSC/8, enable A/D
	
loop1
	movlw 	0x06
	movwf 	count		;initialize count
loop
	decfsz 	count,f 
	goto 	loop		;until finished
	bsf 	ADCON0,GO	;initiate conversion
test
	btfsc 	ADCON0,GO	;conversion done?
  	goto 	test
	movf	ADRESH,W
 
Last edited:
Hi folks, I finally have the AD working and I am measuring the output from 0-5v. I was under the opinion that the output would be linear, that 0v give 00000000, and that 5v gives 11,11111111. Which it does. It's the intermediary measures that bug me. I was thinking that 2.5v would be approximately half way down the scale which would be around 506 which is the mid point between 0 and 1024. But I'm getting:

102/0.1v
700/0.5v
918/1.2v
984/2.0v
996/2.24v
1020/4.25v
1023/5.0v

This is not a linear progression.
hi,
Whats the value of the ADC input source impedance.?

Looking at the code fragment you posted, I dont see any reference to ADRESL.?
 
Last edited:
I'm using firefly 16F88 for input, so it's impedance should be OK, I measured it at 2k. I have the osc at 72, maybe that's too fast. I combined ADRESH-L into one figure, since both add up to 1024, I gave the total value of both H and L registers. I've also increased the count waiting time to 12 cycles, I just got home so I haven't had time to fickle with it yet.

I changed the OSC to 42, and the AD still reads too high.
PortA1 reads 0.
 
Last edited:
I use left justify and eliminate the lower register anyway, that's why I only need ADRESH. When the AD is working properly that is. As it is, I can't use it since everything over 1v is over 900. I have to graduate voltages in 128 segments linearly. That's the normal breakdown.
 
In the third post of this thread, I posted code that reads the ADC on the firefly. The posted code works correctly, can you try it?

If your still having problems, can you post all your code. I can then try it on my Firefly and see if I get the same results.

Mike.
 
Seems the adresh goes from 0 to 255, with a rapid growth in the early stage like mine. I think my problem was to use right justify instead of left justify, like you do. With left justify I can use only ADRESH to calculate the values I need.

ADRESH Readings for your program are:

114-.3v
188-.6v
219-1v
240-1.75v
243-1.9v
250-2.5v
252-3.25v
255-5v

I think I would get the same results if I use left justify and use only ADRESH. That is still not linear since at 2.5v a linear equ would be 125, not 250, as it is now.
 
Last edited:
This is the AD code I'm using...

Code:
LIST	p=16F88
	include "P16F88.inc"
	__config _CONFIG1, _WDT_OFF & _INTRC_IO & _MCLR_ON & _LVP_OFF
	ERRORLEVEL 0, -302

	cblock	0x20
	count
	endc

	banksel	TRISA 		;bank 1
	movlw	0x70		;8MHz clock
	movwf	OSCCON
	movlw 	B'00000010'
	movwf 	TRISA		;all outputs except RA1
	movwf	ANSEL
	movlw 	b'11000000'
	movwf 	ADCON1 
	banksel	PORTA		;bank 0
	movlw 	B'01001001'
	movwf 	ADCON0		; channel 0, FOSC/8, enable A/D
	bsf	STATUS,RP0
	movlw	0x70
	movwf	OSCCON
	bcf	STATUS,RP0
beep	movlw 	0x06
	movwf 	count		;initialize count
loop	decfsz 	count,F 
	goto 	loop		;until finished
	bsf 	ADCON0,GO	;initiate conversion
test	btfsc 	ADCON0,GO	;conversion done?
	goto 	test		;not finished
	goto	beep
	end
 
Robert,

I just tried your code on my Firefly and I get the full range of readings. Zero at one extreme and 0x3ff at the other. I did notice one thing that may be a problem in your code, FOSC/8 is only recommended for 5MHz and below, you are running at 8MHz and so you should use FOSC/16. The only other explanation I can think of is a solder bridge on your board, can you try using VR1 instead?

For completeness, the way I tested it was to put a breakpoint on the line labeled beep and pressed F9 (run) to get a new reading.

Mike.
 
Thanks Pommie, what about the intermediary values...? I do get full range also, but not linear. Is your full range linear? I jump to the high range quite quickly. Can you run a couple of intermediary values like 1v, 2v, 3v, e.g. and give me the adresh right justified, as your code has it. I would appreciate that. And I will change the fosc to 16.
 
If I put the pot at mid position I get a reading of 0x1f0 which is about right. If I measure the voltages, I have Vdd=4.4V (USB powered) and the slider is 2.15V. 4.4/2.15*1023 = 499 = 0x1f3, so pretty accurate. Are you taking the ADC reading whilst your meter is connected as your meter could be loading the pin.

Mike.
 
You're up bright and early... :) (it's 4am here...)

If I put the slider midrange, I read about 1v. What is Vdd? Supply? When I slide the pot, I don't have a continuous range of voltage, I go from 2.3 directly to 5 at the 3/4 position, there is no intermediary values, very hard to get values between 2v and 4.9v from the pot. You divide the Vdd with the input, why do you do that? 0x1F0, is that both H and L or just H?
OK I get it, divide for average calculated value vs reading.
 
Last edited:
At 2.45v, the results ADHL I get, are 11111000-00000000 which is 0xF8-0x00, or in digital, 248 out of 255 for H, and 0 for L. For a linear ratio I should be looking at 127 or so.

I'll try with a different chip.
 
Last edited:
It sounds like you have a broken pot or it could be a logarithmic rather than linear pot. Did you check VR2? FYI, it's 6:30 in the evening here so I've been up a long time.:)

Mike.
 
It sounds like you have a broken pot or it could be a logarithmic rather than linear pot. Did you check VR2? FYI, it's 6:30 in the evening here so I've been up a long time.:)

Mike.


Good Morning Mike.
I think you had the Vdd/Vinp transposed.? ie [Vin/Vdd] * 1023 ==

Hi Robert,
What voltages do you measure on the wiper/slider of the pot using a meter as you slowly vary the pot.?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top