/** * @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 COMP2 configuration. * @param None * @retval None */ void COMP_Config(void) { /* COMP Peripheral clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_COMP, ENABLE); /* SYSCFG Peripheral clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); /* Close the I/O analog switch number n */ SYSCFG_RIIOSwitchConfig(RI_IOSwitch_GR6_2, ENABLE); /* COMP2 Init: COMP2 is enabled as soon as inverting input is selected */ COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_VREFINT; COMP_InitStructure.COMP_OutputSelect = COMP_OutputSelect_None; COMP_InitStructure.COMP_Speed = COMP_Speed_Slow; COMP_Init(&COMP_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; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* Clear EXTI22 line */ EXTI_ClearITPendingBit(EXTI_Line22); /* Configure COMP IRQ */ NVIC_InitStructure.NVIC_IRQChannel = COMP_IRQn; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
/** * @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); }
/** * @brief COMP Configuration. * @param None * @retval None */ static void COMP_Config(void) { COMP_InitTypeDef COMP_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* GPIOA Peripheral clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* Configure PA0 in analog mode: PA0 is connected to COMP7 non inverting input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Enable SYSCFG clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); /* COMP7 Init:use DAC1 output as a reference voltage: DAC1 output is connected to COMP7 inverting input */ COMP_InitStructure.COMP_NonInvertingInput = COMP_NonInvertingInput_IO1; COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_DAC1; COMP_InitStructure.COMP_Output = COMP_Output_None; 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_COMP7, &COMP_InitStructure); /* Enable the COMP peripheral */ COMP_Cmd(COMP_Selection_COMP7, ENABLE); }
/** * @brief Configure COMP1 and COMP2 with interrupt * @param None * @retval None */ void COMP_Config(void) { COMP_InitTypeDef COMP_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* GPIOB Peripheral clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); /* Configure PB5 in analog closes the switch GR6-2: PB5 is connected to COMP2 non inverting input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); /* COMP Peripheral clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_COMP, ENABLE); /* COMP2 Init: COMP2 is enabled as soon as inverting input is selected */ /* In this example, the lower threshold is set to VREFINT/4 ~ 1.22 / 4 ~ 0.305 V but can be changed to other available possibilities */ COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_1_4VREFINT; COMP_InitStructure.COMP_OutputSelect = COMP_OutputSelect_None; COMP_InitStructure.COMP_Speed = COMP_Speed_Slow; COMP_Init(&COMP_InitStructure); /* Enable Window mode */ COMP_WindowCmd(ENABLE); /* Enable COMP1: the higher threshold is set to VREFINT ~ 1.22 V */ COMP_Cmd(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 = COMP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
/** * @brief Configure Comparator peripheral * @param None * @retval None */ static void COMP_Config(void) { /* Close I/O Switch 23 to select PD0 as comparator 2 non inverting input: External signal should be connected to PD0 */ SYSCFG_RIIOSwitchConfig(RI_IOSwitch_23, ENABLE); /* Init COMP2: VREFINT is used as COMP2 inverting input COMP2 output is connected to TIM2 input capture 2 (default configuration) COMP2 speed is fast */ COMP_Init(COMP_InvertingInput_VREFINT, COMP_OutputSelect_TIM2IC2, COMP_Speed_Fast); /* COMP2 edge detection: rising edge */ COMP_EdgeConfig(COMP_Selection_COMP2, COMP_Edge_Rising); }
/** * @brief Configure COMP peripheral * @param None * @retval None */ static void COMP_Config(void) { /* Init COMP2: DAC1 output is used as COMP2 inverting input COMP2 output is connected to TIM1 break COMP2 speed is fast */ /* If non inverting input exceeds inverting input (input voltage applyed on PE5 exceeds DAC1 output) COMP2 output is set at high level and since COMP2 output is connected to break input, the non-inverted channels and inverted channels of TIM1 switch to high level (TIM1_OCIdleState_Set, TIM1_OCNIdleState_Set). Once COMP2 output is at low level, the non-inverted channels and inverted channels return to PWM state at the next update (TIM1_AutomaticOutput_Enable).*/ COMP_Init(COMP_InvertingInput_DAC1, COMP_OutputSelect_TIM1BRK, COMP_Speed_Fast); /* COMP2 edge detection: rising edge */ COMP_EdgeConfig(COMP_Selection_COMP2, COMP_Edge_Rising); }
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); }