cancel
Showing results for 
Search instead for 
Did you mean: 

Read 6 waveform frequencies with stm32f103c8t, along with analog DMA reads and serial TTL communication

DCord.1
Associate

Hi I'm having an issue while reading 6 inputs each one with one waveform at the same time I'm collecting analog inputs and sending to TTL and reading settings from TTL.

At some point if any of the wave inputs gets 119 Khz or more, the stm32 becomes unpredictable. Sometimes it stop sending to serial, sometimes stop to receive serial, stop reading the frequencies.

I'm using TIM2(3, 4) and TIM3(1, 2, 3, 4).

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
	uint32_t capturedValue = 0;
	uint8_t timerPosition = 0; 
	if(htim->Instance == TIM3 && htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) {
		timerPosition = 0;
                capturedValue = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
	} else if(htim->Instance == TIM3 && htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) {
		timerPosition = 1;
                capturedValue = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
	} else if(htim->Instance == TIM3 && htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3) {
		timerPosition = 2;
                capturedValue = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3);
	} else if(htim->Instance == TIM3 && htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4) {
		timerPosition = 3;
                capturedValue = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4);
	} else if(htim->Instance == TIM2 && htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3) {
		timerPosition = 4;
                capturedValue = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3);
	} else if(htim->Instance == TIM2 && htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4) {
                capturedValue = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4);
		timerPosition = 5;
	}
 
	if (isFirstCapture[timerPosition]==0) {
	  icValue1[timerPosition] = capturedValue;
	  isFirstCapture[timerPosition] = 1;
	} else if (isFirstCapture[timerPosition]) {
		icValue2[timerPosition] = capturedValue;
	  if (icValue2[timerPosition] > icValue1[timerPosition]) {
		diference[timerPosition] = icValue2[timerPosition]-icValue1[timerPosition];
	  } else if (icValue2[timerPosition] < icValue1[timerPosition]) {
		  diference[timerPosition] = ((0xffff-icValue1[timerPosition])+icValue2[timerPosition]) +1;
	  } else {
		Error_Handler();
	  }
	  frequency[timerPosition] = ( HAL_RCC_GetPCLK1Freq()/diference[timerPosition] ) * 2;
	  isFirstCapture[timerPosition] = 0;
	}
}

I can read from just one channel without problem using this approach. But this mean blocking my main loop for 1 second what is way too much, imagine 6 seconds of readings.

void read_freq (void)
{
	if (frequency[0] == 0) {
		htim2.Instance->CNT = 0;
		while ((htim2.Instance->CNT) < 1000)  
		{
			while (!(HAL_GPIO_ReadPin (FREQ_PIN_GPIO_Port, FREQ_PIN_Pin)));  
			while ((HAL_GPIO_ReadPin (FREQ_PIN_GPIO_Port, FREQ_PIN_Pin)));  
			increment++;
		}
		frequency[0] = increment;  
		increment = 0;
	} 
}

There is any way I can come out with a solution for this problem?

I need to read as fast as possible both frequencies and analogs and send through serial.

Also I need to be able to read from 1 Khz to 8 Mhz in each channel.

Perhaps is there a STM32 more suitable for the job or the STM32f103 can do it?

0 REPLIES 0