I'm modifying the msp432p401_adc14_5.c program to take 4 readings and then average the 4 readings. I'm not always getting the interrupt to trigger but if I enter the debugger and pause that seems to get the triggers to happen yet my breakpoints don't always work. I'm far from a great programmer so hopefully someone can take a look at what I've done and see where I'm not doing things correctly.
/* --COPYRIGHT--,BSD_EX * Copyright (c) 2013, Texas Instruments Incorporated * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of Texas Instruments Incorporated nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ******************************************************************************* * * MSP432 CODE EXAMPLE DISCLAIMER * * MSP432 code examples are self-contained low-level programs that typically * demonstrate a single peripheral function or device feature in a highly * concise manner. For this the code may rely on the device's power-on default * register values and settings such as the clock configuration and care must * be taken when combining code from several examples to avoid potential side * effects. Also see www.ti.com/.../mspdriverlib for an API functional * library & https://dev.ti.com/pinmux/ for a GUI approach to peripheral configuration. * * --/COPYRIGHT--*/ //****************************************************************************** //****************************************************************************** // MSP432P401 Demo - ADC14, Sample A1, AVcc Ref, Set P1.0 if A1 > 0.5*AVcc // // Description: A single sample is made on A1 with reference to AVcc. // Software sets ADC14SC to start sample and conversion - ADC14SC // automatically cleared at EOC. ADC14 internal oscillator times sample (16x) // and conversion. In Mainloop MSP432 waits in LPM0 to save power until ADC14 // conversion complete, ADC14_ISR will force exit from LPM0 in Mainloop on // reti. If A0 > 0.5*AVcc, P1.0 set, else reset. The full, correct handling of // and ADC14 interrupt is shown as well. // // // MSP432p401rpz // ----------------- // /|\| XIN|- // | | | // --|RST XOUT|- // | | // >---|P5.4/A1 P1.0|-->LED // // Dung Dang // Texas Instruments Inc. // November 2013 // Built with Code Composer Studio V6.0 //****************************************************************************** #include "msp432.h" #define NUM_SAMPLES 4 short samples[NUM_SAMPLES]; int sample_index=0; int voltage_ave=0; int main(void) { volatile unsigned int i; WDTCTL = WDTPW | WDTHOLD; // Stop WDT // Initialize the shared reference module // By default, REFMSTR=1 => REFCTL is used to configure the internal reference while(REFCTL0 & REFGENBUSY); // If ref generator busy, WAIT REFCTL0 |= REFVSEL_0 + REFON; // Enable internal 1.2V reference // GPIO Setup P1OUT &= ~BIT0; // Clear LED to start P1DIR |= BIT0; // Set P1.0/LED to output P5SEL1 |= BIT4; // Configure P5.4 for ADC P5SEL0 |= BIT4; __enable_interrupt(); NVIC_ISER0 = 1 << ((INT_ADC14 - 16) & 31); // Enable ADC interrupt in NVIC module while(!(REFCTL0 & REFGENRDY)); // Wait for reference generator // to settle // Configure ADC14 ADC14CTL0 = ADC14SHT0_2 | ADC14SHP | ADC14ON; // Sampling time, S&H=16, ADC14 on ADC14CTL1 = ADC14RES_2; // Use sampling timer, 12-bit conversion results //ADC14MCTL0 |= ADC14INCH_1; // A1 ADC input select; Vref=AVCC ADC14MCTL0 =ADC14VRSEL_1 + ADC14INCH_1; // A1 ADC input select; Vref=1.2V ADC14IER0 |= ADC14IE0; // Enable ADC conv complete interrupt SCB_SCR &= ~SCB_SCR_SLEEPONEXIT; // Wake up on exit from ISR while (1) { //for (i = 20000; i > 0; i--); // Delay ADC14CTL0 |= ADC14ENC | ADC14SC; // Start sampling/conversion // while(!(ADC14IFGR0 & BIT0)); // Wait for conversion to complete if (sample_index == 0) { voltage_ave = 0; for (i=0; i<4; i++) { voltage_ave = voltage_ave + samples[i]; } voltage_ave = voltage_ave >> 2; // divide by 4 by right shifting 2 for (i=0; i<4; i++) { samples[i] = 0; } P1OUT ^= BIT0; // Toggle LED0 after every average } __sleep(); // __bis_SR_register(LPM0_bits | GIE); // LPM0, ADC14_ISR will force exit __no_operation(); // For debugger } } // ADC14 interrupt service routine void ADC14IsrHandler(void) { samples[sample_index] = ADC14MEM0 ; sample_index = (sample_index + 1) % NUM_SAMPLES ; // modulus 4 (wrap around) }