dmzdmz666666 发表于 2021-4-30 23:26

【AT32F421测评】+ COMP比较器使用

本帖最后由 dmzdmz666666 于 2021-4-30 23:33 编辑

<p style="text-indent:32.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><font face="黑体">本章测评我将进行对</font>AT32F421内部比较器的测试,AT32F421内部有一个低功耗的比较器,可以作为独立器件(有IO口作为输入输出口),也可以和定时器进行联调.比较器的输入还可以使用内部的参照电压,这样就比较方便.同时,比较器支持可编程的迟滞,消隐输出,还可以控制工作的速度和功耗.</span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><font face="黑体">在这里</font>,我将测试比较器的模拟信号调理功能,分别用内部参照电压和外部IO口(PA4)输入作为比较器的负极端,用PA1作为比较器的正极输入,用PA0作为比较器输出.</span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><font face="黑体">这里是参考官方提供的</font>BSP中AT_START_F421中的COMP中的COMP_Out示例.</span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><font face="黑体">首先</font>,在HARDWARE文件夹中新建COMP.c 及其对应的h头文件。然后在FWLIB中添加at32f4xx_comp.c、如下图所示</span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><font face="黑体">第二步</font>,在COMP.c编写COMP_Configuration函数,首先跟之前的外设使用一样,配置GPIO和对应的时钟,我们将PA1和PA4配置为模拟输入输出模式,将PA0复用为COMP_OUT,部分配置如下图所示,</span></span></span></span></p>

<p></p>

<p style="text-indent:0.0000pt; text-align:left"><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><font face="黑体">接下来</font>,进行比较器的配置,首先,通过以下语句来使能比较器的时钟</span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:黑体"><span style="color:#00b0f0">RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_SYSCFGCOMP, ENABLE);</span></span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><font face="黑体">如果要将内部参照电压作为比较器负极输入</font>,添加以下代码,同时AT32F421还支持1/4、2/4 、3/4倍参照电压输入</span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:黑体"><span style="color:#00b0f0">COMP_InitStructure.COMP_INMInput = COMP_INMInput_VREFINT;</span></span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><font face="黑体">如果要将</font>IO外部电压作为比较器负极输入,添加以下代码,这里我选择PA4作为负极端,添加以下代码</span></span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:黑体"><span style="color:#00b0f0">COMP_InitStructure.COMP_INMInput = COMP_INMInput_IN1;//PA4</span></span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><span style="color:#ff0000"><font face="黑体">这里要说明一点,内部输入和外部输入只能二选一。</font></span></span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><span style="color:#000000"><font face="黑体">因为</font>AT32F421比较器的输出不仅可以从IO口输入,还可以输入到定时器,但作为模拟信号调理,我这里配置为不输出到其他外设,见以下代码</span></span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:黑体"><span style="color:#00b0f0">COMP_InitStructure.COMP_Output = COMP_Output_None;</span></span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><span style="color:#000000"><font face="黑体">同时在实际测试过程中,我发现在模拟信号调理时,开启迟滞功能和不开启输出效果差距很多(当然了,可能是我的信号源问题)</font></span></span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:黑体"><span style="color:#00b0f0">COMP_InitStructure.COMP_Hysteresis = COMP_Hysteresis_High;</span></span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><span style="color:#000000"><font face="黑体">上面这句是开启高度迟滞,</font></span></span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:黑体"><span style="color:#00b0f0">COMP_InitStructure.COMP_Hysteresis =</span></span></span><span style="font-size:12.0000pt"><span style="font-family:黑体"><span style="color:#00b0f0">COMP_Hysteresis_No</span></span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><font face="黑体">上面这句是不开启迟滞</font></span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><font face="黑体">接下来,我会对比这两者的区别</font></span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><font face="黑体">具体的代码请参考我的附件,最后在</font>main函数里调用COMP_Configuration();就可以使用比较器功能。</span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><font face="黑体">接下来,我将用示波器,信号发生器和万用表来进行测试比较器的实际效果。</font></span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><font face="黑体">我先用单片机</font></span></span><span style="font-size:16.0000pt"><span style="font-family:黑体"><span style="color:#ff0000"><font face="黑体">内部参照电压</font></span></span></span><span style="font-size:16.0000pt"><span style="font-family:黑体"><font face="黑体">作为比较器负极输入,同时</font></span></span><span style="font-size:16.0000pt"><span style="font-family:黑体"><span style="color:#ff0000"><font face="黑体">不开启迟滞</font></span></span></span><span style="font-size:16.0000pt"><span style="font-family:黑体"><font face="黑体">,可以观察到,在正极输入</font>1KHZ的0-3.3V的正弦波时,比较器将输出0-3.3V的方波,正好可以证明其是轨到轨的,因为可以输出的高电平是工作电压;同时,通过示波器的光标进行测量,可以看到比较器是时1.2V时进行翻转的,因为通过之前的实验和数据手册展示,单片机的内部参照电压为1.2V左右,实验现象符合预期,就是当正极小于1.2V时,比较器输出低电平,高于1.2V,输出高电平.</span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><span style="color:#000000"><font face="黑体">但是将波形放大</font>,会发现比较器在1.2V附近会反复比较,翻转,造成了实际波形的上升沿有多种波形重合,造成这种现象的原因,我以为一方面可能是我输入的波形不是理想的,存在一些毛刺,另一方面是单片机自身的原因,因为参照电压不是一个恒定值,它也是会变化的,同时比较器自身也是会存在一定的误差.</span></span></span></span></span></span></p>

<p></p>

<p><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><span style="color:#000000"><font face="黑体">为了减少这种反复比较的波形产生</font>,我开启了比较器的高度迟滞,果然,波形变的干净了不少</span></span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><span style="color:#000000"><font face="黑体">最后我测试了一下外部</font>IO口作为比较器的负极输入端,我在PA4上输入电压2.000V,实际效果如下</span></span></span></span></span></span></p>

<p></p>

<p><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><span style="color:#000000"><font face="黑体">通过改变外部输入电压的值</font>,可以改变输出方波的占空比,接下来,通过一个gif</span></span></span></span></span></span><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:16.0000pt"><span style="font-family:黑体"><span style="color:#000000">来进行展示,</span></span></span></span></span></span></p>

<p></p>

<p>&nbsp;</p>

<p>&nbsp;</p>

Jacktang 发表于 2021-5-1 17:52

<p>COMP比较器使用,看示波器波形测评很完美</p>

<p>期待下来其他项目测试</p>
页: [1]
查看完整版本: 【AT32F421测评】+ COMP比较器使用