cancel
Showing results for 
Search instead for 
Did you mean: 

Hi Friends, I am trying to generate the audio by using STM32F103C8T6 board . I am using raw wav data. I configured TIM2 to generate 8Khz PWM pulse . when I am attaching the speaker to PA0 Pin only some tic and noise sound generate. Please help me.

Ravi Sharma
Associate II

//The wav sample format is from 8bit sound, 8K Hz in an hexadecimal converted array

The given below is details of my TIM2 Init function.

static void MX_TIM2_Init(void)

{

 TIM_MasterConfigTypeDef sMasterConfig;

 TIM_OC_InitTypeDef sConfigOC;

 htim2.Instance = TIM2;

 htim2.Init.Prescaler = 0;

 htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim2.Init.Period = 8999;

 htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

 htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

 if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)

 {

   _Error_Handler(__FILE__, __LINE__);

 }

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

 if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)

 {

   _Error_Handler(__FILE__, __LINE__);

 }

 sConfigOC.OCMode = TIM_OCMODE_PWM1;

 sConfigOC.Pulse = 0;

 sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

 sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

 if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)

 {

   _Error_Handler(__FILE__, __LINE__);

 }

 HAL_TIM_MspPostInit(&htim2);

}

And this is details of my PWM Interrupt handler function where I am changing the data for next pulse.

void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)

{

   uprint("Pulse Finished-\r\n");

      if(i<4096)

         {

         i = i + j;

         //data is an array of 8 bit sample from raw wav file

         uint16_t v = (data[i]);

         __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, v);

         }

      else

         {

         HAL_GPIO_TogglePin (GPIOC, GPIO_PIN_13);

i=0;

         }

}

This is my main function details.

int main(void)

{

 HAL_Init();

SystemClock_Config();

 MX_GPIO_Init();

 MX_USART3_UART_Init();

 MX_TIM2_Init();

 HAL_TIM_PWM_Start_IT(&htim2, TIM_CHANNEL_1);

 while (1)

 {

 }

}

1 REPLY 1

Observe the waveform on PA0 with an oscilloscope. Is it what you expect?

> when I am attaching the speaker to PA0 Pin

Is there any low-pass filter and an amplifier between the pin and speaker?

JW