Writing to a file (fprintf) in MCUXpresso IDE (11.3.1)

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Writing to a file (fprintf) in MCUXpresso IDE (11.3.1)

Jump to solution
1,502 Views
caden013
Contributor III

Hi all,

I am wanting to print contents to a file using the C function "fprintf," but I cannot get the code to work properly. Here is my code: 

FILE *analog_values = fopen("analog_values.txt", "w");
if (analog_values == NULL)
{
PRINTF("File could not be opened\n");
}

FPRINTF(analog_values, "%d\n", g_LpadcResultConfigStruct.convValue >> g_LpadcResultShift);

fclose(analog_values);

 

When I run this code, I receive "File could not be opened" in the terminal every time, and the rest of my code (not shown here) does not run. If anyone can help, I would greatly appreciate it.

Thanks in advance.

0 Kudos
1 Solution
1,482 Views
converse
Senior Contributor V

Suggest you use the lower case functions calls (printf, scan etc) as the UPPER CASE are macro's and their actual function depends on the header files used, and the setting of various #defines. Suggest you read the section of the User Guide on C/C++ support - this describes how to use the sim hosting libraries and the use of printf (and macros etc)

Make sure the correct project is selected in project explorer to ensure the library types are selectable.

View solution in original post

8 Replies
1,451 Views
MK001
Contributor I

I want to know please, where we can find the created file (on the computer or on the flash memory of the microcontroller)

0 Kudos
1,419 Views
caden013
Contributor III

The created file will be in the same folder as the source code, which creates the file.

0 Kudos
1,498 Views
converse
Senior Contributor V

1. Make sure you use fprintf instead of FPRINTF (to avoid the macros)

2. Make sure you are using the semihosting version of the library. Either Redlib (semihost), Newlib (semihost) or NewlibNano (semihost) from the Quick Settings

Screenshot 2021-06-08 at 16.02.54.png

 

1,497 Views
caden013
Contributor III

Thanks for the quick response.

I should have mentioned that I used a macro to define FPRINTF as fprintf -- just to maintain the formatting like the other functions (PRINTF, GETCHAR, SCANF, etc.). 

From the quick settings, I can access the "Set library/header type" tab, but all the options are grey and inaccessible. How can I go about fixing this issue?

0 Kudos
1,483 Views
converse
Senior Contributor V

Suggest you use the lower case functions calls (printf, scan etc) as the UPPER CASE are macro's and their actual function depends on the header files used, and the setting of various #defines. Suggest you read the section of the User Guide on C/C++ support - this describes how to use the sim hosting libraries and the use of printf (and macros etc)

Make sure the correct project is selected in project explorer to ensure the library types are selectable.

1,479 Views
caden013
Contributor III

Thank you -- that seemed to work. My code now creates a file -- exactly what I want it to do. However, the file is blank, and I can't seem to get anything to print into it. 

I am using the following line of code to print to my file: 

fprintf(analog_values, "%d\n", (g_LpadcResultConfigStruct.convValue) >> g_LpadcResultShift);

Is there something wrong with this line, or is the problem somewhere else?

0 Kudos
1,470 Views
converse
Senior Contributor V

Do you ever fclose the file? If not, try fflush. The data is probably cached.

0 Kudos
1,467 Views
caden013
Contributor III

I used the fflush function before and after the fprintf statement to no avail. Here is the code that I am using now:

FILE *analog_values = fopen("analog_values.txt", "w");

int x = 0;

while (x < 500)
{
LPADC_DoSoftwareTrigger(DEMO_LPADC_BASE, 1U); /* 1U is trigger0 mask. */
while (!g_LpadcConversionCompletedFlag)
{
}
PRINTF("ADC value: %d\r\n",
((g_LpadcResultConfigStruct.convValue) >> g_LpadcResultShift));
g_LpadcConversionCompletedFlag = false;

if (analog_values == NULL)
{
PRINTF("File could not be opened\n");
}

fprintf(analog_values, "%d\n", (g_LpadcResultConfigStruct.convValue) >> g_LpadcResultShift);
fflush(analog_values);

if ( (int)g_LpadcResultConfigStruct.convValue > 0 && (int)g_LpadcResultConfigStruct.convValue < 16384)
{
GPIO_PinWrite(GPIO, BOARD_LED_RED_GPIO_PORT, BOARD_LED_RED_GPIO_PIN, 1);
GPIO_PinWrite(GPIO, BOARD_LED_GREEN_GPIO_PORT, BOARD_LED_GREEN_GPIO_PIN, 0);
GPIO_PinWrite(GPIO, BOARD_LED_BLUE_GPIO_PORT, BOARD_LED_BLUE_GPIO_PIN, 0);
}

else if ( (int)g_LpadcResultConfigStruct.convValue > 16384 && (int)g_LpadcResultConfigStruct.convValue < 32768)
{
GPIO_PinWrite(GPIO, BOARD_LED_RED_GPIO_PORT, BOARD_LED_RED_GPIO_PIN, 0);
GPIO_PinWrite(GPIO, BOARD_LED_GREEN_GPIO_PORT, BOARD_LED_GREEN_GPIO_PIN, 1);
GPIO_PinWrite(GPIO, BOARD_LED_BLUE_GPIO_PORT, BOARD_LED_BLUE_GPIO_PIN, 0);
}

else if ( (int)g_LpadcResultConfigStruct.convValue > 32768 && (int)g_LpadcResultConfigStruct.convValue < 49152)
{
GPIO_PinWrite(GPIO, BOARD_LED_RED_GPIO_PORT, BOARD_LED_RED_GPIO_PIN, 0);
GPIO_PinWrite(GPIO, BOARD_LED_GREEN_GPIO_PORT, BOARD_LED_GREEN_GPIO_PIN, 0);
GPIO_PinWrite(GPIO, BOARD_LED_BLUE_GPIO_PORT, BOARD_LED_BLUE_GPIO_PIN, 1);
}

else if ((int)g_LpadcResultConfigStruct.convValue > 49152 && (int)g_LpadcResultConfigStruct.convValue < 65536)
{
GPIO_PinWrite(GPIO, BOARD_LED_RED_GPIO_PORT, BOARD_LED_RED_GPIO_PIN, 1);
GPIO_PinWrite(GPIO, BOARD_LED_GREEN_GPIO_PORT, BOARD_LED_GREEN_GPIO_PIN, 1);
GPIO_PinWrite(GPIO, BOARD_LED_BLUE_GPIO_PORT, BOARD_LED_BLUE_GPIO_PIN, 1);
}

else
{
GPIO_PinWrite(GPIO, BOARD_LED_RED_GPIO_PORT, BOARD_LED_RED_GPIO_PIN, 1);
}

x++;

}

fclose(analog_values);

return 0;
}

 

0 Kudos