/** * @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 Configure DAC peripheral * @param None * @retval None */ static void DAC_Config(void) { /* Init DAC channel 1 */ DAC_Init(DAC_Channel_1, DAC_Trigger_None, DAC_OutputBuffer_Disable); /* Enable DAC channel 1 */ DAC_Cmd(DAC_Channel_1, ENABLE); /* Set DAC Channel1 DHR register: * DAC1 output = VREF * DOR / 256 = 3.3 * 116 / 256 = 1.5 V */ DAC_SetChannel1Data(DAC_Align_8b_R, 116); /* Close I/O Switch 24 to select PE5 as comparator 2 non inverting input: * External signal should be connected to PE5 */ SYSCFG_RIIOSwitchConfig(RI_IOSwitch_24, ENABLE); }
/** * @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 Comparator peripheral * @param None * @retval None */ static void COMP_Config(void) { /* Connect internal reference voltage to COMP1 inverting input */ COMP_VrefintToCOMP1Connect(ENABLE); /* close the analog switch number 14 */ SYSCFG_RIAnalogSwitchConfig(RI_AnalogSwitch_14, ENABLE); /* close the analog switch number 1 */ SYSCFG_RIAnalogSwitchConfig(RI_AnalogSwitch_1, ENABLE); /* close the I/O switch number 4 */ SYSCFG_RIIOSwitchConfig(RI_IOSwitch_4, ENABLE); /* Enable COMP1 Interrupt */ COMP_ITConfig(COMP_Selection_COMP1, ENABLE); /* Configure the event detection */ COMP_EdgeConfig(COMP_Selection_COMP1, COMP_Edge_Rising); }
/** * @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) { } }