PDA

View Full Version : Need help with 16f819 10 bit ADC



bannrikae
- 23rd September 2005, 10:18
Hi

I'm new to forums and pic basic so please forgive if Iv'e missed somthing obvious.

I have built a battery charger, and I am using the 8 bit ADC on the 16f819 to measure the batt voltage and detect when it is fully charged.
The resolution for 8 bits was not quite good enough for my requirements so I want to use it in the 10 bit format.

My circuit and program works when using 8 bit, but when I try to use 10 bit, nothing happens.

I have a simple test circuit with LED's on the output of port b, and I can get the LED's to light, as a bar graph, in approx 10 mV increments.

Using the same test circuit and the code below for 10 bits, the leds are coming on as shown

RB0- on = 0V
RB1- on = 1.26V
RB2- on = 2.52V
RB3- on = 3.78V
RB4- dosn't come on.

Iv'e looked at the data sheet and lots of examples of 10 bit code, but can't see anything wrong with my program. I was hopeing for a resolution of about 5 mV.
I would be very gratefull if sombody could help.

Test program


DEFINE adc_bits 10
DEFINE adc_clock 3
DEFINE adc_sampleus 50

adval VAR WORD

portb=%00000000
trisb=%00000000

trisa=%11111111

adcon1=%100000010

loop:
ADCIN 2, adval

ledtst1:
IF adval > 0 THEN tst2
portb = %00000001
GOTO cont

tst2:
IF adval > 1 THEN tst3
portb = %00000011
GOTO cont

tst3:
IF adval > 2 THEN tst4
portb = %00000111
GOTO cont

tst4:
IF adval > 3 THEN tst5
portb = %00001111
GOTO cont

tst5:
portb = %00011111

cont:
PAUSE 100
GOTO loop
END

Ingvar
- 23rd September 2005, 13:58
You have nine bits in this line.
adcon1=%100000010
That's probably the reason for the strange behaviour.

bannrikae
- 23rd September 2005, 15:18
Sorry! thats a typing error in my post, actual code is correct.
It seems so simple I can't understand why it dosen't work.

modifyit
- 23rd September 2005, 16:38
Don't you need to change your command to ADCIN 4, adval to get AN4?

I personally have been using Melanies direct method reading analog values that can be seen in this thread

http://www.picbasic.co.uk/forum/showthread.php?t=369&highlight=adcon0

Its pretty straight forward and I can understand what is going on.

Ingvar
- 23rd September 2005, 17:06
Defines should be in uppercase.

DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50

bannrikae
- 23rd September 2005, 18:18
Thanks modifyit, I'll try this when I get back to work on Monday and let you know how I get on.

Ingvar, I didn't think these were case sensitive, it worked OK lower case for 8 bit ADC but I will also try this on Monday.

Thanks both.

Ingvar
- 26th September 2005, 08:42
Your defines didn't do anything atall since they were the default values.

bannrikae
- 26th September 2005, 08:49
So if they are default values Then shouldn't the prog work?
The rest of the code works when set up for 8 Bits, can't see what else is wrong, confused!.

BG

Ingvar
- 26th September 2005, 08:53
It worked before you changed it to 10 bits. 8 bits is default. That's why it worked even though you wrote the defines in lowercase.

bannrikae
- 26th September 2005, 13:46
OK ingvar, see what your saying, will try as soon as I get the time,
(work gets in the way of projects)

Thanks

BG

bannrikae
- 26th September 2005, 15:20
ingvar, just changed defaults to upper case, program now works fine.
Just knew it had to be somthing stupid I had done.
Thanks for your help and patience.

BG