比较器测试
比较器顾名思义就是用来作比较的,有两个输入端IN+和IN-,可选择其中一个输入端作为参考点来比较,当另一输入端电压小于参考电压时比较器输出低电平,反之输出高电平。
32A455内部比较器,IN-可以接2个 64 级可编程的比较电压参考源,非常方便。
比较器的典型应用就是模拟电压的检测,当输入的电压超过设定的阈值时,输出信号,执行对应的操作,比如电机控制的过压,过流功能。
例程功能
这个例程我们要改一下
PB10用作了RGB 的LED,我们需要改一下那我们把它改成PA1,INM测我们使用内部参考源
代码适配
#include "main.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void COMP_Configuratoin(void);
void NVIC_Configuration(void);
void ChangeVmVp(void);
/**
* [url=home.php?mod=space&uid=159083]@brief[/url] Main program
*/
int main(void)
{
/* System clocks configuration ---------------------------------------------*/
RCC_Configuration();
/* NVIC configuration ------------------------------------------------------*/
NVIC_Configuration();
/* GPIO configuration ------------------------------------------------------*/
GPIO_Configuration();
/* COMP configuration ------------------------------------------------------*/
COMP_Configuratoin();
while (1)
{
}
}
/**
* @brief Self Generate Puls ,by skip line connect to vp and vm if need.
*/
void ChangeVmVp(void)
{
GPIO_SetBits(GPIOE, GPIO_PIN_2);
GPIO_ResetBits(GPIOE, GPIO_PIN_3);
{
uint32_t i = 0;
while (i++ < 1000)
;
}
GPIO_ResetBits(GPIOE, GPIO_PIN_2);
GPIO_SetBits(GPIOE, GPIO_PIN_3);
{
uint32_t i = 0;
while (i++ < 1000)
;
}
}
/**
* @brief Configures the comp module.
*/
void COMP_Configuratoin(void)
{
COMP_InitType COMP_Initial;
/*Set dac2,dac1. because dac1/PA4 is share pin line,so only PB0 puls 0/1, can find out puls*/
COMP_SetRefScl(16, true, 32, true);
/*Initial comp*/
COMP_StructInit(&COMP_Initial);
COMP_Initial.InpSel = COMP1_CTRL_INPSEL_PA1;
COMP_Initial.InmSel = COMP1_CTRL_INMSEL_VERF1;
COMP_Initial.SampWindow = 18; //(0~32)
COMP_Initial.Thresh = 30; //(0~32)
COMP_Init(COMP1, &COMP_Initial);
COMP_SetRefScl(0,DISABLE,21,ENABLE);
COMP_Enable(COMP1, ENABLE);
}
/**
* @brief Configures the different system clocks.
*/
void RCC_Configuration(void)
{
/* Enable COMPE clocks */
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_COMP | RCC_APB1_PERIPH_COMP_FILT, ENABLE);
/* Enable GPIOA, GPIOB, GPIOC and GPIOD clocks */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO | RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_GPIOC
| RCC_APB2_PERIPH_GPIOD | RCC_APB2_PERIPH_GPIOE | RCC_APB2_PERIPH_GPIOF,
ENABLE);
}
/**
* @brief Configures the different GPIO ports.
*/
void GPIO_Configuration(void)
{
GPIO_InitType GPIO_InitStructure;
// INP
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.Pin = GPIO_PIN_1;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.Pin = GPIO_PIN_10;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
// OutSel
// PB1 if remap [1:0] == 01 with comp1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.Pin = GPIO_PIN_1;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
SetBit(AFIO->RMP_CFG4, BIT0);
ClrBit(AFIO->RMP_CFG4, BIT1);
}
/**
* @brief Configures Vector Table base location.
*/
void NVIC_Configuration(void)
{
NVIC_InitType NVIC_InitStructure;
/* Configure and enable ADC interrupt */
NVIC_InitStructure.NVIC_IRQChannel = COMP_1_2_3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file pointer to the source file name
* @param line assert_param error line source number
*/
void assert_failed(const uint8_t* expr, const uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
1,我更改了两行代码
COMP_Initial.InpSel = COMP1_CTRL_INPSEL_PA1;
COMP_Initial.InmSel = COMP1_CTRL_INMSEL_VERF1;
2. 添加了内部参考源的设置
COMP_SetRefScl(0,DISABLE,21,ENABLE);
3. 更改了GPIO的配置,PB10要设置成输入或开漏
void GPIO_Configuration(void)
{
GPIO_InitType GPIO_InitStructure;
// INP
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.Pin = GPIO_PIN_1;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.Pin = GPIO_PIN_10;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
// OutSel
// PB1 if remap [1:0] == 01 with comp1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.Pin = GPIO_PIN_1;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
SetBit(AFIO->RMP_CFG4, BIT0);
ClrBit(AFIO->RMP_CFG4, BIT1);
}
实验结果,符合功能