SNIU028D February 2016 – September 2020 UCD3138 , UCD3138064 , UCD3138064A , UCD3138128 , UCD3138A , UCD3138A64
#define ADCCTRL_SINGLE_SWEEP 8
#define ADCCTRL_MAXCONV_SHIFT 4
#define ADCCTRL_ADC_ENA 1
#define ADCCOMPEN_COMP0_EN 1
#define ADCCOMPEN_COMP3_EN 8
#define ADCCOMPEN_COMP5_EN 0x20
int Temporary;
AdcRegs.ADCCTRL.all = ADCCTRL_SINGLE_SWEEP + (7 << ADCCTRL_MAXCONV_SHIFT) + ADCCTRL_ADC_ENA;
AdcRegs.ADCAVGCTRL.bit.AVG2_EN = 1;
// Means: Averaging on the result of the third measurement in the sequence is enabled
AdcRegs.ADCAVGCTRL.bit.AVG2_CONFIG = 2;
// Means: The third sequence result will undergo moving average of 16 samples
AdcRegs.ADCSEQSEL0.bit.SEQ0 = 15;
//Means that channel 15 (the very last input channel) is selected as the very first measurement in the measurement sequence.
AdcRegs.ADCSEQSEL1.all = 5 + (6 << 5) + (7 << 10)
//Means, the channels 5, 6 and 7 are set to be the fifth, sixth and the seventh (SEQ4 to SEQ6) respectively in the measurement sequence.
AdcRegs.ADCCTRL.bit.SW_START = 1;
//Means; the ADC is instructed to start a new conversion sequence
AdcRegs.ADCCTRL.bit.ADC_INT_EN = 1; // Means: enable interrupt at local ADC level
CimRegs.REQMASK.bit.REQMASK_ADC_CONV =1; // Means: Enable ADC interrupt at CPU level.
if (AdcRegs.ADCSTAT.bit.ADC_INT == 1) // Means; if ADC measurement completed.
Temporary = AdcRegs.ADCCTRL.all; // Means :read and clear completion flags
Temporary = AdcRegs.ADCRESULT[3].all; // Read the fourth raw ( non averaged) result register
Temporary = AdcRegs.ADCAVGRESULT[0].all; // Read the first averaged result register
if (AdcRegs.ADCCOMPRESULT.bit.DCOMP0_LO_RAW == 1)
//Means: if the lower limit is exceeded on the first comparator
AdcRegs.ADCCOMPEN.all = ADCCOMPEN_COMP0_EN + ADCCOMPEN_COMP3_EN + ADCCOMPEN_COMP5_EN;
//Means : Enable the first, the fourth and the sixth digital comparators.
AdcRegs.ADCCOMPEN.bit.COMP5_EN = 1;
// Enable the sixth digital comparison mechanism
AdcRegs.ADCCOMPEN.bit.COMP4_DATA_SEL = 1;
//Means: The fifth comparator is configured to compare the average data from ADC12 and not the raw data
AdcRegs.ADCCOMPEN.bit.COMP3_UP_INT_EN = 1;
// Means: Enable Digital comparator 3 interrupt when exceeding upper limit at Local level
AdcRegs.ADCCOMPLIM[5].bit.LOWER_LIMIT = 0x0FA;
// Sets the lower limit to be compared to the sixth result register
//( Note: bit does not necessarily mean one bit, but a bits field of any length
CimRegs.REQMASK.bit. REQMASK_DIGI_COMP =1;
// Means: Enable Digital comparator interrupt at CPU(CIM) level.