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

asked Jan 31, 2022 at 13:34
\$\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\$ Commented Jan 31, 2022 at 14:55
  • \$\begingroup\$ But if I do that the DAC triggers in falling edges. Please see my edit. \$\endgroup\$ Commented Jan 31, 2022 at 15:26
  • 1
    \$\begingroup\$ One way to work around that would be to invert the PWM signal. \$\endgroup\$ 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.

answered Jan 31, 2022 at 16:48
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.