Author Topic: pic24 ADC converter making me crazier  (Read 2360 times)

0 Members and 1 Guest are viewing this topic.

Offline snarkysparkyTopic starter

  • Frequent Contributor
  • **
  • Posts: 414
  • Country: us
pic24 ADC converter making me crazier
« on: February 18, 2017, 11:42:14 pm »
Values coming back mid scale.  Changing slightly as input goes from zero to Vdd.


setup
Code: [Select]
    TRISA = 0xffbf;   //  1111 1111 1011 1111     // RA6  output  (pwm)
    TRISB = 0x0f7f;   //  0000 1111 0111 1111     //  RB12,13,14,15,7  output   
    ANSA =  0x000d;   //  0000 0000 0000 1101     RA0 RA2 RA3  analog inputs
    ANSB =  0x0003;   //  0000 0000 0000 0011     RB0 RB1  analog inputs
    AD1CON2 = 0x0800; //  0000 1000 0000 0000
    AD1CON1 = 0x0400; //  0000 0100 0000 0000   conv starts with samp bit clear, 12 bit operation
                      //                        abs decimal result unsigned right justified
                      //    AD1CON2 ,AD1CON3  default 0 values OK
    AD1CON3 - 0x0000;
    bit_set(AD1CON1,15);  //  turn on converter       


// Call
unsigned int read_AD(unsigned int channel)
{

   bit_clear(AD1CON1,15);  //  turn off converter 
   AD1CHS = 14;  // channel & 0x000f;
   bit_set(AD1CON1,15);  //  turn on converter 
   AD_SampStart();            // #define AD_SampStart() bit_set(AD1CON1,1)
   delay_cycles(10);
   AD_ConvStart();            // #define AD_ConvStart() bit_clear(AD1CON1,1)
   delay_cycles(3);
   do{}while(AD_Converting);  // #define AD_Converting (!bit_test(AD1CON1,0))

   return ADC1BUF14;

}





Thanks
 

Online mikeselectricstuff

  • Super Contributor
  • ***
  • Posts: 13755
  • Country: gb
    • Mike's Electric Stuff
Re: pic24 ADC converter making me crazier
« Reply #1 on: February 19, 2017, 12:02:28 am »
Sounds like ADC is not sampling the input you want it to.
 
You don't say which PIC - might it be the case that the ANx pin numbers don't map 1:1 with the RAx numbers? - I think this is the case with some PICs.

Also, check you haven't accidentally pin-mapped something else onto the pin - mapping an input might override the analogue input, in the same way that pin-mapped functions can override TRIS settings.
 
Youtube channel:Taking wierd stuff apart. Very apart.
Mike's Electric Stuff: High voltage, vintage electronics etc.
Day Job: Mostly LEDs
 

Offline snarkysparkyTopic starter

  • Frequent Contributor
  • **
  • Posts: 414
  • Country: us
Re: pic24 ADC converter making me crazier
« Reply #2 on: February 19, 2017, 12:21:36 am »
Well I fixed it.  It was a pic24F32KA301

The pin I was trying to read was setup in the configuration as OSCOUT.

From the data sheet for the configuration bits.

bit 2 OSCIOFNC: CLKO Enable Configuration bit
1 = CLKO output signal is active on the OSCO pin; primary oscillator must be disabled or configured
for the External Clock mode (EC) for the CLKO to be active (POSCMD<1:0> = 11 or 00)
0 = CLKO output is disabled


So the lesson here is that CLKO overrides the TRIS register.  Pin was set to input but still had the clock on it.

It's converting OK now. 

Thanks

I will delete this topic in a little while
 

Offline Buriedcode

  • Super Contributor
  • ***
  • Posts: 1613
  • Country: gb
Re: pic24 ADC converter making me crazier
« Reply #3 on: February 19, 2017, 02:08:14 am »
Why delete?  The forum is full of little 'gotchas' like this, and not all of them have a solution/reason.  I say leave it, someone might be doing something similar, google, and find your post helpful!
 

Offline MrBungle

  • Supporter
  • ****
  • Posts: 75
  • Country: au
Re: pic24 ADC converter making me crazier
« Reply #4 on: February 19, 2017, 06:54:10 am »
^ exactly.
This is way more usefull than threads like "Have you seen a room messier?", and yet stuff like that sticks around  :-//
Keep it, it will eventually help someone.
« Last Edit: February 19, 2017, 06:56:22 am by MrBungle »
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4228
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: pic24 ADC converter making me crazier
« Reply #5 on: February 19, 2017, 08:48:26 am »
Agreed. I always try to remember to post the solutions to questions I've asked, for this very reason. Just because nobody else cares today doesn't mean nobody will care tomorrow.


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf