0
#include <msp430.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.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


    FILE * fp;

    // 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`enter code here`
       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

    fp = fopen ("file.txt", "w+");

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

    fclose(fp);



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




    while (sig < 16)
    {

        //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;


        fp = fopen ("file.txt", "w+");
        fprintf(fp,"\"A5\",\"%f\",\"%f\",\" \",\" \",\"%s\"\n",A5,I5,s);
        //printf(" %s current I5= %f \n",s,I5);

        fprintf(fp,"\"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);

        fprintf(fp,"\"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);

        fprintf(fp,"\"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);

        fprintf(fp,"\"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);

        fprintf(fp,"\"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);

        fclose(fp);



        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;


        fp = fopen ("file.txt", "w+");

        fprintf(fp,"\"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);

        fprintf(fp,"\"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);

        fprintf(fp,"\"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);

        fclose(fp);

        //__delay_cycles(2000);

        /*if (sig==0x10)
        {
            sig = 0x00;
            fclose(fp);
        }*/

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


    }

    sig = 0x00;

}

#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;
    }
}

I am using MSP430f6736 micro-controller and Code Composer Studio to program the micro-controller. I am using the internal ADCs and SD converters to read analog inputs and display them on the screen. I want to write the values into a .CSV file or a txt file. I have written the code above using the fopen statement.

I am unable to write the data that I am getting from the ADCs and SD24 converters into the txt file. I want to generate a .CSV file. The values that I am getting after the conversion, I am storing them in CSV format.

Is there an error in the code? If yes, what shall I do in order to achieve the result? I want to generate a .CSV file using the data that I get from the micro-controller.

could you help me out in solving this issue?

Thank you.

KD0704
  • 9
  • 1
  • 3
    Where this file is supposed to be located? – Eugene Sh. Aug 14 '18 at 20:54
  • 2
    You should check the return value from fopen. – stark Aug 14 '18 at 20:58
  • the file is located in the Debug folder of the code directory. – KD0704 Aug 14 '18 at 21:58
  • How do I check the return value from fopen? – KD0704 Aug 14 '18 at 21:59
  • 1
    Is your `fopen` semihosted? If not, how do you expect msp430 to write your computer? – Eugene Sh. Aug 14 '18 at 22:01
  • I have connected the microcontroller to the computer using a Flash Emulation tool that has a JTAG interface and is connected to the computer through a USB. Will this help in writing the data to the computer? – KD0704 Aug 14 '18 at 22:08
  • 1
    fopen() could not write to a file located on your computer, you're working on an embedded machine. fopen() will write to the heap memory on your controller. – Mike Aug 15 '18 at 05:37
  • strongly suggest only call `fopen()` once, then call `fclose()` when ready to exit the program. Suggest using `fflush()` to assure each data gets written, just in case the chip is reset or the power is disconnected. Suggest stepping through the 'main()' loop of your program to assure the interrupts are actually occurring and the watchdog is not firing – user3629249 Aug 15 '18 at 22:13
  • "how to check the returned value from `fopen()`"? `fp = fopen ("file.txt", "w+"); if( !fp ) { perror( "fopen failed" ); ` – user3629249 Aug 15 '18 at 22:15
  • @Mike: It's more likely `fopen` is just a dummy and returns an error unconditionally. I'm actually surprised it even existx. Using a heap on such small MCUs (or most MCUs) is already a bad idea and `malloc`, etc. should not exist at all (together with all other parts of the standard library except the mandatory for a freestanding implementation/environment. – too honest for this site Aug 16 '18 at 09:29
  • @user3629249 I altered the code and called fopen() and fclose() just once. But I am unable to write float and string values into the file. I am able to write integer values only. I am unable to write float and string values(%f and %s). – KD0704 Aug 16 '18 at 17:30
  • @Mike the fopen() command opens a file in the debug folder of the workspace that I work on currently. However, when I just write integer data to the file, it writes the data into the file, but same is not the case for float and string values. – KD0704 Aug 16 '18 at 17:31

0 Answers0