SNIU028D February 2016 – September 2020 UCD3138 , UCD3138064 , UCD3138064A , UCD3138128 , UCD3138A , UCD3138A64
The following example code demonstrates how to configure (initialize) the ADC-12 for a start stop software initiated operation.
Requirement: Configure the ADC-12 in single weep mode and then periodically read the conversion results and start a new measurement sequence immediately after the read of results.
The function init_ADC_polled(); configures the number of conversions in a single sweep sequence, sets the order of channels within the sequence and configures the post conversion averaging setting for each measurement.
void init_ADC_polled(void)
{
AdcRegs.ADCCTRL.bit.MAX_CONV = 6; // A total of 7 conversions
AdcRegs.ADCCTRL.bit.SINGLE_SWEEP = 1;
AdcRegs.ADCCTRL.bit.ADC_EN = 1;
AdcRegs.ADCSEQSEL0.bit.SEQ0 = 3; // Vout-AD03
AdcRegs.ADCSEQSEL0.bit.SEQ1 = 2; // Iout-AD02
AdcRegs.ADCSEQSEL0.bit.SEQ2 = 4; // Ipri-AD04
AdcRegs.ADCSEQSEL0.bit.SEQ3 = 5; // Ishare-AD05
AdcRegs.ADCSEQSEL1.bit.SEQ4 = 6; // Vin voltage sensing
AdcRegs.ADCSEQSEL1.bit.SEQ5 = 7; // Copper-Temp-AD12
AdcRegs.ADCSEQSEL1.bit.SEQ6 = 8; // Ext-Temp-AD08
AdcRegs.ADCAVGCTRL.bit.AVG0_CONFIG = 2; // Average 16 samples
AdcRegs.ADCAVGCTRL.bit.AVG0_EN = 1; // Module0 averaging enabled
AdcRegs.ADCAVGCTRL.bit.AVG1_CONFIG = 2; // Average 16 samples
AdcRegs.ADCAVGCTRL.bit.AVG1_EN = 1; // Module1 averaging enabled
AdcRegs.ADCAVGCTRL.bit.AVG2_CONFIG = 2; // Average 16 samples
AdcRegs.ADCAVGCTRL.bit.AVG2_EN = 1; // Module2 averaging enabled
AdcRegs.ADCAVGCTRL.bit.AVG3_CONFIG = 2; // Average 16 samples
AdcRegs.ADCAVGCTRL.bit.AVG3_EN = 1; // Module3 averaging enabled
AdcRegs.ADCAVGCTRL.bit.AVG4_CONFIG = 2; // Average 16 samples
AdcRegs.ADCAVGCTRL.bit.AVG4_EN = 1; // Module4 averaging enabled
AdcRegs.ADCAVGCTRL.bit.AVG5_CONFIG = 2; // Average 16 samples
AdcRegs.ADCAVGCTRL.bit.AVG5_EN = 1; // Module5 averaging enabled
}
The function poll_adc(); is reading the measurement results after it verifies that the conversion of last sequence is concluded. The function also starts a new sequence of measurements that can be read when the function is called next time.
The raw (not averaged) result of AD07 is being read here.
void poll_adc(void)
{
if(AdcRegs.ADCSTAT.bit.ADC_INT == 1) //If the conversion isn’t complete
{
adc_values.Vout = AdcRegs.ADCAVGRESULT[0].bit.RESULT; // AD01
adc_values.isec = AdcRegs.ADCAVGRESULT[1].bit.RESULT; // AD02
adc_values.i_pri = AdcRegs.ADCAVGRESULT[2].bit.RESULT; // AD04
adc_values.ishare = AdcRegs.ADCAVGRESULT[3].bit.RESULT; // AD05
adc_values.Vin = AdcRegs.ADCAVGRESULT[4].bit.RESULT; // AD05
adc_values.current_temp = AdcRegs.ADCAVGRESULT[5].bit.RESULT; // AD12
adc_values.temp_sense = AdcRegs.ADCRESULT[6].bit.RESULT; // AD07
}
AdcRegs.ADCCTRL.bit.SW_START = 1; // trigger anew measurement sequence
}
Now poll_adc(); can be called from any part of the code that is periodically executed. In this example the function call is placed in the standard interrupt which is invoked by a timer interrupt in 100µs intervals. Since conversion of a sequence of 6 measurements will require less than 30µs, the verification of end of conversion is not really required.
#pragma INTERRUPT(standard_interrupt,IRQ)
void standard_interrupt(void)
{
poll_adc();
}