This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/MSP432P401R: CCS Sine Wave Graphing / Sample Rate

Part Number: MSP432P401R

Tool/software: Code Composer Studio

Hi,

I am running the code for the adc-14-single-conversion-repeat.c from the resource explorer with modified code to display the normalized adc and graph the normalizedADC, however when graphing a sine wavewith a frequency of greater than 500mHz the graph does not act as a sine wave due to the speed of which the adc is being read.

As seen in the images I attached below, at a frequency of 100mHz the read ADC (The floats outputted by the console) is consistent with a sine wave, BUT as soon as the frequency is increased to something like 1kHz the ADC value being outputted to the console shows that the ADC is not being read fast enough to graph the sine wave appropriately.

So my question is how would I speed up the ADC read speed(sample rate) so that the ADC reading would be accurate for a sine wave so the graph can be plotted accordingly

MY CODE:

/* --COPYRIGHT--,BSD
* Copyright (c) 2017, 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.
* --/COPYRIGHT--*/
/*******************************************************************************
* MSP432 ADC14 - Single Channel Sample Repeat
*
* Description: This code example will demonstrate the basic functionality of
* of the DriverLib ADC APIs with sampling a single channel repeatedly. Each
* time the ADC conversion occurs, the result is stored into a local variable.
* The sample timer is used to continuously grab a new value from the ADC
* module using a manual iteration that is performed in the ADC ISR. A
* normalized ADC value with respect to the 3.3v Avcc is also calculated using
* the FPU.
*
* MSP432P401
* ------------------
* /|\| |
* | | |
* --|RST P5.5 |<--- A0 (Analog Input)
* | |
* | |
* | |
* | |
* | |
*
******************************************************************************/
/* DriverLib Includes */
#include <ti/devices/msp432p4xx/driverlib/driverlib.h>

/* Standard Includes */
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>

/* Statics */
static volatile uint16_t curADCResult; //uint16_t
static volatile float normalizedADCRes;
float graph;

static float convertToFloat(uint16_t result)
{
int32_t temp;
if(0x8000 & result)
{
temp = (result >> 2) | 0xFFFFC000;
//printf("hit if");
return ((temp * 3.3f) / 8191);
}
else
{
//printf("hit else");
return ((result/2)*3.3f) / (8191);
}
}


int main(void)
{
/* Halting the Watchdog */
MAP_WDT_A_holdTimer();

/* Initializing Variables */
curADCResult = 0;

/* Setting Flash wait state */
MAP_FlashCtl_setWaitState(FLASH_BANK0, 1);
MAP_FlashCtl_setWaitState(FLASH_BANK1, 1);

/* Setting DCO to 48MHz */
MAP_PCM_setPowerState(PCM_AM_LDO_VCORE1);
MAP_CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_48);

/* Enabling the FPU for floating point operation */
MAP_FPU_enableModule();
MAP_FPU_enableLazyStacking();

//![Single Sample Mode Configure]
/* Initializing ADC (MCLK/1/4) */
MAP_ADC14_enableModule();
MAP_ADC14_initModule(ADC_CLOCKSOURCE_MCLK, ADC_PREDIVIDER_1, ADC_DIVIDER_4,
0);
//MAP_ADC14_initModule(ADC_CLOCKSOURCE_MCLK, ADC_PREDIVIDER_1, ADC_DIVIDER_4,0);

/* Configuring GPIOs (5.5 A0) */
MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5, GPIO_PIN5,
GPIO_TERTIARY_MODULE_FUNCTION);

/* Configuring ADC Memory */
MAP_ADC14_configureSingleSampleMode(ADC_MEM0, true);
MAP_ADC14_configureConversionMemory(ADC_MEM0, ADC_VREFPOS_AVCC_VREFNEG_VSS,
ADC_INPUT_A0, false);

/* Configuring Sample Timer */
MAP_ADC14_enableSampleTimer(ADC_MANUAL_ITERATION);

/* Enabling/Toggling Conversion */
MAP_ADC14_enableConversion();
MAP_ADC14_toggleConversionTrigger();
//![Single Sample Mode Configure]

/* Enabling interrupts */
MAP_ADC14_enableInterrupt(ADC_INT0);
MAP_Interrupt_enableInterrupt(INT_ADC14);
MAP_Interrupt_enableMaster();

while (1)
{
MAP_PCM_gotoLPM0();
}

}

//![Single Sample Result]
/* ADC Interrupt Handler. This handler is called whenever there is a conversion
* that is finished for ADC_MEM0.
*/
void ADC14_IRQHandler(void)
{
int i = 0;
int j = 0;
float val[100];
float avg = 0;

//double nRes;
uint64_t status = MAP_ADC14_getEnabledInterruptStatus();
//float nRes;
//uint32_t temp;
MAP_ADC14_clearInterruptFlag(status);

if (ADC_INT0 & status)
{
while(i<100)
{
curADCResult = MAP_ADC14_getResult(ADC_MEM0);
normalizedADCRes = convertToFloat(curADCResult);
//normalizedADCRes = (curADCResult * 3.3) / 16384;
//nRes = (double)normalizedADCRes;
//printf("\n\r> D0 = %d.%03d V", nRes);
val[i] = normalizedADCRes;
graph = val[i];
//printf("%f\n", normalizedADCRes);
printf("%f\n", val[i]);
MAP_ADC14_toggleConversionTrigger();
i++;
}
//printf("FINISHED 100 samples\n");

while(j < 100)
{
avg = avg + val[j];
j++;
}

avg = avg/100;
printf("Average: %f\n", avg);
exit();
}


}
//![Single Sample Result]

IMAGES OF MY RESULTS:

1kHz OUTPUTS:

Please Help, and Thank You!

  • >printf("%f\n", val[i]);

    This call takes a long time, and dominates your loop. 

    I suggest you

    1) move the "val" array (and "i") outside the ISR (global)

    2) Don't call exit(), rather call ADC14_disableConversion() and return from the ISR

    3) In your while() loop in main(), watch for "i" to reach 100, then call printf() for the entire val[] array there.

    This should increase your sample rate, but it won't really guarantee what that rate is -- for that you should use a timer trigger.(as seen in e.g. adc14_single_conversion_repeat_timera_source)

    ----

    I don't know offhand how long printf() takes, and there's some evidence that it varies. I added code to toggle a GPIO in the sampling loop, and my scope tells me that the first 3 samples run at about 6kHz, and the rest at about 10kHz.

  • Hi,

    Thank You for the help, however I still seem to be encountering issues with the sampling speed as when looking at the values in my val[i] array for a 1kHz signal the values are not what they should be. 

    I have modified my code to the conventions you advised as so:

    /* DriverLib Includes */
    #include <ti/devices/msp432p4xx/driverlib/driverlib.h>

    /* Standard Includes */
    #include <stdint.h>
    #include <stdbool.h>
    #include <stdio.h>

    /* Statics */
    static volatile uint16_t curADCResult; //uint16_t
    static volatile float normalizedADCRes;

    /* Other variables*/
    float graph;
    float val[100]; //Moved val[] and i,j,k variables
    int i = 0;
    int j = 0;
    int k = 0;

    static float convertToFloat(uint16_t result)
    {
    int32_t temp;
    if(0x8000 & result)
    {
    temp = (result >> 2) | 0xFFFFC000;
    //printf("hit if");
    return ((temp * 3.3f) / 8191);
    }
    else
    {
    //printf("hit else");
    return ((result/2)*3.3f) / (8191);
    }
    }


    int main(void)
    {
    /* Halting the Watchdog */
    MAP_WDT_A_holdTimer();

    /* Initializing Variables */
    curADCResult = 0;

    /* Setting Flash wait state */
    MAP_FlashCtl_setWaitState(FLASH_BANK0, 1);
    MAP_FlashCtl_setWaitState(FLASH_BANK1, 1);

    /* Setting DCO to 48MHz */
    MAP_PCM_setPowerState(PCM_AM_LDO_VCORE1);
    MAP_CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_48);

    /* Enabling the FPU for floating point operation */
    MAP_FPU_enableModule();
    MAP_FPU_enableLazyStacking();

    //![Single Sample Mode Configure]
    /* Initializing ADC (MCLK/1/4) */
    MAP_ADC14_enableModule();
    MAP_ADC14_initModule(ADC_CLOCKSOURCE_MCLK, ADC_PREDIVIDER_1, ADC_DIVIDER_4,
    0);
    //MAP_ADC14_initModule(ADC_CLOCKSOURCE_MCLK, ADC_PREDIVIDER_1, ADC_DIVIDER_4,0);

    /* Configuring GPIOs (5.5 A0) */
    MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5, GPIO_PIN5,
    GPIO_TERTIARY_MODULE_FUNCTION);

    /* Configuring ADC Memory */
    MAP_ADC14_configureSingleSampleMode(ADC_MEM0, true);
    MAP_ADC14_configureConversionMemory(ADC_MEM0, ADC_VREFPOS_AVCC_VREFNEG_VSS,
    ADC_INPUT_A0, false);

    /* Configuring Sample Timer */
    MAP_ADC14_enableSampleTimer(ADC_MANUAL_ITERATION);

    /* Enabling/Toggling Conversion */
    MAP_ADC14_enableConversion();
    MAP_ADC14_toggleConversionTrigger();
    //![Single Sample Mode Configure]

    /* Enabling interrupts */
    MAP_ADC14_enableInterrupt(ADC_INT0);
    MAP_Interrupt_enableInterrupt(INT_ADC14);
    MAP_Interrupt_enableMaster();

    while (1)
    {
    MAP_PCM_gotoLPM0();
    }

    }

    //![Single Sample Result]
    /* ADC Interrupt Handler. This handler is called whenever there is a conversion
    * that is finished for ADC_MEM0.
    */
    void ADC14_IRQHandler(void)
    {
    float avg = 0;

    //double nRes;
    uint64_t status = MAP_ADC14_getEnabledInterruptStatus();
    //float nRes;
    //uint32_t temp;
    MAP_ADC14_clearInterruptFlag(status);

    if (ADC_INT0 & status)
    {
    while(i<100)
    {
    curADCResult = MAP_ADC14_getResult(ADC_MEM0);
    normalizedADCRes = convertToFloat(curADCResult);
    //normalizedADCRes = (curADCResult * 3.3) / 16384;
    //nRes = (double)normalizedADCRes;
    //printf("\n\r> D0 = %d.%03d V", nRes);
    val[i] = normalizedADCRes;
    graph = val[i];
    //printf("%f\n", normalizedADCRes);
    //printf("%f\n", val[i]);
    MAP_ADC14_toggleConversionTrigger();
    i++;
    if(i == 100) //trigger when array is filled and print out values of the array once hit
    {
    while(k < 100)
    {
    printf("%f\n", val[k]);
    k++;
    }
    }
    }

    //printf("FINISHED 100 samples\n");

    while(j < 100) //calculates the average using the values stored in array
    {
    avg = avg + val[j];
    j++;
    }

    avg = avg/100;
    printf("Average: %f\n", avg);
    ADC14_disableConversion(); //instead of exit() disabling conversion to exit
    }


    }

    but the output of the graphs look like this:

    Please advise, and Thank you so much! 

  • I did the same procedure as before, and my scope reports a sample rate of about 278ksps. At that rate 100 samples is about 360 usec, roughly 1/3rd of a 1kHz sine wave. It would be much less of a 0.5Hz sine wave, so I don't think you'd see the multi-cycle curves that are shown at that imgur link.

    Briefly put: I suspect you're not testing what you think you are. Are you sure the (new) code downloaded?

    [Edit: Unsolicited; You're not actually waiting for the ADC to finish before capturing the results. I think this works by accident due to the other things you're doing in the loop, but it's something to watch out for.]

    I wasn't able to hook up the CCS graphing, so I pasted the 1kHz results into Open Office, and it shows a fairly smooth curve.

  • Thank You!

    So it seems the error I had was due to using breakpoints for graphing as once I removed breakpoints and just used excel to graph the val[ ] array i got consistent values with a sine wave. Thank You very much!

    https://imgur.com/a/Tl4mGys