cancel
Showing results for 
Search instead for 
Did you mean: 

How to store values in memory card from array

Apill
Senior

I am using an ADC input pin and trying to store values in memory card. Before storing in memory card i have stored in an array thinking that after some 2000 values i can store in memory card. Before storing 2000 values i tried to store 5 values but i couldnt able to store in memory for some reason.

I have configured adc with timer for a flexible sampling rate. I tried multiple ways to store the values from array to sdcard but to no avail. Below is the code.

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)

{

 /* Prevent unused argument(s) compilation warning */

 UNUSED(hadc);

adc_val= HAL_ADC_GetValue(&hadc1);

}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

 /* Prevent unused argument(s) compilation warning */

 UNUSED(htim);

temp_value[i] =adc_val; 

i++;

}

In the main, code is as follows:

HAL_ADC_Start_IT(&hadc1);

HAL_TIM_Base_Start(&htim2);

char myPath [] = "WRITE1.TXT\0";

char myData [] = " hello ashok\0";

f_mount(&myFATFS,SDPath,1);

f_open(&myFILE, myPath, FA_WRITE | FA_CREATE_ALWAYS);

f_write(&myFILE, myData, sizeof(myData), &testByte);

f_printf(&myFILE, "Hello world \0");

/* USER CODE END 2 */

 //// the above said i.e., hello ashok gets written to sd card

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

 if(i==5)

 {

sd_card();

 }

 }

}

void sd_card(void)

{

for(j=5; j<=0; j--)

{

temp = temp_value[j];

f_printf(&myFILE, "\r\n %d", temp);

f_printf(&myFILE, "\r\n %d Hello\0", j);

}

f_close(&myFILE);

}

i can able to write values to sd card if i am not using the variable i,( the global values which is in timer function). when ever i am keeping the condition that if i == some value and trying to write values to sd card it is not happening. any inputs please ? Here I is in short seconds. as i have kept the timer to trigger every 1 second.

6 REPLIES 6

Is there an error reported?

This stuff is very similar to the File IO implemented by STDIO, you can print the data as ASCII, this will use considerably more space that writing a block of binary data.

uint16_t array[8192];

f_write((&myFILE, array, sizeof(array), &testByte);

If you must use printf() type implementation, run it in a loop filling a buffer :

length = 0;

for(j=0; j<5; j++)

length += sprintf(buffer + length, "%d,", temp_value[j]);

f_write((&myFILE, buffer, length, &testByte);

Ideally a much larger buffer, and where you manage the buffer in a fashion that allows for an aligned power-of-two write, say of 8192 bytes, or 32768 bytes.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Apill
Senior

@Community member​  There is no error reported. I will try your suggestion above.

AvaTar
Lead

Dense, non-redundant binary data have the disadvantage of undetectable corruption.

Apill
Senior

@Community member​  any specific reason why you are stressing more on f_write rather than f_ printf. Even though both the functions can write on to sd card.

Using f_printf has the efficiency of filling a 55 gallon barrel with a teaspoon...

Your method might get 10KBps, writing large blocks I can get 10MBps

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

could you please elaborate your answer