1
\$\begingroup\$

I have a Nucleo board, and want to update the DAC output each time one of the PWM rising edge occurs.

When I configure a PWM using a timer, it auto generates the following interrupt only:

void TIM1_UP_TIM16_IRQHandler(void)
{
  /* USER CODE BEGIN TIM1_UP_TIM16_IRQn 0 */

  /* USER CODE END TIM1_UP_TIM16_IRQn 0 */
  HAL_TIM_IRQHandler(&htim16);
  /* USER CODE BEGIN TIM1_UP_TIM16_IRQn 1 */

  /* USER CODE END TIM1_UP_TIM16_IRQn 1 */
}

I should add the HAL_DAC_SetValue function to some section so that at each rising edge of the PWM HAL_DAC_SetValue outputs a value. Is there a function or way to do that using HAL library or else?

edit:

enter image description here

\$\endgroup\$
3
  • \$\begingroup\$ I don't really understand - you can insert your own code between the comments BEGIN and END. So you could call the HAL_DAC_SetValue there. Not sure what you are really asking. \$\endgroup\$
    – Arsenal
    Commented Jan 31, 2022 at 14:55
  • \$\begingroup\$ But if I do that the DAC triggers in falling edges. Please see my edit. \$\endgroup\$
    – user1999
    Commented Jan 31, 2022 at 15:26
  • 1
    \$\begingroup\$ One way to work around that would be to invert the PWM signal. \$\endgroup\$
    – Arsenal
    Commented Jan 31, 2022 at 16:37

1 Answer 1

1
\$\begingroup\$

Normally, when the TIM update event occurs (counter == ARR), TIM counter is reset and output rises to logic high. Signal falls again when counter matches with CCR value. So, your scope observation is confusing, I would expect it to be other way around.

The easiest thing you can try is inverting the output using relevant output polarity bits in TIMx_CCER register, as @Arsenal suggested in the comments.

But a more effective approach would be using DAC automatic triggering and DMA. You can configure your TIM to generate trigger outputs on various events, like update event or CCR match (see MMS bits in TIMx_CR2 register). Then, you can configure your DAC to use this TRGO of your TIM, so that TRGO triggers a DMA transfer and DAC output update. Of course, DMA also needs to be configured. In this case, you don't even need interrupts. Unfortunately, TIM1 doesn't seem to be available for DAC triggering, but most other timers are usable.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.