# PIC16F1939 - problem with adc and pot

#### hunterage2000

Joined May 2, 2010
487
Hi I'm trying to test the adc on a picf161939. I have followed the adc procedure but its not working. I am measuring an analog value from a pot and sending the adresh and adresl to port c and d with led's on it.

At the outputs of portc and d I am getting a random sequence.

I assume the code is wrong but can't see where I've gone wrong.
Code:
#include <pic16f1939.h>
#include <xc.h>
#define _XTAL_FREQ 500000 //1ms delay

void main()
{
PORTC = 0; PORTD = 0;
LATC = 0; LATD = 0;
TRISAbits.TRISA0 = 1;
ANSELAbits.ANSA0 = 1;
TRISC = 0x00;
TRISD = 0x00;
ANSELD = 0x00;

while(1)
{
__delay_ms(10);
PORTC = ADRESH;    //Also tried LATC and LATD
__delay_ms(250);
}

}

#### Picbuster

Joined Dec 2, 2013
1,026

#### ErnieM

Joined Apr 24, 2011
8,125
Line 25 looks fine to me, it loops right there until the conversion is complete. Line 22 would better be done once at initialization but probably not the cause either.

You are setting AN0 on pin 2 as the source, is your pot connected there?

#### hunterage2000

Joined May 2, 2010
487
Yeah the pot is on pin 2. The leds have the following sequence and don't change.

C5, C1, D7, D6, D5, D3, D2, D1 and D0 = ON

C7, C6, C4, C3, C2, C1, C0 and D4 = OFF

As the adc is right justified I would of thought C5 would be OFF but then again nothing is changing when adjusting the pot.

I tried using debug to watch PORTC, PORTD, ADRESL and ADRESH after the conversion was finished but it read all 0's and not sure how to check the analog value on RA0.

Any ideas what is happening?

#### hunterage2000

Joined May 2, 2010
487
Spent ages looking into what is stopping this working and can't see what is wrong.

Running debug all the bits are correct until I get to ADREL & H which show 0.

Anyone know how I can see if RA0 is reading the pot value?

#### JohnInTX

Joined Jun 26, 2012
4,379
Are you sure the chip is running? You don't show any CONFIG #pragmas in the code.

#### hunterage2000

Joined May 2, 2010
487
yeah its running. I'm using the internal oscillator.

#### nerdegutta

Joined Dec 15, 2009
2,676
yeah its running. I'm using the internal oscillator.
How do you know?

It is good practice to include the config settings.

#### ErnieM

Joined Apr 24, 2011
8,125
How do you know?

It is good practice to include the config settings.
I always do a quick test program to insure by measurement my clock is what I think it should be. It is easy if you have a scope but still doable if you do something like blink a led every second and time it by any means necessary.

#### takao21203

Joined Apr 28, 2012
3,700
You can only write to the LATX on extended midrange (I think).

Many things can go wrong, need to process one by one. Blinking LED?

Or can you manipulate the ports manually? If not the PIC isn't running.

Check the datasheet for the IO ports and peripherals they can be reassigned on some new pics. Check a/d reference setting and supply.

#### nerdegutta

Joined Dec 15, 2009
2,676
I always do a quick test program to insure by measurement my clock is what I think it should be. It is easy if you have a scope but still doable if you do something like blink a led every second and time it by any means necessary.
That's usually what I do first. A while-loop blinking an LED.