[HC32F448 Review] + Output adjustable sine wave with DAC
[Copy link]
This post was last edited by dmzdmz666666 on 2023-9-1 23:45
Chapter 7: Using DAC to output adjustable sine wave
This article will evaluate the HC32F448 DAC output sine wave.
HC32F448 is equipped with a 12-bit DAC with two channels. The built-in AMP also has an output amplification function.
When output amplification is not enabled, the output voltage range is larger, the output is rail-to-rail, the settling time is relatively fast, and the theoretical update rate is 400KSPS-670KSPS.
When the output amplifier is enabled, the load capacity should be improved. The output is not rail-to-rail, but has a rise of about 200mV.
-----------------------Start transplanting------------------------
The resources needed here include DAC.
First, create C files such as DAC.c and their corresponding h header files in the Hardware folder. At the same time, add the library function of hc32_ll_dac.c. Also remember to enable the corresponding components of hc32f4xx_conf.h.
The initialization of DAC is relatively simple. What needs to be noted here is that there is an ADC conversion priority mode, which means that after the ADC is turned on, the DAC can only be converted after the ADC conversion to prevent power supply influence.
Because we want to generate a sine wave here, we need to calculate the digital quantity required to output the sine wave. Here we use the sin() function directly to calculate. We need to add the <math.h> header file. The specific calculation formula is:
for(i=0U;i<Sine_Dot_Num;i++)
{SinTable=(uint32_t)(2048*(sin(2*Pi*i/Sine_Dot_Num)+1));}
Where Sine_Dot_Num is the number of points contained in a waveform. The more points there are, the finer the waveform is, but the generated waveform frequency is lower because the update rate of the DAC is fixed.
It should be noted here that the DAC's sine wave data table must be generated first and then written to the output, because calculating and outputting at the same time may affect the update rate of each point of the DAC.
Add the following code to the main function to change the number of sine wave points by pressing the button. After pressing the button, the sine wave table will be updated and regenerated, and the current number of points will be printed.
-----------------------practical testing------------------------
First, the number of output points is 100. After actual testing, the frequency is 8.86KHz. It can be calculated that the update rate is about 886KSPS, the setup time is about 1.13us, the output peak value is about 3.4V, and the output bottom value is 0V, which is consistent with the description in the data sheet.
It is OK to be seen by naked eyes, but after zooming in, the steps are still quite obvious, and the output waveform purity is not enough
The number of output points is 50. After actual testing, the frequency is 17.58KHz. It can be found that the waveform is basically unusable and the waveform is not smooth enough.
The output point number is 10, and it can be found that the sine wave directly turns into a step wave
The number of output points is 256, and the sine wave is basically available.
The conclusion is that if this DAC is used to output a sine wave, the frequency should be limited to below 10KHz, otherwise it is basically unusable. Of course, the DAC built into the MCU is generally not used to generate medium and high frequency arbitrary waves. After all, it is not a high-speed DAC, but is used to generate static voltage.
At this point, the DAC output sine wave has been tested and is basically in a usable state.
|