/** * @brief Configures COMP2: PA3 as COMP2 non inverting input * VREFINT as COMP2 inverting input * and COMP2 output to TIM2 BKIN. * @param None * @retval None */ void COMP_Config(void) { COMP_InitTypeDef COMP_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* GPIOA Peripheral clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* Configure PA3 in analog mode: PA3 is connected to COMP2 non inverting input */ GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); /* COMP Peripheral clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); /* COMP2 config */ COMP_StructInit(&COMP_InitStructure); COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_VREFINT; COMP_InitStructure.COMP_Output = COMP_Output_TIM1BKIN; COMP_InitStructure.COMP_Hysteresis = COMP_Hysteresis_No; COMP_InitStructure.COMP_Mode = COMP_Mode_UltraLowPower; COMP_InitStructure.COMP_OutputPol = COMP_OutputPol_NonInverted; COMP_Init(COMP_Selection_COMP2, &COMP_InitStructure); /* Enable COMP2 */ COMP_Cmd(COMP_Selection_COMP2, ENABLE); }
/** * @brief Configures COMP1: DAC channel 1 to COMP1 inverting input * and COMP1 output to TIM2 IC4. * @param None * @retval None */ void COMP_Config(void) { /* Init Structure definition */ COMP_InitTypeDef COMP_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* GPIOA Peripheral clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* Configure PA1: PA1 is used as COMP1 non inveting input */ GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); /* COMP Peripheral clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); /* COMP1 Init: DAC1 output is used COMP1 inverting input */ COMP_StructInit(&COMP_InitStructure); COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_DAC1; /* Redirect COMP1 output to TIM2 Input capture 4 */ COMP_InitStructure.COMP_Output = COMP_Output_TIM2IC4; COMP_InitStructure.COMP_Mode = COMP_Mode_HighSpeed; COMP_InitStructure.COMP_Hysteresis = COMP_Hysteresis_No; COMP_Init(COMP_Selection_COMP1, &COMP_InitStructure); /* Enable COMP1 */ COMP_Cmd(COMP_Selection_COMP1, ENABLE); }
void PowerMonitor_Init(void) { uint32_t temp32u; COMP_InitTypeDef COMP_InitStructure; COMP_CVRefInitTypeDef COMP_CVRefInitStructure; PORT_InitTypeDef PORT_InitStructure; COMP_StructInit(&COMP_InitStructure); COMP_InitStructure.COMP_PlusInputSource = COMP_PlusInput_CVREF; COMP_InitStructure.COMP_MinusInputSource = COMP_MinusInput_IN1; COMP_Init(&COMP_InitStructure); COMP_CVRefStructInit(&COMP_CVRefInitStructure); COMP_CVRefInitStructure.COMP_CVRefSource = COMP_CVREF_SOURCE_AVdd; COMP_CVRefInitStructure.COMP_CVRefRange = COMP_CVREF_RANGE_Up; COMP_CVRefInitStructure.COMP_CVRefScale = COMP_CVREF_SCALE_14_div_32; COMP_CVRefInit(&COMP_CVRefInitStructure); COMP_Cmd(ENABLE); COMP_CVRefCmd(ENABLE); while(COMP_GetCfgFlagStatus(COMP_CFG_FLAG_READY) != SET); // Setup GPIO PORT_StructInit(&PORT_InitStructure); PORT_InitStructure.PORT_Pin = (1 << POWER_MONITOR_PIN); PORT_Init(POWER_MONITOR_PORT, &PORT_InitStructure); // Wait until power supply is stable temp32u = DWT_StartDelayUs(50000); while (DWT_DelayInProgress(temp32u)) { if (COMP_GetFlagStatus(COMP_STATUS_FLAG_SY) == SET) { // False triggering - restart delay temp32u = DWT_StartDelayUs(50000); } } // Read and clear comparator result latch COMP_GetResultLatch(); COMP_ITConfig(ENABLE); NVIC_EnableIRQ(COMPARATOR_IRQn); }
/** * @brief Configures COMP1: DAC channel 1 to COMP1 inverting input * and COMP1 output to TIM2 IC4. * @param None * @retval None */ static void COMP_Config(void) { /* Init Structure definition */ COMP_InitTypeDef COMP_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* GPIOA Peripheral clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* Init GPIO Init Structure */ GPIO_StructInit(&GPIO_InitStructure); /* Configure PA1: PA1 is used as COMP1 non inveting input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); /* COMP Peripheral clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); /* Init COMP init struct */ COMP_StructInit(&COMP_InitStructure); /* COMP1 Init: PA1 is used as COMP1 non-inverting input */ COMP_InitStructure.COMP_NonInvertingInput = COMP_NonInvertingInput_IO1; /* DAC1 output is as used COMP1 inverting input */ COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_DAC1; /* Redirect COMP1 output to TIM2 Input capture 4 */ COMP_InitStructure.COMP_Output = COMP_Output_TIM2IC4; COMP_InitStructure.COMP_OutputPol = COMP_OutputPol_NonInverted; COMP_InitStructure.COMP_BlankingSrce = COMP_BlankingSrce_None; COMP_InitStructure.COMP_Hysteresis = COMP_Hysteresis_High; COMP_InitStructure.COMP_Mode = COMP_Mode_UltraLowPower; COMP_Init(COMP_Selection_COMP1, &COMP_InitStructure); /* Enable COMP1 */ COMP_Cmd(COMP_Selection_COMP1, ENABLE); }
/** * @brief Configure COMP1 and COMP2 with interrupt * @param None * @retval None */ static void COMP_Config(void) { COMP_InitTypeDef COMP_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* GPIOA Peripheral clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* Configure PA1: PA1 is used as COMP1 and COMP2 non inveting input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); /* COMP Peripheral clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); /* COMP1 Init: the higher threshold is set to VREFINT ~ 1.22V but can be changed to other available possibilities */ COMP_StructInit(&COMP_InitStructure); COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_VREFINT; COMP_InitStructure.COMP_Output = COMP_Output_None; COMP_InitStructure.COMP_Mode = COMP_Mode_LowPower; COMP_InitStructure.COMP_Hysteresis = COMP_Hysteresis_High; COMP_Init(COMP_Selection_COMP1, &COMP_InitStructure); /* COMP2 Init: the lower threshold is set to VREFINT/4 ~ 1.22 / 4 ~ 0.305 V but can be changed to other available possibilities */ COMP_StructInit(&COMP_InitStructure); COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_1_4VREFINT; COMP_InitStructure.COMP_Output = COMP_Output_None; COMP_InitStructure.COMP_Mode = COMP_Mode_LowPower; COMP_InitStructure.COMP_Hysteresis = COMP_Hysteresis_High; COMP_Init(COMP_Selection_COMP2, &COMP_InitStructure); /* Enable Window mode */ COMP_WindowCmd(ENABLE); /* Enable COMP1: the higher threshold is set to VREFINT ~ 1.22 V */ COMP_Cmd(COMP_Selection_COMP1, ENABLE); /* Enable COMP2: the lower threshold is set to VREFINT/4 ~ 0.305 V */ COMP_Cmd(COMP_Selection_COMP2, ENABLE); /* Configure EXTI Line 21 in interrupt mode */ EXTI_InitStructure.EXTI_Line = EXTI_Line21; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* Configure EXTI Line 22 in interrupt mode */ EXTI_InitStructure.EXTI_Line = EXTI_Line22; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* Clear EXTI21 line */ EXTI_ClearITPendingBit(EXTI_Line21); /* Clear EXTI22 line */ EXTI_ClearITPendingBit(EXTI_Line22); /* Configure COMP IRQ */ NVIC_InitStructure.NVIC_IRQChannel = ADC1_COMP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }