/** * @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) { } }