Automatically Read A Single Analog Input
CloseADC10();// ensure the ADC is off before setting the configuration
OpenADC10(
(ADC_MODULE_ON | ADC_IDLE_STOP | ADC_FORMAT_INTG16 | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON | ADC_SAMP_ON), //AD1CON1 register
(ADC_VREF_AVDD_AVSS | ADC_SCAN_OFF | ADC_ALT_INPUT_OFF | ADC_SAMPLES_PER_INT_10), //AD1CON2 register
(ADC_SAMPLE_TIME_4 | ADC_CONV_CLK_PB | ADC_CONV_CLK_3Tcy), //AD1CON3 register (SET THE SAMPLE TIME ETC)
(ENABLE_AN0_ANA), //AD1PCFG register (SET THE ANALOG PINS)
(0) //AD1CSSL register (SELECTS INPUTS TO BE SEQUENTIALLY SCANNED) (0 when ADC_SCAN_OFF)
);
EnableADC10();
//Read the voltage from ADC1BUF0
If this doesn't work (seems to sometimes not) then simply use the Multiple Analog Inputs example below with just the one input defined in the last 2 arguments
Automatically Read Multiple Analog Inputs
CloseADC10();// ensure the ADC is off before setting the configuration
OpenADC10(
(ADC_MODULE_ON | ADC_IDLE_STOP | ADC_FORMAT_INTG16 | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON | ADC_SAMP_ON), //AD1CON1 register
(ADC_VREF_AVDD_AVSS | ADC_SCAN_ON | ADC_ALT_INPUT_OFF | ADC_SAMPLES_PER_INT_4), //AD1CON2 register (ENSURE ADC_SAMPLES_PER_INT_ = NO OF INPUTS BEING SAMPLES)
(ADC_SAMPLE_TIME_4 | ADC_CONV_CLK_PB | ADC_CONV_CLK_3Tcy), //AD1CON3 register (SET THE SAMPLE TIME ETC)
(ENABLE_AN3_ANA | ENABLE_AN2_ANA | ENABLE_AN1_ANA | ENABLE_AN0_ANA), //AD1PCFG register (SET THE ANALOG PINS)
(~(SKIP_SCAN_AN3 | SKIP_SCAN_AN2 | SKIP_SCAN_AN1 | SKIP_SCAN_AN0)) //AD1CSSL register (SELECTS INPUTS TO BE SEQUENTIALLY SCANNED) (looks like OpenADC10() inverts what we give it, hence the ~)
);
EnableADC10();
//Read the voltage from the corresponding ADC1BUF0, ADC1BUF1 etc (Starts filling from ADC1BUF0 regardless of actual AN# pin numbers being sampled)
Converting Result To Voltage
//10 bit AtoD (0 - 1023)
//[Vsource] - 2K7 - [PIC] - 2K2 - [0V] Potential divider
//1024 = 3.3V = 7.35Vsource
//Result = (MaxReadingSource / MaxReading) * AtoD value
UINT16 atod_vin_x100 = (UINT16)(((double)735 / 1024) * ADC1BUF0);
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.