cancel
Showing results for 
Search instead for 
Did you mean: 

Timer input capture mode: DAC generation on incoming frequency from function generator.

MSpir.2
Associate II

Timer2 uses timer input capture mode. The input is coming from a function generator and is a block signal. It triggers the capture callback on falling edge.

On the capture Callback interrupt I read in the capture compare register of Timer2 and I change the ARR register (period) from Timer1 to that value (count). So basicly Timer1 is a timer that generates interrupts on the frequency of the falling edge of the incoming PWM signal.

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
 
count = __HAL_TIM_GET_COMPARE(htim, TIM_CHANNEL_4);
((htim)->Instance->CNT = (0));
TIM1->ARR = count;
 
}

I have an array of 360 int32 elements representing a sine wave. On the interrupt of timer1 I write the index element of the array to a DAC channel.

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	 if(htim->Instance==TIM1){
 
	    HAL_DAC_SetValue(&hdac1,DAC_CHANNEL_1, DAC_ALIGN_12B_R, x[i]);
	    if (i == 359)
	      {
	  	 i = 0;
	      }
	    i++;
 }
 
 
}

When I speed up or slow down the frequency of the PWM signal from the function generator, the sine wave also needs to speed up or slow down. This happens but when changing the frequency the value seems to hold for a moment. (see picture). 0693W00000LxEX6QAN.jpg

3 REPLIES 3

Is TIM1->ARR preloaded, i.e. is TIM1->CR1.ARPE set?

JW

S.Ma
Principal

So if I understand well, you input a pulse with a certain peruod, and after measuring it,, generate a say 100 times faster pulse which trigger a dac update which DMA feed cyclically from an array of Dac samples, is this right?

indeed