Quantcast
Channel: MSP low-power microcontroller forum - Recent Threads
Viewing all articles
Browse latest Browse all 22296

MSP430F5529: Convert and store 1k samples from ADC

$
0
0

Part Number:MSP430F5529

I am trying to capture 1k samples from an analog accelerometer using ADC and store them in an array for further processing and sending through mqtt afterwards. I am a beginner in this and tried a lot of ways including using timer but wasn't successful. So I finally came up with the following code that works, but I would like to know if this is a good approach.

The code is as follows: I have setup the ADC using ACLK clock

static void setup_ADC(void){
    ADC12CTL0 = ADC12SHT02 + ADC12ON;         // Sampling time, ADC12 on
    ADC12CTL1 = ADC12SHP + ADC12SSEL_1;       // Use sampling timer; Pulse Sample Mode; ADC12SHT02 decides the interval of the sampling timer
                                              // ACLK (32.768 kHz) selected as the clock source
    ADC12IE = 0x01;                           // Enable interrupt
    ADC12CTL0 |= ADC12ENC;
    P6SEL |= 0x01;                            // P6.0 ADC option select
    }

The infinite while loop in main has an mqtt function that should run after 1000 samples have been stored.

while(1){
        /* Signal processing and communication only to be done after collecting 1K samples */
        if (SamplesDone){
            /* Send the values through mqtt */
            mqtt();
            samples_count = 0;
        }

        ADC12CTL0 |= ADC12SC;                   // Start sampling/conversion
        __bis_SR_register(LPM0_bits + GIE);     // LPM0, ADC12_ISR will force exit
        __no_operation();                       // For debugger
    }

In the ISR, I simply move the data into the array that can store upto 1000 elements

#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector = ADC12_VECTOR
__interrupt void ADC12_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(ADC12_VECTOR))) ADC12_ISR (void)
#else
#error Compiler not supported!
#endif
{
    switch(__even_in_range(ADC12IV,34))
    {
    case  0: break;                           // Vector  0:  No interrupt
    case  2: break;                           // Vector  2:  ADC overflow
    case  4: break;                           // Vector  4:  ADC timing overflow
    case  6:                                  // Vector  6:  ADC12IFG0
        adc_value = ADC12MEM0 & 0x0FFF; // keep only low 12 bits; Move results, IFG is cleared
        adc_values[samples_count] = adc_value;
        samples_count += 1;
        /* Set the flag when 1000 samples are collected */
        if (samples_count == 999) {
            SamplesDone = 1;
        }
        __bic_SR_register_on_exit(LPM0_bits);   // Exit active CPU
    case  8: break;                           // Vector  8:  ADC12IFG1
    case 10: break;                           // Vector 10:  ADC12IFG2
    case 12: break;                           // Vector 12:  ADC12IFG3
    case 14: break;                           // Vector 14:  ADC12IFG4
    case 16: break;                           // Vector 16:  ADC12IFG5
    case 18: break;                           // Vector 18:  ADC12IFG6
    case 20: break;                           // Vector 20:  ADC12IFG7
    case 22: break;                           // Vector 22:  ADC12IFG8
    case 24: break;                           // Vector 24:  ADC12IFG9
    case 26: break;                           // Vector 26:  ADC12IFG10
    case 28: break;                           // Vector 28:  ADC12IFG11
    case 30: break;                           // Vector 30:  ADC12IFG12
    case 32: break;                           // Vector 32:  ADC12IFG13
    case 34: break;                           // Vector 34:  ADC12IFG14
    default: break;
    }
}

I would be very thankful to any suggestions/comments on this approach.


Viewing all articles
Browse latest Browse all 22296

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>