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.

Fatfs SD Card

I'm looking at packing my messages using sprintf, then have f_write to write into the sdcard. However, I'm currently having the issue writing when I'm using the following sprintf(message, "hygro: %d", hygro_reading); suggestions why? I got no problem writing sprintf(message, "hello world");

		//Declaration
		uint16_t hygr_Data = 0;
		int32_t hygr_Reading;
		char message[200];
		memset(message, NULL, sizeof(message));
		const char newline[] = "\n\r";

		//part of the code within the for loop.
		hygr_Reading = (125*(hygr_Data/65536.0))-6;

		UARTprintf("%04x %d, %d\n", hygr_Data, hygr_Data, hygr_Reading);

		sprintf(message, "Hygrometer:%d", hygr_Reading);

		UARTprintf("%s\n", message);

		iFResult = f_write(&fil, message, sizeof(message), &count);
		if(iFResult != FR_OK) {UARTprintf("Error writing to file: %s\n", StringFromFResult(iFResult));}
		SysCtlDelay(SysCtlClockGet()/3);

		iFResult = f_write(&fil, newline, sizeof(newline), &count);
		if(iFResult != FR_OK) {UARTprintf("Error writing to file: %s\n", StringFromFResult(iFResult));}
		SysCtlDelay(SysCtlClockGet()/3);

		iFResult = f_sync(&fil);
		if(iFResult != FR_OK) {UARTprintf("Error syncing to file: %s\n", StringFromFResult(iFResult));}
		SysCtlDelay(SysCtlClockGet()/3);

		memset(message, NULL, sizeof(message));

		SysCtlDelay(SysCtlClockGet() / 12);

  • Hong Rui Chong said:
    having the issue writing when I'm using the following sprintf(message, "hygro: %d", hygro_reading); suggestions why? I got no problem writing sprintf(message, "hello world");

     Hi,

    Where are you writing "hello world"?

    Hong Rui Chong said:
    iFResult = f_write(&fil, message, sizeof(message), &count);

     Is fil initialized and a file assigned to?

  • hi robert. regarding the first question, I got interchange the code between the following.

    sprintf(message, "Hygrometer:%d", (int)(hygr_Reading)); and

    sprintf(message, "Hello World");

    Hello world writes successfully every loop. So i'm thinking if there could be some overflow error happening.

    regarding part 2, yes, fil is initialized with a file assigned to it.

  •  I think you are writing float value to var than sprintf fail on some unpredictable manner...

    Hong Rui Chong said:
    int32_t hygr_Reading;

     This is defined as Int32 but also float can accomodate here, c is not checking for type so check next :

    Hong Rui Chong said:
    hygr_Reading = (125*(hygr_Data/65536.0))-6;

     Dangerous prcatice, maybe it is casted or not, you are dividing by a float value so inner parenthesis promote int16 to float then return a float, multiplied by an integer 125 is again promoted to float and then 6 again so final result is float stored on int32, there is space to do that... casted or not to int?

     What is printed to usart?

     And what is written to file?

    try cast result

    hygr_Reading = (int)(125*(hygr_Data/65536.0))-6;

    so please specify more about what issue is: generic error, wrong number, write failure...

  • Hong Rui Chong said:
    thanks robert. =D

     From this I assume it was wrong number printed....

     C is dangerous, never like VHDL but too many time value are silently converted and assigned to different type.