PIC16F1939 - problem with adc and pot

Thread Starter

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;
    ADCON1bits.ADFM = 1;
    ANSELD = 0x00;
    ADCON1bits.ADPREF = 0b00;
    ADCON1bits.ADNREF = 0;
    ADCON0bits.CHS = 0b00000;
    ADCON1bits.ADCS = 0b000;
       
   while(1)
    {
       ADCON0bits.ADON = 1;
        __delay_ms(10);
        ADCON0bits.GO_nDONE = 1;
        while(ADCON0bits.GO_nDONE==1);
        PORTC = ADRESH;    //Also tried LATC and LATD
        PORTD = ADRESL;
        __delay_ms(250);
    }
   
}
 

ErnieM

Joined Apr 24, 2011
8,377
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?
 

Thread Starter

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?
 

Thread Starter

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?
 

ErnieM

Joined Apr 24, 2011
8,377
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,702
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,684
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.
 
Top