PIC16F1939 - problem with adc and pot

Discussion in 'Embedded Systems and Microcontrollers' started by hunterage2000, May 2, 2016.

  1. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    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 (Text):
    1. #include <pic16f1939.h>
    2. #include <xc.h>
    3. #define _XTAL_FREQ 500000 //1ms delay
    4.  
    5. void main()
    6. {
    7.     PORTC = 0; PORTD = 0;
    8.     LATC = 0; LATD = 0;
    9.     TRISAbits.TRISA0 = 1;
    10.     ANSELAbits.ANSA0 = 1;
    11.     TRISC = 0x00;
    12.     TRISD = 0x00;
    13.     ADCON1bits.ADFM = 1;
    14.     ANSELD = 0x00;
    15.     ADCON1bits.ADPREF = 0b00;
    16.     ADCON1bits.ADNREF = 0;
    17.     ADCON0bits.CHS = 0b00000;
    18.     ADCON1bits.ADCS = 0b000;
    19.        
    20.    while(1)
    21.     {
    22.        ADCON0bits.ADON = 1;
    23.         __delay_ms(10);
    24.         ADCON0bits.GO_nDONE = 1;
    25.         while(ADCON0bits.GO_nDONE==1);
    26.         PORTC = ADRESH;    //Also tried LATC and LATD
    27.         PORTD = ADRESL;
    28.         __delay_ms(250);
    29.     }
    30.    
    31. }
    32.    
    33.    
    34.    
    35.  
    36.  
    37.  
    38.  
    39.  
    40.  
    41.  
    42.  
    43.  
    44.    
    45.  
     
  2. Picbuster

    Member

    Dec 2, 2013
    376
    50
    look at your instruction line 25 >>> ADCON0bits.GO_nDONE = 1;
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,607
    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?
     
  4. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    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?
     
  5. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    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?
     
  6. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    Are you sure the chip is running? You don't show any CONFIG #pragmas in the code.
     
    ErnieM likes this.
  7. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    yeah its running. I'm using the internal oscillator.
     
  8. nerdegutta

    Moderator

    Dec 15, 2009
    2,517
    785
    How do you know?

    It is good practice to include the config settings.
     
    ErnieM and arunpradh like this.
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,607
    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.
     
  10. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    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.
     
  11. nerdegutta

    Moderator

    Dec 15, 2009
    2,517
    785
    That's usually what I do first. A while-loop blinking an LED.
     
Loading...