PDA

View Full Version : Adcon register with P18F4550



lerameur
- 24th February 2011, 07:05
Hi ,

I am trying to use A/D conversion, but my code compiles but does not even get to main loop when I add ADCON1 AND ADCON2 registers, these are valid registers in this pic, anybody knows why?

ADCON0 = %11000001 ' AN0, Configure and turn on A/D Module
'ADCON1 = %10001001 ' Analog input
'ADCON2 = %10000001 ' Right justified results for 10 bits,Fosc/8

K

mackrackit
- 24th February 2011, 09:09
ADCON0 and ADCON1 bits 6 and 7 should both be 0.

Are you using ADCIN or working the registers? Show your code.

lerameur
- 24th February 2011, 14:17
Ok Thanks , I wiil try it, but the datasheet sheet says: bit 7-6 Unimplemented: Read as ‘0’ .. shouldn't there be no difference as what is there?
ok work now, but gives me an 8 bit result instead of 10 bit

K

lerameur
- 24th February 2011, 14:33
Ok Thanks , I wiil try it, but the datasheet sheet says: bit 7-6 Unimplemented: Read as ‘0’ .. shouldn't there be no difference as what is there?
ok work now, but gives me an 8 bit result instead of 10 bit

I also have these defines in the prog:
Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uSec

K

mackrackit
- 24th February 2011, 19:12
Adcon2.7=1

lerameur
- 24th February 2011, 20:13
ya thats what i had before I commented out. See I have 2.23v on pin ANA5 and it reading 93 on the LCD


K

lerameur
- 24th February 2011, 22:35
I change a bit of the program , but i am still getting 8 bit results ( max 255)


Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uSec

ADCON0 = %00000001 ' AN0, Configure and turn on A/D Module
ADCON1 = %00001001 ' Analog input
ADCON2.7 =1 ' Right justified results for 10 bits,Fosc/8

Check_voltage_Low:
ADCON0 = 00010001 'Start Conversion
ADCON2.7 =1
ADCIN 4, voltage_check 'analog pin 4 get the 10 bit result Check the battery voltage on pin PORTA.5

lcdout $FE,1, "Battery voltage"
lcdout $FE,$C0, "TempC: ", dec voltage_check
pause 1500
Return

mackrackit
- 24th February 2011, 22:54
I never did like ADCIN. Take a look at this thread. It may help.
http://www.picbasic.co.uk/forum/showthread.php?t=11947&p=79694#post79694

lerameur
- 24th February 2011, 23:34
Thanks Dave, its working now with your method.
still wandering why the button command do not work.

lerameur
- 25th February 2011, 00:24
Hi,

I added a 1uF capacitor to the analog pin, hoping it would stabilize the voltage. I am measuring a 12v battery, using a 80Mohm and a 16Mohm divider. I am reading about 2.2v on the pin, but the output oscillates between 425, 429, 433. which seems to be a lot. any other things I can do to stabilize the output ? (hardware) I am also thinking of taking an average of the last three inputs..

K

lerameur
- 25th February 2011, 00:54
Hi , I added these lines: but it still fluctuates a lot:
then I output the ChAN_AVG


GOSUB READ_AD
CHAN4_2 = CHAN4_1
CHAN4_1 = CHAN4

CHAN4.HighByte = ADRESH 'PLACES THE HIGH AND LOW BYTE
CHAN4.LowByte = ADRESL 'INTO VAR CHAN0

CHAN_AVG = (CHAN4_2 + CHAN4_1 + CHAN4 ) /3

RETURN

mackrackit
- 25th February 2011, 01:00
On similar apps I use a 22uF cap from the ADC pin to VSS.

How stable is VDD?

lerameur
- 25th February 2011, 01:51
I use a 7805 with a 1uF cap and a 47uF cap on the adc pin
K

MikeWinston
- 26th February 2011, 00:56
I believe your problem is this "80Mohm and a 16Mohm divider".

See section 21.1 in 18F4550 spec sheet.
It says the maximum recommended impedance for analog
sources is 2.5 kΩ.