cancel
Showing results for 
Search instead for 
Did you mean: 

DAC. Arbitrary form signal generation.

Ann Darkhanova
Associate II
Posted on May 31, 2018 at 08:43

Hello, dear Community!

I'm using DAC to generate oscillogram of a short-circuit in a power line(50Hz). To achieve this I created array of discrete samples in MathCAD with sampling frequency of 4000Hz. In order to feed my samples to DAC with the same frequency(4kHz) I'm initializing a timer.

The problem is that I see something inappropriate on my scope that hardly can be named a sinewave at all. I looked through the similar examples across the web, so here I have:

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){

     if (htim->Instance==TIM6)

   {

      for(int i=0; i<n; i++){

      HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1,  DAC_ALIGN_12B_R, inf[i]);

                              }

                                                                                                       }

 HAL_TIM_Base_Start_IT(&htim6);

 __HAL_DAC_ENABLE(&hdac, DAC_CHANNEL_1);

  htim6.Instance = TIM6;

  htim6.Init.Prescaler = 999;

  htim6.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim6.Init.Period = 53;

  htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

  HCLK is 216 MHz.

Could you clarify please how it should be organized properly and make sure that DAC gives me proper signal?

#arbitrary_signals #stm32f7
3 REPLIES 3
stm322399
Senior
Posted on May 31, 2018 at 13:25

The timer callback is only required to feed one single sample to DAC.

Remove that *for* loop and only write one single sample which index is a global variable (or a declared with static keyword), then increment, wrap to zero when necessary, that should do the job.

Posted on June 02, 2018 at 09:43

  Thank you for reply, Laurent! I drew my attention to the examples I had seen before and noticed there are no any *for* loops in callbacks. I understood my mistake I changed the body of the callback function. It may sound foolish but I got strange syntax error, whereas code seems to be syntactically right.

0690X0000060KtTQAU.png

   As far as I'm considered I may also place my piece of code in the TIM6_DAC_IRQHandler() function  which is declared in the separate source file for interrupts. Though, results are poor.

   What might be wrong with code on the pic? Could you please clarify the difference between the callback function for my timer and interrupt handler?

   I'm new to MCU's so I say sorry for my silly questions beforehand)

Posted on June 02, 2018 at 09:53

It was my mistake again! I should place the callback function outside main().

P.S. if (i == n) i =