ADC in PIC 16F1503

Status
Not open for further replies.
Code:
    ADCON0 |= Channel<<2;      // Clear Channel selection bits
This doesn't clear the current channel select bits before ORing in the new ones.
In your simple single channel example it works, but it won't as soon as you try to change the channel.

Code:
   return (int)(ADRESH<<8)+ ADRESL ;   // Return 10 bit ADC value
Due to C's integer promotion rules, the cast isn't required but if you do cast it should be 'unsigned int'.
It's also usually more efficient to OR instead of adding.

Code:
    ANSELA   = 0x00;
    ANSELC   = 0x00;        // Clear Pin selection bits
    if(Channel < 4)  
        ANSELA  |= Channel;       // Select Channel
    else
        ANSELC  |= Channel;
These are just flat out wrong. ANSELx are bit-oriented, so something like 'ANSELC |= 7' doesn't set bit 7,
it sets bits 2-0. It also clears any existing ANSELx setting, changing pins back to digital mode.

Code:
    CM1CON0  = 0x07;         // Shut off the Comparator, so that pins are available for ADC
    FVRCON  = 0x00;         // Shut off the Voltage Reference for Comparator
The first one doesn't do what it says it does, and the second is not needed.
Anyway, the comparators don't prevent the ADC from working (unless you enable the comparator output to an ADC pin).

Code:
    TRISCbits.TRISC3 = 1;
That sets RC3 as input, but the OP wants to "use 2 adc pins from 2 different ports (RA4, RC3)",
so RA4 wasn't addressed. It would work since all pins are inputs by default, but I like to explicitly set them,
especially if you're trying to show someone what to do.

The other things are minor (mixing signed and unsigned ints, placing config settings after includes, etc).

EDIT: and personally, I'd move the setting of the pin mode + TRIS out of the InitADC function.
 
Last edited:
This doesn't clear the current channel select bits before ORing in the new ones.
In your simple single channel example it works, but it won't as soon as you try to change the channel.
No but the init function does that.. As the init function was selecting a single channel, it would have worked.
Due to C's integer promotion rules, the cast isn't required but if you do cast it should be 'unsigned int'.
It's also usually more efficient to OR instead of adding.
Not always... The ADRESH is an 8 bit register.. XC actually have got a 16bit variable to grab the read, but we don't use it.
The unsigned/ signed in't nessasary as the result is ALWAYS smaller than10 bit.
These are just flat out wrong. ANSELx are bit-oriented, so something like 'ANSELC |= 7' doesn't set bit 7,
it sets bits 2-0. It also clears any existing ANSELx setting, changing pins back to digital mode.
I f**ked up But it worked as the number 7 still set AN7.. go figure..
Correct!!
I already admitted I was fooled by RA3 not AN3..
The other things are minor (mixing signed and unsigned ints, placing config settings after includes, etc).
Already in the text doc when I got it..

EDIT: and personally, I'd move the setting of the pin mode + TRIS out of the InitADC function.
Yep there is a shed lot more I'd do too, But as everyone else gave up, I tried....
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…