A
A
Alexander Semykin2016-07-11 09:09:14
Microcontrollers
Alexander Semykin, 2016-07-11 09:09:14

How to correctly set the interrupt frequency for sound output through the DAC of the microcontroller?

Gentlemen, I have encountered an unusual problem and need help and advice.
Task: output a WAV file to the DAC channel of the microcontroller (I work with the STM32F4discovery board). The task is trivial, you don't even need to read the file from the flash drive - I immediately ripped out the PCM data from the WAV file (the file was specially prepared: 8 kHz, 8-bit, mono) and loaded them as an array from the header file.
The next portion of information is output to the DAC by interrupts from a timer operating at a frequency of 8 kHz (which corresponds to the file sampling frequency). However, when I try to bring it all to the speaker, I get some kind of wheezing and hiss, which is quite far from what I want to hear.

void Audio_TIM_Init(void) {
  TIM_ClockConfigTypeDef sClockSourceConfig;
  TIM_MasterConfigTypeDef sMasterConfig;

  __HAL_RCC_TIM3_CLK_ENABLE();

  htim.Instance = TIM3;
  htim.Init.Prescaler = (uint16_t) (SystemCoreClock / 1000000) - 1;
  htim.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim.Init.Period = 125;
  htim.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  HAL_TIM_Base_Init(&htim);

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  HAL_TIM_ConfigClockSource(&htim, &sClockSourceConfig);

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 HAL_TIMEx_MasterConfigSynchronization(&htim, &sMasterConfig);

  HAL_NVIC_SetPriority(TIM3_IRQn, 0, 1);
  HAL_NVIC_EnableIRQ(TIM3_IRQn);
  HAL_TIM_Base_Start_IT(&htim);
}

Through experimentation, I achieved the desired sound (albeit with terrible noise) at an interruption frequency of 32 kHz. And now I'm trying to understand what is the reason for this behavior of the microcontroller and what should I be guided by when setting up interrupts?
UPD: The issue was resolved by an oscilloscope and a thoughtful study of the STM32 clocking circuit. As it turned out, I set the wrong divisor during the initialization of the system bus, and therefore the timer clocked just 4 times slower than I expected.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Armenian Radio, 2016-07-11
@vagrantnotes

Connect your DAC to an oscilloscope (sound card will suffice) and look at the signal.

E
evgeniy_lm, 2016-07-11
@evgeniy_lm

The output of the DAC should have a low-pass filter.

A
Anatoly Voronchikhin, 2016-07-11
@tolikvoron

Remember Kotelnikov's theorem.... The polling frequency should be at least twice as high as the frequency of the polled signal. And, of course, filtering... That is, the frequency is higher than 30-40 kHz... The more often, the better....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question