Care to elaborate? Tested okay for me..Actually, there are a couple of problems with that code, esp. for the 16F1503
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Care to elaborate? Tested okay for me..Actually, there are a couple of problems with that code, esp. for the 16F1503
ADCON0 |= Channel<<2; // Clear Channel selection bits
return (int)(ADRESH<<8)+ ADRESL ; // Return 10 bit ADC value
ANSELA = 0x00;
ANSELC = 0x00; // Clear Pin selection bits
if(Channel < 4)
ANSELA |= Channel; // Select Channel
else
ANSELC |= Channel;
CM1CON0 = 0x07; // Shut off the Comparator, so that pins are available for ADC
FVRCON = 0x00; // Shut off the Voltage Reference for Comparator
TRISCbits.TRISC3 = 1;
No but the init function does that.. As the init function was selecting a single channel, it would have worked.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.
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.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.
I f**ked up But it worked as the number 7 still set AN7.. go figure..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 already admitted I was fooled by RA3 not AN3..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.
Already in the text doc when I got it..The other things are minor (mixing signed and unsigned ints, placing config settings after includes, etc).
Yep there is a shed lot more I'd do too, But as everyone else gave up, I tried....EDIT: and personally, I'd move the setting of the pin mode + TRIS out of the InitADC function.