//Functions:
//ADC_Init() is used to configure A/D to scan and convert 2 input channels
//per interrupt. The A/D is set up for a total sampling rate of 8KHz
//or 4KHz per channel. The internal counter in the A/D is used to provide
//Acquisition time delay. The input pins being scanned are AN2 and AN3.
//AN2 and AN3 are connected to the Temperature Sensor and the Potentiometer
//on the dsPICDEM2 board.
void ADC_Init(void)
{
//ADCON1 Register
ADCON1=0;
//Set up A/D for Automatic Sampling, Auto-Convert
//All other bits to their default state
//ADCON1bits.SIMSAM = 1; // Simultaneous Sampling enabled
ADCON1bits.SSRC = 7; // Internal counter ends sampling and starts conversion (auto convert)
ADCON1bits.ASAM = 1; // 1 = Sampling begins immediately after last conversion completes
ADCON1bits.FORM=0; // 00 = Integer (DOUT = 0000 dddd dddd dddd)
//ADCON2 Register
//Set up A/D for interrupting after 2 samples get filled in the buffer
//Also, enable Channel scanning
//All other bits to their default state
ADCON2bits.SMPI = 6; // interuupt after taking seven samples
ADCON2bits.VCFG = 3; // External VREF+ pin and External VREF- pin -----> +3.3 vref+
ADCON2bits.CSCNA = 1; // scan input selection for ch0 + s/h input for mux A input multiplexer setting bit
//ADCON3 Register
//Set up Acquisition time (Tacq) for 31 Tad periods
//where, Tad = A/D conversion clock time.
//Set up Tad period to be 20.5 Tcy (Tcy = instruction cycle time)
//Given that each conversion takes 14*Tad (=Tconv) periods,
//Total Sample Time = Acquisition Time + Conversion Time
// = (31 + 14)*Tad = 45*Tad periods
// = 45 * 20.5 * Tcy = 922.5*Tcy periods
//At 7.3728 MIPS, Tcy = 135 ns = Instruction Cycle Time
//So Tsamp = Tacq + Tconv = 45*Tad(in this example)= 125.1 microseconds
//So Fsamp = Sampling Rate ~= 8 KHz
//All other bits to their default state
ADCON3bits.SAMC = 31; //Set up Acquisition time (Tacq) for 31 Tad periods
ADCON3bits.ADRC=0; // clock derived fron system clock
ADCON3bits.ADCS = 20; // A/D Conversion Clock Select bits
//ADCHS Register
//When Channel scanning is enabled (ADCON2bits.CSCNA=1)
//AND Alternate mux sampling is disabled (ADCON2bits.ALTS=0)
//then ADCHS is a "don't care"
ADCHS = 0x0000;
//ADCSSL Register
//Scan channels AN6,7,8,9,11,12,13 fast part of scanning sequence
//ADCSSL = 0x000C;
ADCSSLbits.CSSL6 = 1; // Select AN6 for input scanning
ADCSSLbits.CSSL7 = 1; // Select AN7 for input scanning
ADCSSLbits.CSSL8 = 1; // Select AN8 for input scanning
ADCSSLbits.CSSL9 = 1; // Select AN9 for input scanning
ADCSSLbits.CSSL11 = 1; // Select AN11 for input scanning
ADCSSLbits.CSSL12 = 1; // Select AN12 for input scanning
ADCSSLbits.CSSL13 = 1; // Select AN13 for input scanning
//ADPCFG Register
//Set up channels AN2, AN3 ... as analog inputs and leave rest as digital
//Recall that we configured all A/D pins as digital when code execution
//entered main() out of reset
//ADPCFGbits.PCFG2 = 0;
ADPCFGbits.PCFG6 = 0;
ADPCFGbits.PCFG7 = 0;
ADPCFGbits.PCFG8 = 0;
ADPCFGbits.PCFG9 = 0;
ADPCFGbits.PCFG11 = 0;
ADPCFGbits.PCFG12 = 0;
ADPCFGbits.PCFG13 = 0;
IFS0bits.ADIF = 0; //Clear the A/D interrupt flag bit
//Set the A/D interrupt enable bit
//IEC0bits.ADIE = 1;
//Turn on the A/D converter
//This is typically done after configuring other registers
// IEC0bits.ADIE = 1; // interrupt enable
// ADCON1bits.ADON = 1;
}