This post was last edited by dmzdmz666666 on 2021-4-30 23:33
In this chapter, I will test the internal comparator of AT32F421. AT32F421 has a low-power comparator inside, which can be used as an independent device (with IO port as input and output port), or can be combined with the timer. The input of the comparator can also use the internal reference voltage, which is more convenient. At the same time, the comparator supports programmable hysteresis, blanking output, and can also control the working speed and power consumption.
Here , I will test the analog signal conditioning function of the comparator, using the internal reference voltage and the external IO port (PA4) input as the negative terminal of the comparator, PA1 as the positive input of the comparator, and PA0 as the comparator output.
Here is the COMP_Out example in COMP in AT_START_F421 in the official BSP.
First , create COMP.c and its corresponding h header file in the HARDWARE folder. Then add at32f4xx_comp.c in FWLIB, as shown below
The second step is to write the COMP_Configuration function in COMP.c. First, configure the GPIO and the corresponding clock as before. We configure PA1 and PA4 as analog input and output mode, and multiplex PA0 as COMP_OUT. Part of the configuration is shown in the figure below.
Next , configure the comparator. First, enable the comparator clock by using the following statement:
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_SYSCFGCOMP, ENABLE);
If you want to use the internal reference voltage as the negative input of the comparator , add the following code. At the same time, AT32F421 also supports 1/4, 2/4, 3/4 times the reference voltage input
COMP_InitStructure.COMP_INMInput = COMP_INMInput_VREFINT;
If you want to use the IO external voltage as the negative input of the comparator, add the following code. Here I choose PA4 as the negative terminal and add the following code
COMP_InitStructure.COMP_INMInput = COMP_INMInput_IN1;//PA4
One thing to note here is that you can only choose between internal input and external input.
Because the output of the AT32F421 comparator can be input not only from the IO port, but also from the timer. However, as an analog signal conditioner, I configure it not to output to other peripherals. See the following code
COMP_InitStructure.COMP_Output = COMP_Output_None;
At the same time, during the actual test process, I found that when conditioning the analog signal, there was a big difference in the output effect between turning on the hysteresis function and not turning it on (of course, it might be a problem with my signal source)
COMP_InitStructure.COMP_Hysteresis = COMP_Hysteresis_High;
The above sentence is to turn on the high hysteresis.
COMP_InitStructure.COMP_Hysteresis =COMP_Hysteresis_No
The above sentence does not turn on hysteresis
Next, I will compare the differences between the two
Please refer to my attachment for the specific code. Finally, call COMP_Configuration() in the main function to use the comparator function.
Next, I will use an oscilloscope, a signal generator, and a multimeter to test the actual effect of the comparator.
I first used the internal reference voltage of the microcontroller as the negative input of the comparator, and did not turn on the hysteresis . It can be observed that when a 1KHZ 0-3.3V sine wave is input to the positive electrode, the comparator will output a 0-3.3V square wave, which just proves that it is rail-to-rail, because the high level that can be output is the working voltage; at the same time, through the cursor measurement of the oscilloscope, it can be seen that the comparator is flipped at 1.2V, because according to the previous experiments and data manual, the internal reference voltage of the microcontroller is about 1.2V. The experimental phenomenon is in line with expectations, that is, when the positive electrode is less than 1.2V, the comparator outputs a low level, and when it is higher than 1.2V, it outputs a high level.
However, if you zoom in on the waveform , you will find that the comparator will repeatedly compare and flip around 1.2V, causing multiple waveforms to overlap on the rising edge of the actual waveform. I think the reason for this phenomenon may be that the waveform I input is not ideal and there are some burrs. On the other hand, it is the problem of the microcontroller itself, because the reference voltage is not a constant value, it will also change, and the comparator itself will also have a certain error.
In order to reduce the generation of this kind of repeated comparison waveform , I turned on the high hysteresis of the comparator. Sure enough, the waveform became much cleaner.
Finally, I tested the external IO port as the negative input of the comparator. I input a voltage of 2.000V on PA4, and the actual effect is as follows
By changing the value of the external input voltage , the duty cycle of the output square wave can be changed. Next, a gif is used to demonstrate it.
|