STM32 PWM and DAC exercises



1. Use STM32F103 to output a PWM waveform

1. Relevant resource preparation

Link: https://pan.baidu.com/s/13bnrB-S8Y6JDRdXBbzzbjA
Extraction code: pmf1

1): Follow the path in the figure below to find "1-Book supporting routines-F103VE guide" and download it
Insert picture description here
2): Unzip after downloading (32- TIM—Universal Timer- >2-TIM—Universal Timer-4 PWM Output)
Insert picture description here
Insert picture description here

2. Use keil to directly view the PWM waveform

1): Open the project
Insert picture description here
2): Related configuration changes
Magic wand->debug->Use Simulaor
Insert picture description here
Insert picture description here
3): Debug after compiling,
Insert picture description here
open the analysis window,
Insert picture description here
click setup,
Insert picture description here
enter GPIOA_IDR.6, select bit
Insert picture description here
Insert picture description here
4): Run to view the waveform (if you can’t see it) Scroll to view)
Insert picture description here
Insert picture description here

Two, STM32F103 DAC practice

1. Relevant resource preparation

Decompress "38-DAC—Sine Wave Output" in the supporting materials downloaded above, and download Au (I use Adobe_Audition_CS6), UltraEdit and WavToC converter on the Internet.
Insert picture description here

2. Convert and save audio files

1): Use music software to download your favorite music (preferably an mp3 file
) and convert it on Au.
Insert picture description here
Select the starting position. In the lower right corner, we set the duration to 2s, and the interval is automatically generated
Insert picture description here

Insert picture description here
2): Digital audio file generation.
Right-click the interval -> click the storage selection area to
Insert picture description here
change the sampling type.
Insert picture description here
Sampling 8khz, quantization 16bit, single channel

Insert picture description here
Save when you are done

3): Sine wave file generation
New audio file
Insert picture description here
sampling 8khz, quantization 16bit, single channel
Insert picture description here

Choose to generate the basic tone in the effect and
Insert picture description here
set it as shown in the figure
Insert picture description here

After zooming in, you can see the waveform as shown in the figure
Insert picture description here

Save
Insert picture description here
Insert picture description here

3. Use WavToC to convert audio files into c language code

1): Open WavToC, open the file,
Insert picture description here
select the file,
Insert picture description here
generate the code,
Insert picture description here
you can listen to it yourself, and then click to save the code, remember the number of output bytes here
Insert picture description here

The same is true for sine waves
Insert picture description here
Insert picture description here

4. Write the code and display the output with an oscilloscope

1). Convert digital audio song data to analog audio waveform output

Use Notepad to open the music audio code
Insert picture description here
Crtl+A and select all, copy and
Insert picture description here
open the previously unzipped "38-DAC—Sine Wave Output" project.
Insert picture description here
Change bsp—dac.c to Insert picture description here
modify the code as shown in the figure:

Insert picture description here
Compile and generate hex file. After
Insert picture description here
burning, connect the board to the oscilloscope and you can see the output waveform
Insert picture description here

Insert picture description here

2). Output a sine wave with a period of 2khz

Copy the code
Insert picture description here
Modify the bsp-dac.c code as shown in the figure.
Insert picture description here
Compile and generate a hex file.
Insert picture description here
Here I forgot to use the oscilloscope to display the waveform, so there is no waveform.

Guess you like

Origin blog.csdn.net/aiwr_/article/details/112095944