[SOLVED] I want to sample analog values and write these into an array but in the end there are not enough samples in the array as expected.
I expect at least 22937 samples until the array is full. My guess now is, that my index counter "number_of_samples" rises faster than that the actual samples that are written into the array. After the index counter reaches the theoretical max value of samples the array would be written on the SD card. But before that the array is not full. Only around 3780 samples are written of the max. available 22937 samples.
I can' t get my head around why me code isn't working.
Specs:
Board: Nucleo 64 F411RE
actual CPU freq.: 64 MHz
sampling frequ.: 8130 Hz
Array size: 110592 Bytes
I appreciate any help!
Best regards Dietrich
//---------------for FATFS----------------
FATFS fs; // file system
FATFS *pfs;
FIL fil; // file
FRESULT fresult; // to store the result
UINT br, bw; // file read/write count
DWORD fre_clust;
uint32_t total, free_space;
//----------------------------------------
//--------------user----------------------
uint8_t blue_button_abort = 0;
uint16_t ADCval = 0;
uint32_t number_of_samples = 0;
uint8_t write_samples_array_yes = 0;
char buffer1_samples_adc[27*4096] = { 0 }; // buffer with adc values (char) who is written on SD
// 4 KByte = 1 * 4096 // 80 KByte = 20 * 4 KByte // 120 KByte = 30 * 4 // geht nicht -> 128 KByte = 32 * 4 KByte
// char one_sample[6] = { 0 };
char one_sample[6] = { 0 };
//----------------------------------------
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_SPI2_Init(void);
static void MX_ADC1_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
// ################## CLEAR BUFFER ##################################################
/*
function to clear buffers
values to put in:
1. the actual buffer
2. the buffer length for example with sizeof(buffer)
*/
void clearbuffer (char *buffer, int buffer_length)
/*clearbuffer(actual buffer to be cleared, sizeof(buffer)) */
{
for (int i = 0; i < buffer_length; i++)
{
*buffer = '\0';
buffer++;
}
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
// get new ADC value
ADCval = HAL_ADC_GetValue(&hadc1);
// increase number of samples that have been read
number_of_samples++;
// marker -> write sample in array
write_samples_array_yes=1;
// activate ADC interrupt
HAL_ADC_Start_IT(&hadc1);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_SPI2_Init();
MX_FATFS_Init();
MX_ADC1_Init();
/* USER CODE BEGIN 2 */
//##### Mount SD Card #############################################################################
fresult = f_mount(&fs, "", 0);
fresult = f_getfree("", &fre_clust, &pfs);
fresult = f_open(&fil, "Data_ADC.txt", FA_OPEN_ALWAYS | FA_WRITE);
//#################################################################################################
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
HAL_ADC_Start_IT(&hadc1);
while (1)
{
// 1.) make adc values to 5 Byte char
// 2.) append adc values (char) in samples buffer
if (write_samples_array_yes == 1)
{
sprintf(one_sample, "%4i\n", ADCval); // onesample size: 5 * 8 Bit = 5 Byte
strncat(buffer1_samples_adc, one_sample, sizeof(one_sample)); // buffer1 size: 5 * 819 + 1 = 4096
clearbuffer(one_sample, sizeof(one_sample));
write_samples_array_yes=0;
}
// if buffer is full (to be precise, RAM), write buffer on SD
if (number_of_samples > 458740) // max. number of samples
{
fresult = f_write(&fil, buffer1_samples_adc, sizeof(buffer1_samples_adc), &bw); // "Buffer" has to be 4096 Bytes -> for performance
clearbuffer(buffer1_samples_adc, sizeof(buffer1_samples_adc));
fresult = f_close(&fil); // close the latest file
fresult = f_mount(0, "", 0); // unmount the SD Card from the FATFS file system
while (1)
{
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
HAL_Delay(250);
}