38.3.3 Selecting the Format of the ADC Result
The data in the ADC Result register can be read in any of the four supported data formats. The user can select from unsigned integer, signed integer, unsigned fractional or signed fractional. Integer data is right-justified and fractional data is left-justified.
- The integer or fractional data format selection is specified globally for all analog inputs using the Fractional Data Output Format bit, FRACT (ADCCON1[23]).
- The signed or unsigned data format selection can be independently specified for each individual analog input using the SIGNx bits in the ADCIMCONx registers
.
The following table provides how a result is formatted.
FRACT | SIGNx | Description | 32-bit Output Data Format | |||
---|---|---|---|---|---|---|
|
| Unsigned integer |
|
|
|
|
|
|
|
| |||
|
| Signed integer |
|
|
|
|
|
|
|
| |||
|
| Fractional |
|
|
|
|
|
|
|
| |||
|
| Signed fractional |
|
|
|
|
|
|
|
|
The following code is an example for ADC Class 2 configuration and fractional format.
int main(int argc, char** argv) {
int result[3];
/* Configure ADCCON1 */
ADCCON1bits.FRACT = 1; // use Fractional output format ADCCON1bits.SELRES = 3; // ADC resolution is 12 bits ADCCON1bits.STRGSRC = 0; // No scan trigger.
/* Configure ADCCON2 */
ADCCON2bits.SAMC = 5; // ADC sampling time = 5 * TAD7
ADCCON2bits.ADCDIV = 1; // ADC clock freq is half of control clock = TAD7
/* Initialize warm up time register */ ADCANCON = 0;
ADCANCONbits.WKUPCLKCNT = 5; // Wakeup exponent = 32 * TADx
/* Clock setting */ ADCCON3 = 0;
ADCCON3bits.ADCSEL = 0; // Select input clock source
ADCCON3bits.CONCLKDIV = 1; // Control clock frequency is half of input clock
ADCCON3bits.VREFSEL = 0; // Select AVDD and AVSS as reference source
/* No selection for dedicated ADC modules, no presync trigger, not sync sampling */ ADCTRGMODEbits = 0;
/* Select ADC input mode */
ADCIMCON1bits.SIGN7 = 0; // unsigned data format ADCIMCON1bits.DIFF7 = 0; // Single ended mode ADCIMCON1bits.SIGN8 = 0; // unsigned data format ADCIMCON1bits.DIFF8 = 0; // Single ended mode ADCIMCON1bits.SIGN9 = 0; // unsigned data format ADCIMCON1bits.DIFF9 = 0; // Single ended mode
/* Configure ADCGIRQENx */
ADCGIRQEN1 = 0; // No interrupts are used
ADCGIRQEN2 = 0;
/* Configure ADCCSSx */
ADCCSS1 = 0; // No scanning is used
ADCCSS2 = 0;
/* Configure ADCCMPCONx */
ADCCMPCON1 = 0; // No digital comparators are used. Setting the ADCCMPCONx ADCCMPCON2 = 0; // register to '0' ensures that the comparator is disabled. ADCCMPCON3 = 0; // Other registers are “don't care”.
ADCCMPCON4 = 0;
ADCCMPCON5 = 0; ADCCMPCON6 = 0;
/* Configure ADCFLTRx */
ADCFLTR1 = 0; // No oversampling filters are used. ADCFLTR2 = 0;
ADCFLTR3 = 0; ADCFLTR4 = 0; ADCFLTR5 = 0; ADCFLTR6 = 0;
/* Set up the trigger sources */
ADCTRGSNSbits.LVL7 = 0; // Edge trigger ADCTRGSNSbits.LVL8 = 0; // Edge trigger ADCTRGSNSbits.LVL9 = 0; // Edge trigger
ADC1TRG2bits.TRGSRC7 = 1; // Set AN7 to trigger from software
ADC2TRG3bits.TRGSRC8 = 1; // Set AN8 to trigger from software
ADC2TRG3bits.TRGSRC9 = 1; // Set AN9 to trigger from software
/* Early interrupt */
ADCEIEN1 = 0; // No early interrupt
ADCEIEN2 = 0;
/* Turn the ADC on */ ADCCON1bits.ON = 1;
/* Wait for voltage reference to be stable */
while(!ADCCON2bits.BGVRRDY); // Wait until the reference voltage is ready while(ADCCON2bits.REFFLT); // Wait if there is a fault with the reference voltage
/* Enable clock to analog circuit */
ADCANCONbits.ANEN7 = 1; // Enable the clock to analog bias
/* Wait for ADC to be ready */
while(!ADCANCONbits.WKRDY7); // Wait until ADC7 is ready
/* Enable the ADC module */ ADCCON3bits.DIGEN7 = 1; // Enable ADC7
while (1) {
/* Trigger a conversion */ ADCCON3bits.GSWTRG = 1;
/* Wait the conversions to complete */
while (ADCDSTAT1bits.ARDY7 == 0);
/* fetch the result */
result[0] = ADCDATA7;
while (ADCDSTAT1bits.ARDY8 == 0);
/* fetch the result */
result[1] = ADCDATA8;
while (ADCDSTAT1bits.ARDY9 == 0);
/* fetch the result */
result[2] = ADCDATA9;
/*
* Process results here
*
* Note 1: Loop time determines the sampling time since all inputs are Class 2.
* If the loop time happens is small and the next trigger happens before the
* completion of set sample time, the conversion will happen only after the
* sample time has elapsed.
*
* Note 2: Results are in fractional format
*
*/
}
return (1);
}