Discovering the STM32 CUBE IDE Part 3: Interfacing Analog Audio Device
2023-10-27 | By DWARAKAN RAMANATHAN
License: General Public License ARM mbed
Introduction:
Discover the world of Analog Audio Devices in STM32 CUBE IDE: As crucial components in embedded systems, these devices enable audio signal processing and reproduction. STM32 CUBE IDE provides a powerful development environment for STM32 microcontrollers, streamlining the integration and configuration of Analog-to-Digital Converters (ADCs) and Digital-to-Analog Converters (DACs). This comprehensive guide delves into the essentials of Analog Audio Devices in STM32 CUBE IDE, from configuring ADCs to capture analog audio inputs to setting up DACs for audio output. Whether you're working on audio playback, voice recording, or sound-based sensing applications, mastering these capabilities empowers you to create innovative and immersive audio experiences. Unleash the full potential of STM32 microcontrollers in audio applications and embark on an exciting journey into the world of analog audio.
Note: Before learning this part of this STM32 series, I would recommend you first complete part-1 and part-2 of this series.
The link to the first part of this series is provided here: Discovering the STM32 CUBE IDE: A Simple LED Blink and GPIO Project
The link to the second part of this series is provided here: Discovering the STM32 CUBE IDE Part 2: Learning the UART
Creating New Project:
To create a project, go to File > New Project. There will be a new window opened for you to select the board or MCU. In this project, we will use B-L4S5I-IOT01A, the part is also linked below. Go to Board Selector and select the board by typing in the commercial part number (B-L4S5I-IOT01A) and click Next.
After clicking Next, Type the project name (In this case: Analog_input) and click on Finish.
After clicking finish, STM32 CUBE IDE will automatically download the software packages that your board requires. After the download is complete, you will see a workspace something like this (a .ioc file will be opened):
Assigning ports:
In this project, we will be sending messages to our console from the device (STM32 Board). STM32 Board uses UART-based communication. Now, to enable UART in your board go to connectivity > USART1 and check if it is in Asynchronous Mode. Now, you will see a warning symbol near USART1. To remove that, please reset PB3, PB4, and PB5. After doing so, you will see that the warning is gone, and a green tick near the USART1 is visible.
Also, you need to use one of the ADCs present in the device. So, to utilize one of the ADC we need to enable it also with the UART. To do so, go to Analog and Enable only the first ADC to IN-1 Single-Ended.
Generating code:
As you have completed assigning the ports you can generate the code. Click Ctrl+S to save the file and then generate code by going to Project > Generate Code.
After Generating the code, you will have to edit three sections:
- The User Include Section
- The Private variables Section
- The User Code Begin 3 Inside the while loop
- The User Code Begin 4
The codes of each section are given below:
1. The User Include Section
#include "stdio.h"
#include "string.h"
2. The Private Variables Section
float mic_x = 0.0;
3. The User Code Begin 3
HAL_ADC_Start(&hadc1);
mic_x = HAL_ADC_GetValue(&hadc1);
printf("%.2f", mic_x);
printf("\r\n");
HAL_Delay(100);
4. The User Code Begin 4
int __io_putchar(int ch){
HAL_UART_Transmit(&huart1, (uint8_t *) &ch, 1, HAL_MAX_DELAY);
return ch;
}
Understanding: The Include section is filled with stdio.h and string.h libraries because we are using both print statements as well as string datatype. The Private Variables Section is added because we need a variable to store the data from the analog device. So, we create a variable of the datatype float. The User Code Begin 3 is the actual code that will Run on the Microcontroller Device (Send messages to the console). The User Code Begin 4 is for transmitting characters (Data) from the Microcontroller to the Console using UART. We are just enabling the microcontroller to use UART and transmit data.
Removing the printf error:
Whenever you try to use printf or scanf statement there will be a compiler error which simply means that the compiler does not allow using printf or scan. So, to allow the compiler to recognize them, right-click on the project > Properties > C/C++ Build > Settings > MCU Settings and select both scanf and printf statements. The code will now look good.
Click on Apply and Close.
Building and Debugging:
To build the project go to Project > Build Project. If you see no errors then the code is ready to be debugged.
Connections:
After building the project connect your Microcontroller board and check the Communication port in the device manager (Ex: COM7). The device name in my case can be different from yours. Do not panic; just check for the Port Number for any STM-based Device.
In this case, the port is 3. Now, Debug the main.c file and check whether the console shows "Download verified successfully".
After debugging the code, you should use the ability of the device to use UART to send messages to your console, and for that, you have to create a console mentioning the port that your device is connected to. To do so, go to the console window below the workspace and select Command shell console under Open Console.
After clicking on it, you will see a new window opened for you to choose the port and name of the console. The connection type is Serial port and click on "New".
After clicking New, type the name of your port, select the serial port to which your device is connected, and click Finish.
After clicking finish you will see a new console created for you and it will tell you that the console is connected.
Now, you are ready to resume the debugging. Click on "resume" which is available on the topmost taskbar. After that, you will see that your microcontroller is sending you the values from the analog device.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum