class 2 analog pins on pic32 code example

Example: pic32 adc polling code

#include "adc.h"#include "app.h"   void init_ADC(){ADC0CFG = DEVADC0;ADC1CFG = DEVADC1;ADC2CFG = DEVADC2;ADC3CFG = DEVADC3;ADC4CFG = DEVADC4;ADC7CFG = DEVADC7; /* Configure ADCCON1 */ADCCON1 = 0; // No ADCCON1 features are enabled including: Stop-in-Idle, turbo,// CVD mode, Fractional mode and scan trigger source.ADCCON1bits.SELRES = 3; // ADC7 resolution is 12 bitsADCCON1bits.STRGSRC = 1; // Select scan trigger, GLSWTRG global software edge trigger /* Configure ADCCON2 */ADCCON2bits.SAMC = 5; // ADC7 sampling time = 5 * TAD7ADCCON2bits.ADCDIV = 1; // ADC7 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 = 3; // Select input clock sourceADCCON3bits.CONCLKDIV = 1; // Control clock frequency is half of input clockADCCON3bits.VREFSEL = 0; // Select AVDD and AVSS as reference source ADC0TIMEbits.ADCDIV = 1;ADC0TIMEbits.SAMC = 5;ADC0TIMEbits.SELRES = 3; //ADCTRGMODEbits.SH0ALT = 0;  /* Select ADC input mode */ADCIMCON1bits.SIGN12 = 0; // unsigned data formatADCIMCON1bits.DIFF12= 0; // Single ended mode   /* Configure ADCGIRQENx */ADCGIRQEN1 = 0; // No interrupts are used.ADCGIRQEN2 = 0; /* Configure ADCCSSx */ADCCSS1 = 0; // Clear all bitsADCCSS2 = 0; // clear all bitsADCCSS1bits.CSS12 = 1; // AN12 (Class set for scan /* Configure ADCCMPCONx */ADCCMPCON1 = 0; // No digital comparators are used. Setting the ADCCMPCONxADCCMPCON2 = 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;   // AN12 Always uses scan trigger soruce  /* Early interrupt */ADCEIEN1 = 0; // No early interruptADCEIEN2 = 0;   /* Turn the ADC on */ADCCON1bits.ON = 1;   /* Wait for voltage reference to be stable */while(!ADCCON2bits.BGVRRDY); // Wait until the reference voltage is readywhile(ADCCON2bits.REFFLT); // Wait if there is a fault with the reference voltage   // Enable clock to the module.ADCCON3bits.DIGEN7 = 1; // Enable ADC7ADCANCONbits.ANEN7 = 1; // Enable clock, ADC7while( !ADCANCONbits.WKRDY7 ); // Wait until ADC7 is ready ADCCON3bits.GSWTRG = 1; /* Enable the ADC module */}   int read_adc(){ADCCON3bits.GSWTRG = 1; // Start software triggerwhile(ADCDSTAT1bits.ARDY12 == 0); // Wait until the measurement runreturn ADCDATA12; // Return with measured data}

Tags:

Misc Example