pic24 ADC converter making me crazier  - Page 1
Forum section: electronics / general

author: snarkysparky
author_rank: Frequent Contributor
timestamp: February 18, 2017, 11:42:14 pm
content: Values coming back mid scale. Changing slightly as input goes from zero to Vdd.setupCode:TRISA = 0xffbf; // 1111 1111 1011 1111 // RA6 output (pwm)TRISB = 0x0f7f; // 0000 1111 0111 1111 // RB12,13,14,15,7 outputANSA = 0x000d; // 0000 0000 0000 1101 RA0 RA2 RA3 analog inputsANSB = 0x0003; // 0000 0000 0000 0011 RB0 RB1 analog inputsAD1CON2 = 0x0800; // 0000 1000 0000 0000AD1CON1 = 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 OKAD1CON3 - 0x0000;bit_set(AD1CON1,15); // turn on converter// Callunsigned int read_AD(unsigned int channel){bit_clear(AD1CON1,15); // turn off converterAD1CHS = 14; // channel & 0x000f;bit_set(AD1CON1,15); // turn on converterAD_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

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: February 19, 2017, 12:02:28 am
content: 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.

author: snarkysparky
author_rank: Frequent Contributor
timestamp: February 19, 2017, 12:21:36 am
content: Well I fixed it. It was a pic24F32KA301The 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 bit1 = CLKO output signal is active on the OSCO pin; primary oscillator must be disabled or configuredfor the External Clock mode (EC) for the CLKO to be active (POSCMD<1:0> = 11 or 00)0 = CLKO output is disabledSo 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.ThanksI will delete this topic in a little while

author: Buriedcode
author_rank: Super Contributor
timestamp: February 19, 2017, 02:08:14 am
content: 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!

author: MrBungle
author_rank: Unknown
timestamp: February 19, 2017, 06:54:10 am
content: ^ exactly.This is way more usefull than threads like "Have you seen a room messier?", and yet stuff like that sticks aroundKeep it, it will eventually help someone.

author: AndyC_772
author_rank: Super Contributor
timestamp: February 19, 2017, 08:48:26 am
content: 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.