Comparator Test
As the name suggests, a comparator is used for comparison. It has two input terminals, IN+ and IN-. One of the input terminals can be selected as a reference point for comparison. When the voltage of the other input terminal is less than the reference voltage, the comparator outputs a low level, otherwise it outputs a high level.
32A455 internal comparator, IN- can connect 2 64-level programmable comparison voltage reference sources, which is very convenient.
The typical application of a comparator is analog voltage detection. When the input voltage exceeds the set threshold, it outputs a signal and performs corresponding operations, such as overvoltage and overcurrent functions of motor control.
Routine Function
We're going to change this routine.
PB10 is used as RGB LED, we need to change it to PA1, INM test we use the internal reference source
Code Adaptation
#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. I changed two lines of code
COMP_Initial.InpSel = COMP1_CTRL_INPSEL_PA1;
COMP_Initial.InmSel = COMP1_CTRL_INMSEL_VERF1;
2. Added internal reference source settings
COMP_SetRefScl(0,DISABLE,21,ENABLE);
3. Changed the GPIO configuration, PB10 should be set to input or open drain
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);
}
Experimental results, consistent with the function