I. Overview
The DAC of STM32 can output triangular waves or noise. This article will see how to generate triangular waves.
A triangle wave is a periodic signal with equal periods but non-sinusoidal waveforms. Its waveform is similar to an isosceles right triangle, and the inclination direction of the hypotenuse can be either rising or falling. We often wonder why we need to generate a triangle wave, or what is the use of a triangle wave? The application range of triangle waves is relatively wide. In audio synthesis, a series of triangle waves can be superimposed to simulate complex sound waveforms. In image processing, triangle waves can be used to generate grayscale stripes to check the performance of scanners or printers. In test instruments, triangle waves can be used to test channel bandwidth, spectrum analysis, and timing measurements.
DAC Block Diagram
Triangle wave with adjustable frequency
Second project configuration
DAC Configuration
Timer Configuration
Each time TIM6 overflows, the DAC output will increase by 1 until it reaches the maximum parameter set, and then gradually decrease until it reaches the base value. This process will be repeated to achieve a triangular wave with adjustable frequency.
Add the start code for DAC and TIM
HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, 0);
HAL_TIM_Base_Start(&htim6);
Three tests
The timer uses TIM6
System clock 160M
160/(159+1)= 1M or 1us
1us*100 = 0.1ms
2048*2*0.1 = 409.6ms is basically consistent with the test value