Register Based Working Example
Set up AtoD
//----- SETUP AtoD -----
AD1PCFG = 0xFFFC; //AN1:0 analog, reset digital
AD1CHSbits.CH0SA = 0; //Channel 0 positive input is AN0
AD1CON1bits.SSRC = 7; //Internal counter ends sampling and starts conversion (auto-convert)
AD1CON1bits.ASAM = 1; //Sampling begins when SAMP bit is set
//The PIC32 can sample up to 1000ksps, but we don't need that.
//We want to sample at 44.1kHz to give a Nyquist frequency of 22.05kHz. So 1 sample every 22.6757uS
//
//Set the ADC Conversion Clock (TAD)
//TAD is the basic timng value of the AtoD .
//TAD must be >= 83.33nS
//A conversion takes 12 x TAD + the number of sampling TAD clocks specified by SAMC (i.e. the fastest possible conversion takes 13 x TAD)
// System clock of 80MHz and PBDIV = 2
// TPB = (1 / 80MHz) * 2 = 25ns (this is the time of the peripheral bus)
//
// TAD = (TPB * 2) * (ADCS + 1) * 2
// TAD = 25ns * (# + 1)
//
AD1CON3bits.ADCS = 11; //TAD=11=600ns
//Auto-Sample Time
//Set number of TAD clock cycles between start of acquisition and the start of conversion.
//SAMC=1=1TAD to SAMC=31=31TAD
//Add this to the 12 TAD clock cycles a conversion takes and you have the total AtoD conversion time per input sample
AD1CON3bits.SAMC = 25; //SAMC=25=22.2uS
IPC6bits.AD1IP = 6; //AtoD1 Interrupt Priority
IPC6bits.AD1IS = 3; //AtoD1 Subpriority
IFS1CLR = 2; //Clear ADC interrupt flag
IEC1bits.AD1IE = 1; //ADC interrupt enable
AD1CON1bits.ON = 1; //ADC enable
Interrupt Per Read
void __ISR(_ADC_VECTOR, ipl6) ADCInterruptHandler()
{
IFS1CLR = 2; //Clear irq flag
samples[sampleCounter++] = ADC1BUF0 - 512;
}
Library Based Working Example
Setting Up The AtoD
//----- SETUP THE A TO D PINS -----
//Pins have allready been set to digital in. This next line not needed as OpenADC10 will do the job
//mPORTBSetPinsAnalogIn(BIT_2 | BIT_3 | BIT_4 | BIT_8); //Pins have allready been set to digital in, set these to analog (this updates AD1PCFG)
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_10), //AD1CON2 register (SCAN_ON so that all enabled analog inputs are automatically scanned_
(ADC_SAMPLE_TIME_31 | ADC_CONV_CLK_PB | ADC_CONV_CLK_32Tcy), //AD1CON3 register (Sets sampling time - 31:32 makes it slow as possible)
(ENABLE_AN4_ANA | ENABLE_AN3_ANA | ENABLE_AN2_ANA), //AD1PCFG register << SET ANALOG PINS
(0) //AD1CSSL register
);
EnableADC10();
Reading the values
void read_atod (void)
{
WORD w_temp;
w_temp = ReadADC10(2);
w_temp = ReadADC10(3);
w_temp = ReadADC10(4);
}
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.