Phipps Electronics

Order within the next 

FREE SHIPPING OVER $199

50,000+ ORDERS

WORLDWIDE SHIPPING

SSL SECURED

Playing Audio Clips on your Microcontroller (Part 2)

Contents

This blog continues from the previous one regarding playing audio clips on your microcontroller using I2S.

Output the Audio on your Microcontroller

After converting your wav file to a .c file, you can now prepare to store this data to your mcu and output audio.

1. You can create firmware for your Black Pill’s I2S peripheral by going through the previous blog on preparing a microcontroller for digital audio.

2. Edit the .ioc or STM32CubeMX file by double-clicking it to enable its graphical interface. Go to Multi-media –> I2S. The I2S parameters should match with your audio clip. With this, ensure you have 16-bit data and frame format. For the audio frequency, since you down sampled your audio to mono, the sampling frequency will only be half of this. With this, a 16 KHz stereo audio sampling rate becomes 8 KHz. However, you may find you have to work on your I2S clock speed. Generate code after setting things up correctly.

3. You can now put your .c audio data to your c project. However, some modifications should be made to make the data compatible to your code. Create a sample.c and sample.h file like below. Note that there were 56200 words generated during your wav to c file converrsion. Don’t forget to include the sample.h header file in your main code.

4. Edit your main.c file to output audio using the HAL_I2S_Transmit( ) function.

				
					  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  if(HAL_GPIO_ReadPin(Key_Button_GPIO_Port, Key_Button_Pin) == 0)
	  {
		  HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
		  HAL_Delay(300);
	  	  HAL_I2S_Transmit(&hi2s2, (uint16_t*)audio_clip, 56200, 10);


	  	HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
	  }

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
				
			

5. Test your code. You should hear your saved audio.

The next blogs will teach you other ways to play audio on your microcontroller.

SUBSCRIBE FOR NEW POST ALERTS

Subscribe to be the first to know when we publish a new article!
List Subscriptions(Required)

POPULAR POSTS

Scroll to Top