SLAA475A October 2010 – March 2019 MSP430L092
The APINT or APFRACT counter that is used to save the result does not stop immediately after the comparator changes its state. Especially with a high-frequency A-Pool input clock, a significant error may occur. To avoid this counting error, it is possible to use the ADC in an overdrive-compensation mode, as shown in the following code.
#include "msp430l092.h"
unsigned char result[2]; // result array
unsigned char i = 0; // counting variable
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
APCNF = CMPON+DBON+CONVON+APREFON; // Enable comparator on +
// Enable DAC buffer +
// Enable conversion +
// Enable reference
APVDIV = A0DIV; // Set 500mV input range
APINT = 0x00; // clear ADC-DAC-REG
APIE |= EOCIE; // Enable end of conversion interrupt
_BIS_SR(GIE); // Switch on global interrupts
APCTL = APPSEL0+APPSEL2+OSEL+CBSTP+SBSTP;
// Set DAC buffer output to PSEL +
// Enable DAC buffer +
// Enable conversion +
// Enable reverence +
// Select output buffer +
// Enable Comparator based stop +
// Enable Saturation based stop +
APCTL |= RUNSTOP; // Start conversion
_BIS_SR(LPM0); // Go to LPM0
APINT = APINT + 0; // Add an offset for counting
APCTL = APPSEL0+APPSEL2+OSEL+CBSTP+SBSTP+SLOPE;
// Set DAC buffer output to PSEL +
// Enable DAC buffer +
// Enable conversion +
// Enable reverence +
// Select output buffer +
// Enable Comparator based stop +
// Enable Saturation based stop +
// Switch to falling slope +
APCTL |= RUNSTOP; // Start conversion
_BIS_SR(LPM0); // Go to LPM0
asm("nop");
while(1);
}
#pragma vector=APOOL_VECTOR // A-Pool interrupt service routine
__interrupt void APOOL_ISR(void)
{
switch(__even_in_range(APIV,8)) // Add offset to PC and delete flag
{
case 0: break;
case 2: result[i++] = APINT; // Save value in result array
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
break;
case 4: break;
case 6: break;
case 8: break;
default: break;
}
}
Both values are stored in the result array for later use. The user application must make sure that the start value of counting is in the correct range. The addition of a fixed number to the APINT or APFRACT register can create problems when the measured value is near the upper or lower counting border. The user application must avoid an overflow of the APINT or APFRACT register.