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/MSP430F6736: Create .CSV file

Part Number: MSP430F6736

Tool/software: Code Composer Studio

Hi Team,

I want to generate a .CSV file using the outputs that I print on the console(in CSV format).

I want to export the values that I print on the console to a .CSV file.

I am using the following code:

1185.final_code.c
#include <msp430.h>
#include <stdio.h>
#include <time.h>

/* Array to store SD24_B conversion results */
unsigned int results[3];                          //SD24 converter result array
unsigned int ADC_Result[6];                      // 8-bit ADC conversion result array
float A0,A1,A2,A3,A4,A5,A6,A7,A8;                //analog voltage result registers
float I0,I1,I2,I3,I4,I5,I6,I7,I8;                //current result registers
unsigned int sig=0x0;
time_t current_time;
char *c_time_string;

int main(void)
{

    WDTCTL = WDTPW | WDTHOLD;               // Stop WDT

    // Setup P1.2 A0, 1.1 A1, 1.0 A2
       P1SEL |= BIT0 | BIT1 | BIT2;                  // Set P1.0,.1,.2 to non-IO
       P9SEL |= BIT1 | BIT2 | BIT3;                  //set P9.1,.2,.3 to non-IO

       P5DIR |= BIT0 | BIT1 | BIT2 | BIT3;




       __disable_interrupt();                        // Disable interrupts; Port Map cnfg
       PMAPKEYID = PMAPKEY;                          // Enable access Port Mapping regs
       P1MAP2 = PM_ANALOG;                           // Enable A0
       P1MAP1 = PM_ANALOG;                           // Enable A1
       P1MAP0 = PM_ANALOG;                           // Enable A2
       PMAPKEYID = 0;                                // Disable access Port Mapping regs
       __enable_interrupt();                         // Re-enable all interrupts


       // Setup ADC10
       ADC10CTL0 = ADC10SHT_3 | ADC10MSC | ADC10ON;  // 16 ADCclks, MSC, ADC ON
       ADC10CTL1 = ADC10SHP | ADC10CONSEQ_1;         // pulse sample mode, single sequence
       ADC10CTL2 |= ADC10RES;                       // 8-bit resolution
       ADC10MCTL0 = ADC10INCH_5;                     // A0,A1,A2(EoS), AVCC reference

       // Setup DMA0 (ADC10IFG trigger)
       DMACTL0 = DMA0TSEL_24;                        // ADC10IFG trigger
       DMA0SZ = 0x06;                                // 3 conversions
       __data16_write_addr((unsigned short) &DMA0SA, (unsigned long) & ADC10MEM0);
       // Source single address
       __data16_write_addr((unsigned short) &DMA0DA, (unsigned long) & ADC_Result[0]);
       // Destination array address
       DMA0CTL = DMADT_4 | DMADSTINCR_3 | DMAEN | DMAIE;
       DMA0CTL &= ~DMASRCBYTE;
       DMA0CTL &= ~DMADSTBYTE;
       // Repeated single transfer
       // Increment destination
       // Byte access
       // Enable int after seq of convs






    SD24BCTL0 |= SD24SSEL_1;      // Select internal REF
    SD24BCTL0 &= ~SD24REFS;       // Select SMCLK as SD24_B clock source

    SD24BCCTL0 &= ~SD24SNGL;
    //SD24BCCTL0 |= SD24ALGN;
    SD24BCCTL0 |= SD24DF0;
    SD24BCCTL0 |= SD24SCS_5;      // Single conversion, group 1

    SD24BCCTL1 &= ~SD24SNGL;
    //SD24BCCTL1 |= SD24ALGN;
    SD24BCCTL1 |= SD24DF0;
    SD24BCCTL1 |= SD24SCS_5;      // Single conversion, group 1

    SD24BCCTL2 &= ~SD24SNGL;
    //SD24BCCTL2 |= SD24ALGN;
    SD24BCCTL2 |= SD24DF0;
    SD24BCCTL2 |= SD24SCS_5;      // Single conversion, group 1

    SD24BIE |= SD24IE2; //| SD24IE1 | SD24IE0;                     // Enable channel 2 interrupt

    printf("\" \",\"V1\",\"Supply\",\"V2\",\"Supply\",\"Time\"\n");
    printf("\" \",\"voltage(in V)\",\"current(in A)\",\"voltage(in V)\",\"current(in A)\"\n");



    __delay_cycles(0x10000);                 // Delay for 1.5V REF startup

    while (1)
    {

        //time setup
        time_t t = time(NULL);
        struct tm *tm = localtime(&t);
        char s[64];
        strftime(s, sizeof(s), "%c", tm);


        while (ADC10CTL1 & ADC10BUSY) ;           // Wait if ADC10 core is active
        ADC10CTL0 |= ADC10ENC | ADC10SC;          // Sampling and conversion start

        __bis_SR_register(LPM0_bits | GIE); // Enter LPM0 w/ interrupts
        __delay_cycles(1000);                     // Delay between sequence convs
        __no_operation();                         // BREAKPOINT; View ADC_Result




        //ADC_10 voltage and current display
        A0=(ADC_Result[5]*3.3)/(1024*5);
        I0=(A0)/2.43;

        A1=(ADC_Result[4]*3.3)/(1024*5);
        I1=(A1)/2.43;

        A2=(ADC_Result[3]*3.3)/(1024*5);
        I2=(A2)/2.43;

        A3=(ADC_Result[2]*3.3)/(1024*5);
        I3=(A3)/2.43;

        A4=(ADC_Result[1]*3.3)/(1024*5);
        I4=(A4)/2.43;

        A5=(ADC_Result[0]*3.3)/(1024*5);
        I5=(A5)/2.43;


        printf("\"A5\",\"%f\",\"%f\",\" \",\" \",\"%s\"\n",A5,I5,s);
        //printf(" %s current I5= %f \n",s,I5);

        printf("\"A4\",\"%f\",\"%f\",\" \",\" \",\"%s\"\n",A4,I4,s);
        //printf("\n %s analog voltage A4= %f \n",s,A4);
        //printf(" %s current I4= %f \n",s,I4);

        printf("\"A3\",\"%f\",\"%f\",\" \",\" \",\"%s\"\n",A3,I3,s);
        //printf("\n %s analog voltage A3= %f \n",s,A3);
        //printf(" %s current I3= %f \n",s,I3);

        printf("\"A2\",\"%f\",\"%f\",\" \",\" \",\"%s\"\n",A2,I2,s);
        //printf("\n %s analog voltage A2= %f \n",s,A2);
        //printf(" %s current I2= %f \n",s,I2);

        printf("\"A1\",\"%f\",\"%f\",\" \",\" \",\"%s\"\n",A1,I1,s);
        //printf("\n %s analog voltage A1= %f \n",s,A1);
        //printf(" %s current I1= %f \n",s,I1);

        printf("\"A0\",\"%f\",\"%f\",\" \",\" \",\"%s\"\n",A0,I0,s);
        //printf("\n %s analog voltage A0= %f \n",s,A0);
        //printf(" %s current I0= %f \n",s,I0);



        SD24BCTL1 |= SD24GRP1SC;            // Set bit to start conversion
        __bis_SR_register(LPM0_bits | GIE); // Enter LPM0 w/ interrupts

        SD24BCTL1 &= ~SD24GRP1SC;           // Clear bit for next conversion
        __no_operation();                   // SET BREAKPOINT HERE


        //SD24 converter voltage and current display
        A6=(results[0]*3.3)/(256*5);
        I6=(A6)/2.43;

        A7=(results[1]*3.3)/(5*256);
        I7=(A7)/2.43;

        A8=(results[2]*3.3)/(256*5);
        I8=(A8)/2.43;

        printf("\"A6\",\"%f\",\"%f\",\" \",\" \",\"%s\"\n",A6,I6,s);
        //printf("\n %s analog voltage A6= %f \n",s,A6);
        //printf(" %s current I6= %f \n",s,I6);

        printf("\"A7\",\"%f\",\"%f\",\" \",\" \",\"%s\"\n",A7,I7,s);
        //printf("\n %s analog voltage A7= %f \n",s,A7);
        //printf(" %s current I7= %f \n",s,I7);

        printf("\"A8\",\"%f\",\"%f\",\" \",\" \",\"%s\"\n",A8,I8,s);
        //printf("\n %s analog voltage A8= %f \n",s,A8);
        //printf(" %s current I8= %f \n",s,I8);

        __delay_cycles(2000);

        if (sig==0x10)
        {
            sig = 0x00;
        }

        else
        {
            printf("select line chosen= %d \n",sig);
            P5OUT = sig;
            sig++;
        }


    }






}

#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=SD24B_VECTOR
__interrupt void SD24BISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(SD24B_VECTOR))) SD24BISR (void)
#else
#error Compiler not supported!
#endif
{
    switch (SD24BIV)
    {
        case SD24BIV_SD24OVIFG:             // SD24MEM Overflow
            break;
        case SD24BIV_SD24TRGIFG:            // SD24 Trigger IFG
            break;
        case SD24BIV_SD24IFG0:              // SD24MEM0 IFG
            break;
        case SD24BIV_SD24IFG1:              // SD24MEM1 IFG
            break;
        case SD24BIV_SD24IFG2:              // SD24MEM2 IFG
            results[0] = SD24BMEMH0;        // Save CH0 results (clears IFG)
            results[1] = SD24BMEMH1;        // Save CH1 results (clears IFG)
            results[2] = SD24BMEMH2;        // Save CH2 results (clears IFG)
            break;
    }

    __bic_SR_register_on_exit(LPM0_bits);   // Exit LPM0
}



#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=DMA_VECTOR
__interrupt void DMA0_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(DMA_VECTOR))) DMA0_ISR (void)
#else
#error Compiler not supported!
#endif
{
    switch (__even_in_range(DMAIV, 16))
    {
        case DMAIV_NONE: break;                   // No interrupts
        case DMAIV_DMA0IFG:                       // DMA0IFG = DMA Channel 0
            // sequence of conversions complete
            __bic_SR_register_on_exit(LPM0_bits); // exit LPM0 on return
            break;
        case DMAIV_DMA1IFG: break;                // DMA1IFG = DMA Channel 1
        case DMAIV_DMA2IFG: break;                // DMA2IFG = DMA Channel 2
        case  8: break;                           // Reserved
        case 10: break;                           // Reserved
        case 12: break;                           // Reserved
        case 14: break;                           // Reserved
        case 16: break;                           // Reserved
        default: break;
    }
}

Is there a way to generate a .CSV file?

Thank you,

Keval

  • Hi Keval,

    I do not believe there is a way to do this in CCS. One thing you could try using CCS is to store your outputs in a variable and graph that. You can then export the graph data as a .csv.

    Other than that, I believe the only way to do what you are describing is to use a script.

    Regards,
    Nathan
  • Hi Nathan,

    How do I store my outputs and graph that?
    And how do I export that graph as a .csv file?

    could you help me out?

    Thanks,
    Keval
  • Hi Nathan,

    I am using this command to generate a csv file.
    The code is able to print the data if I give it directly (if I use "value").
    But I am unable to write the ADC data into the file created using:

    fprintf(fp,"\"A4\",\"%f\",\"%f\",\" \",\" \",\"%s\"\n",A4,I4,s);

    I have attached the code that I am using above. Is there an error in the code?
    Could you take a look at it?

    Thank you,
    Keval
  • How many times are you going to ask this?

    processors.wiki.ti.com/.../Printf_support_for_MSP430_CCSTUDIO_compiler
    processors.wiki.ti.com/.../Tips_for_using_printf

    AFAIK, the C i/o functions are only available if you are using the CCS debugger, which is generally not what you want to do.

    Therefore, you need a program on the host PC that will read the serial port and create the file for you. Almost any language can do this.
  • If you want to do it with CCS and the debugger attached, this works for me on an MSP432P401R

    /* Standard Includes */
    #include <stdint.h>
    #include <stdbool.h>
    #include <stdio.h>
    
    int main(void)
    {
        FILE *f;
        int i, sq;
    
        /* Stop Watchdog  */
        MAP_WDT_A_holdTimer();
        printf("Hello, World!\n");
        fflush(stdout);
    
        f = fopen("testcsv.csv", "w");
        fprintf(f, "Header, Next Col\n");
        fprintf(f, "i, Square\n");
    
        for (i=0; i<10; i++)
        {
            sq = i*i;
            
            fprintf(f, "%d, %d\n", i, sq);
        }
    
        fclose(f);
    
        printf("Done!\n");
        fflush(stdout);
    
        while (1)
        {
    
        }
    
    
    }
    

  • Why all the quotes?

    Instead of
    fprintf(fp,"\"A4\",\"%f\",\"%f\",\" \",\" \",\"%s\"\n",A4,I4,s);

    Quotes are only needed in CSV's when you want to include a comma in the string. Try this:

    fprintf(fp, "A4, %f, %f, %s\n", A4, I4, s);

    make sure s is null terminated and read the links I provided before to make sure that %f is activated for fprintf().

    And give yourself a large heap! I used 1024.
  • Hi Keith,

    I have changed the settings for the printf support.
    The example code that you had in your reply is working for me as well. But I am not able to write float values into the file.
    You have mentioned to activate %f for fprintf(). How do I do that?


    Thank You,
    Keval
  • Works for me (Again for an MSP432P401R):

    /* DriverLib Includes */
    #include <ti/devices/msp432p4xx/driverlib/driverlib.h>
    
    /* Standard Includes */
    #include <stdint.h>
    #include <stdbool.h>
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
        FILE *f;
        int i;
        double l;
    
        MAP_WDT_A_holdTimer();
    
        f = fopen("testfloat.csv", "w");
        fprintf(f, "i, ln()\n");
    
        for (i=1; i<10; i++)
        {
            l = log((double)i);
    
            fprintf(f, "%d, %f\n", i, l);
            printf("%d, %f\n", i, l);
        }
    
        fflush(stdout);
    
        fclose(f);
    
    
    
        while (1)
        {
    
        }
    
    
    }

    Results:

    i     ln()
    1    0
    2    0.693147
    3    1.098612
    4    1.386294
    5    1.609438
    6    1.791759
    7    1.94591
    8    2.079442
    9    2.197225

  • Hi Keith,

    I am using the following code:

    #include <msp430.h> 
    #include <stdint.h>
    #include <stdbool.h>
    #include <stdio.h>
    #include <math.h>
    
    
    /**
     * main.c
     */
    
    
    int main(void)
    {
    	WDTCTL = WDTPW | WDTHOLD;	// stop watchdog timer
    	
    
        FILE *f;
        int i;
        int sq;
        double fl;
    
        printf("Hi \n");
    
                fflush(stdout);
    
                f = fopen("testcsv.txt", "w");
                fprintf(f, "Header, Next Col\n");
                fprintf(f, "i, Square\n");
    
                for (i=0; i<10; i++)
                {
                    sq = i*i;
                    fl=sq/1.25;
                    fprintf(f, "%d %f\n",i,fl);
                }
    
                fclose(f);
    
                printf("Done!\n");
                fflush(stdout);
    
                while (1)
                {
    
                }
    
    }
    

    When I go and check the txt file generated, I don't find any data written into the file.

    Is there an error in the code?

    Thanks,

    Keval

     

  • I tried and ran the same program as yours.
    It created the csv file but there was no data inside.

    Than you,
    Keval
  • Then I don't know. You might try an fflush(), but fclose should take care of that.
  • Hi Keith,

    I tried to run this code:

    #include <msp430.h> 
    #include <stdint.h>
    #include <stdbool.h>
    #include <stdio.h>
    #include <math.h>
    
    
    /**
     * main.c
     */
    
    
    
    int main()
    {
    	WDTCTL = WDTPW | WDTHOLD;	// stop watchdog timer
    
    	FILE *f = fopen("file.csv", "w");
    
    
    	    if (f == NULL)
    	    {
    	        printf("Error opening file!\n");
    	        exit(1);
    	    }
    
    	    /* print some text */
    	    const char *text = "Write this to the file";
    	    fprintf(f, "Some text: %s\n", text);
    
    	    /* print integers and floats */
    	    int i = 1;
    	    float py = i/1.54;
    	    fprintf(f, "Integer: %d, float: %f\n", i, py);
    
    	    /* printing single chatacters */
    	    char c = 'A';
    	    fprintf(f, "A character: %c\n", c);
    
    	    fclose(f);
    
    
    
    
        while (1)
        {
    
        }
    
    
    }
    
    
    
    
    
    This code generated the excel file for me and even printed the float values in the file.
    Do I need to any extra settings other than the ones that you said?

    Thank you,
    Keval

**Attention** This is a public forum