/** * @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 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 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); }
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); }
/**************************************************************** * 函数名称: * 功 能: 2015-4-28新编中断服务函数。 * 2016-06-08 lea比较其开始后捕获第二次中断为相位起始和结束标志 * 当A相电压大于100V,在开启了比较器、输入捕获之后,捕获到A相经过比较器之后的上升沿,则定时器开始计数。 关闭比较器,使无比较输出,即无捕获输入。 * 这时,同时需要判断再次检测到的电压要大于100V,再开启捕获功能,记录下再次捕获到的另一相的上升沿时的计数值。 * 供相序判断。 * 参 数: 无 * 返 回 值: 无 *****************************************************************/ void TIM5_IRQHandler(void) { if(phasestatus != state0)//相序测量状态下比较用于触发相序的时间间隔测量 { if((TIM5CH4_CAPTURE_STA&0x8000)==0)//还未成功捕获A相与B相orC相之间的时间 { // if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET) // { // if(TIM5CH4_CAPTURE_STA&0x4000)//已经捕获到A相的上升沿了 // { // if((TIM5CH4_CAPTURE_STA&0x3FFF)==0x3FFF)//两次捕获A相与B相或C相的时间太长了,0x3F=4.194304s(1MHz计数0xFFFF次,再累加0x3F次) // { // TIM5CH4_CAPTURE_STA|=0x8000;//标记成功捕获了一次 // TIM5CH4_CAPTURE_VAL=0xFFFF; // // COMP_Cmd(COMP_Selection_COMP1, DISABLE);//关闭比较器 // } // else TIM5CH4_CAPTURE_STA++; // } // } if(TIM_GetITStatus(TIM5, TIM_IT_CC4) != RESET)//捕获4发生捕获事件 { if(TIM5CH4_CAPTURE_STA&0x4000) //已经捕获到一次,再次捕获到一个上升沿 { /**************测试两次读取判断第二相线频率脉冲计数个数*/ //20160607lea 两相线上的起始脉冲均从第二个开始,因为刚打开比较器时若此时输入脉冲处于正半周则会立即触发输入捕捉中断 //所以从第二个脉冲开始判定。相序测量功能 相位差的测量基本正确。 if(capstats==0) { capstats=1; } else if(capstats==1) { TIM2_cap1 = TIM2->CNT; TIM5_cap1 = TIM_GetCapture4(TIM5); capstats=2; } else { TIM5CH4_CAPTURE_VAL = TIM_GetCapture4(TIM5); TIM5_cap2 = TIM5CH4_CAPTURE_VAL; TIM_Cmd(TIM5, DISABLE); TIM2_cap2 = TIM2->CNT;//TIM_GetCapture4(TIM2); TIM_Cmd(TIM2, DISABLE); TIM5CH4_CAPTURE_STA|=0x8000; //标记成功捕获到B或C相的上升沿 COMP_Cmd(COMP_Selection_COMP1, DISABLE);//关闭比较器 TIM_Cmd(TIM14, DISABLE);//0.5s定时关闭 TIM5_cap3=TIM5_cap2-TIM5_cap1; //一个周期的计数个数 用于对总计数个数取余后求相位差,确定第二相相序 CCR4_Val = TIM5_cap3; TIM2_cap3=TIM2_cap2-TIM2_cap1; capstats=0; //BUZZER_Open(0);//蜂鸣一声 } /****************/ } else //已经检测到A相电压大于100V,开始比较器和定时器后,第一次捕获上升沿 { if(capstats==0) { capstats=1; } else { TIM5CH4_CAPTURE_STA=0; //清空,此标志除了用来0x4000,0x8000做A、B/C两次的标记外,还用来定时器溢出计时 TIM5CH4_CAPTURE_VAL=0; TIM_SetCounter(TIM5,0); TIM_SetCounter(TIM2,0); TIM5CH4_CAPTURE_STA|=0x4000; //标记捕获到了A相经过比较器之后的上升沿 TIM_Cmd(TIM5, ENABLE);//开启定时器计数 TIM_Cmd(TIM2, ENABLE);//测试 在捕捉到A相上升沿后打开,输出一个脉冲对比待测的相位差 COMP_Cmd(COMP_Selection_COMP1, DISABLE);//关闭比较器,使无比较输出,即无捕获输入。等待下次检测到100V以上电压,再开启 BUZZER_Open(0);//蜂鸣一声 capstats=0; } } } } } else//正常模式下,比较器用于过零点触发交流的采样 { if(TIM_GetITStatus(TIM5, TIM_IT_CC4) != RESET) { /* Clear TIM2 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM5, TIM_IT_CC4); if(VadENA!=1) { VadENA=1; TIM_Cmd(TIM19, ENABLE); //printf("*converting!*\r\n"); } } } TIM_ClearITPendingBit(TIM5, TIM_IT_CC4|TIM_IT_Update); //清除中断标志位 }
/** * @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); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32l1xx_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32l1xx.c file */ /* Initialize LED1 mounted on STM32L1XX-EVAL board */ STM_EVAL_LEDInit(LED1); /******************** comparator COMP1 configuration ************************/ #ifdef USE_STM32L152_EVAL /* GPIOB Peripheral clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); /* Configure PB12 in analog mode */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); #elif USE_STM32L152D_EVAL /* GPIOF Peripheral clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE); /* Configure PF10 in analog mode */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOF, &GPIO_InitStructure); #endif /* COMP Peripheral clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_COMP, ENABLE); /* Enable the Comparator 1 */ COMP_Cmd(ENABLE); /* Enable the switch control mode */ SYSCFG_RISwitchControlModeCmd(ENABLE); /* Close the ADC analog switch VCOMP */ SYSCFG_RIIOSwitchConfig(RI_IOSwitch_VCOMP, ENABLE); #ifdef USE_STM32L152_EVAL /* Close the I/O analog switch number 18 */ SYSCFG_RIIOSwitchConfig(RI_IOSwitch_CH18, ENABLE); #elif USE_STM32L152D_EVAL /* Close the I/O analog switch number 31 */ SYSCFG_RIIOSwitchConfig(RI_IOSwitch_CH31, ENABLE); #endif /* Configure and enable EXTI21 */ 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 and enable COMP interrupt */ 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); while (1) { } }
/** * @brief Main program * @param None * @retval None */ void COMP_Example(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32l1xx_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32l1xx.c file */ /* Initialize LED1 mounted on STM32L1XX-EVAL board */ GPIO_InitTypeDef GPIO_InitStructure; /* GPIOB Periph clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); /* Configure PD0 and PD1 or PD3 and PD7 in output pushpull mode */ GPIO_InitStructure.GPIO_Pin = LD_GREEN_GPIO_PIN | LD_BLUE_GPIO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); /******************** comparator COMP1 configuration ************************/ /* GPIOB Peripheral clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); /* Configure PB12 in analog mode */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; 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); /* Enable the Comparator 1 */ COMP_Cmd(ENABLE); /* Enable the switch control mode */ SYSCFG_RISwitchControlModeCmd(ENABLE); /* Close the ADC analog switch VCOMP */ SYSCFG_RIIOSwitchConfig(RI_IOSwitch_VCOMP, ENABLE); /* Close the I/O analog switch number 18 */ SYSCFG_RIIOSwitchConfig(RI_IOSwitch_CH18, ENABLE); /* Configure and enable EXTI21 */ 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 and enable COMP interrupt */ 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); while (1) { } }