Hello, i am using ADC to monitor battery level using the window comparator, to detect the battery voltage between two threshold or below the lower threshold or higher. The results is not accurate, how can i adjust these thresholds to be for example to be 2.8V and 2V. the input is 3.6 V. Here is my code.
Thank you
#include <msp430.h>
#define High_Threshold 0x0ff // ~2V
#define Low_Threshold 0x00f // ~1V
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop WDT
// Configure GPIO
P1SEL1 |= BIT3; // Configure ADC P1.1/A1
P1SEL0 |= BIT3;
// Disable the GPIO power-on default high-impedance mode to activate
// previously configured port settings
PM5CTL0 &= ~LOCKLPM5;
// Clock System Setup
// Configure ADC12
// tsample = 16ADC12CLK cycles, tconvert = 14 ADC12CLK cycles
// software trigger for SOC, MODOSC, single ch-single conversion,
// tsample controlled by SHT0x settings
// Channel 1, reference = internal, enable window comparator
// Set thresholds for ADC12 interrupts
// Enable Interrupts
ADC12CTL0 = ADC12SHT0_3 | ADC12ON;
ADC12CTL1 = ADC12SHS_0 | ADC12SSEL_3 | ADC12CONSEQ_0 | ADC12SHP;
ADC12CTL2 = ADC12RES_2 ;
ADC12MCTL0 = ADC12INCH_3 | ADC12VRSEL_1 | ADC12WINC;
ADC12HI = High_Threshold;
ADC12LO = Low_Threshold;
ADC12IER2 = ADC12HIIE | ADC12LOIE | ADC12INIE;
// Configure internal reference
while(REFCTL0 & REFGENBUSY); // If ref generator busy, WAIT
REFCTL0 |= REFVSEL_2|REFON; // Select internal ref = 2.5V
REFCTL0 &= ~REFOUT ;
// Internal Reference ON
while(!(REFCTL0 & REFGENRDY)); // Wait for reference generator
// to settle
while(1)
{
ADC12CTL0 |= ADC12ENC | ADC12SC; // Enable & start conversion
}
}