/** * @brief This function handles COMP interrupt request. * @param None * @retval None */ void COMP_IRQHandler(void) { if(EXTI_GetITStatus(EXTI_Line22) != RESET) { if (COMP_GetOutputLevel(COMP_Selection_COMP2) == COMP_OutputLevel_High) { /* A rising edge is detected so the input voltage is over VREFINT and the ADC will be turned on for monitoring input voltage, the COMP2 will be off */ State = STATE_OVER_THRESHOLD; } EXTI_ClearITPendingBit(EXTI_Line22); } }
/** * @brief check input voltage level: within the thresholds, above the upper * threshold or under the lower threshold * @param None * @retval None */ void CheckState(void) { /* Check if COMP2 output level is high */ if ((COMP_GetOutputLevel(COMP_Selection_COMP1) == COMP_OutputLevel_High) && (COMP_GetOutputLevel(COMP_Selection_COMP2) == COMP_OutputLevel_High)) { /* A rising edge is detected so the input voltage is higher than VREFINT */ State = STATE_OVER_THRESHOLD; } else if ((COMP_GetOutputLevel(COMP_Selection_COMP1) == COMP_OutputLevel_Low) && (COMP_GetOutputLevel(COMP_Selection_COMP2) == COMP_OutputLevel_High)) { /* A falling edge is detected so the input voltage is lower than VREFINT */ State = STATE_WITHIN_THRESHOLD; } else if ((COMP_GetOutputLevel(COMP_Selection_COMP1) == COMP_OutputLevel_Low) && (COMP_GetOutputLevel(COMP_Selection_COMP2) == COMP_OutputLevel_Low)) { State = STATE_UNDER_THRESHOLD; } }
/** * @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 */ uint32_t index = 0; /* Configure all GPIO pins in Analog mode for lowsest consumption */ GPIO_Config(); /* ADC configuration: Channel 18 or 31 (PB12 or PF10) is used, End Of Conversion (EOC) interrupt is enabled */ ADC_Config(); #ifdef USE_STM32L152_EVAL /* LCD GLASS Configuration: LSI as LCD clock source */ LCD_Glass_Config(); /* Initialize the TFT-LCD */ STM32L152_LCD_Init(); #elif defined USE_STM32L152D_EVAL /* Initialize the TFT-LCD */ STM32L152D_LCD_Init(); #endif /* Clear the TFT-LCD */ LCD_Clear(LCD_COLOR_WHITE); while(1) { if (State == STATE_OVER_THRESHOLD) /* Input voltage is over the threshold */ { /* Indicator LED: MCU in RUN mode */ STM_EVAL_LEDOff(LED1); /* Disable COMP IRQ */ NVIC_InitStructure.NVIC_IRQChannel = COMP_IRQn; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE; NVIC_Init(&NVIC_InitStructure); /* Enable ADC1 IRQ */ NVIC_InitStructure.NVIC_IRQChannel = ADC1_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* COMP clock disable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_COMP, DISABLE); /* Restore MCU configuration */ RestoreConfiguration(); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConv(ADC1); /* Wait for ADC to be ready */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS)); while(State == STATE_OVER_THRESHOLD) { /* Display measured value on Glass LCD */ DisplayVoltage(ADCVal); /* Display measured value on LCD */ for (index = 0; index < 20; index++) { LCD_DisplayChar(LCD_LINE_3, (319 - (16 * index)), VoltageDisplay[index]); } /* Check if the measured value is below the threshold VREFINT: 1.22 V */ if (ADCVal <= 0x000005EA) { State = STATE_UNDER_THRESHOLD; } } } else /* Input voltage is under the threshold */ { /* LED1 ON: MCU in STOP mode */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDOn(LED1); /* Disable ADC1 IRQ */ NVIC_InitStructure.NVIC_IRQChannel = ADC1_IRQn; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE; NVIC_Init(&NVIC_InitStructure); /* Disable ADC1 */ ADC_Cmd(ADC1, DISABLE); /* Configure COMP2 with interrupt enabled */ COMP_Config(); /* Check COMP2 output level before entering STOP mode */ if (COMP_GetOutputLevel(COMP_Selection_COMP2) == COMP_OutputLevel_Low) { /* Disable LSI oscillator before entering STOP mode */ RCC_LSICmd(DISABLE); /* Enter STOP mode with regulator in low power */ PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI); } } } }
/** * @brief Main program. * @param None * @retval None */ void main(void) { /*************** Initialize LEDs available on STM8L15X-EVAL board ***********/ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); /* CLK configuration -------------------------------------------*/ CLK_Config(); /* ADC configuration -------------------------------------------*/ ADC_Config(); /* COMP configuration -------------------------------------------*/ COMP_Config(); while (1) { if (State != STATE_UNDER_THRESHOLD) /* Input voltage is over the threshold VREFINT */ { /* LD1 ON and LD2 OFF: MCU in run mode */ STM_EVAL_LEDOn(LED1); STM_EVAL_LEDOff(LED2); /* Disable global Interrupts */ disableInterrupts(); /* Disable COMP clock */ CLK_PeripheralClockConfig(CLK_Peripheral_COMP, DISABLE); /* Enable ADC1 clock */ CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE); /* Enable end of conversion ADC1 Interrupt */ ADC_ITConfig(ADC1, ADC_IT_EOC, DISABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConv(ADC1); /* Wait for first end of conversion */ while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); ADCVal = ADC_GetConversionValue(ADC1); /* Enable global Interrupts */ enableInterrupts(); /* Enable end of conversion ADC1 Interrupt */ ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE); while (State == STATE_OVER_THRESHOLD) {} } else /* Input voltage is under the threshold */ { /* LD1 OFF and LD2 ON: MCU in halt mode */ STM_EVAL_LEDOff(LED1); STM_EVAL_LEDOn(LED2); /* Disable global Interrupts */ disableInterrupts(); /* Clear EOC and OVR flags */ ADC_ClearFlag(ADC1, (ADC_FLAG_TypeDef) (ADC_FLAG_EOC | ADC_FLAG_OVER)); /* Disable ADC1 */ ADC_Cmd(ADC1, DISABLE); /* Disable ADC1 clock */ CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, DISABLE); /* Enable COMP clock */ CLK_PeripheralClockConfig(CLK_Peripheral_COMP, ENABLE); /* Enable COMP2 Interrupt */ COMP_ITConfig(COMP_Selection_COMP2, ENABLE); /* Enable global Interrupts */ enableInterrupts(); /* Check COMP2 output level before entering halt mode */ if (COMP_GetOutputLevel(COMP_Selection_COMP2) == COMP_OutputLevel_Low) { /* Enter halt mode */ halt(); } } } }
/** * @brief Main program. * @param None * @retval None */ void COMP_LDR_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_stm32f30x.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f30x.c file */ uint8_t ldrlevel = 11, ldrlevelp = 0, daclevel = 0; uint16_t tmp = 0; /* Initialize the TFT-LCD */ STM32303C_LCD_Init(); /* Clear the LCD */ LCD_Clear(LCD_COLOR_BLACK); /* Set the LCD Back Color */ LCD_SetBackColor(LCD_COLOR_BLACK); /* Set the LCD Text Color */ LCD_SetTextColor(LCD_COLOR_WHITE); /* Displays Light dependent resistor (LDR) message on line 0 */ LCD_DisplayStringLine(LINE(0), (uint8_t *)MESSAGE1); /* COMP Configuration */ COMP_Config(); /* DAC channel 1 Configuration */ DAC_Config(); /* Infinite loop */ while (1) { for(daclevel = 0; daclevel < 11; daclevel++) { /* Set DAC Channel1 DHR register */ DAC_SetChannel1Data(DAC_Align_12b_R, (uint16_t)(daclevel * 150)); Delay((0xFFFF)); /* Check on the Comp output level*/ if (COMP_GetOutputLevel(COMP_Selection_COMP7) == COMP_OutputLevel_High) { ldrlevel--; } } switch(ldrlevel) { case 1: /* Displays MESSAGE on line 7 */ LCD_DisplayStringLine(LINE(7), (uint8_t *)" Level 0 "); break; case 2: /* Displays MESSAGE on line 7 */ LCD_DisplayStringLine(LINE(7), (uint8_t *)" Level 1 "); break; case 3: /* Displays MESSAGE on line 7 */ LCD_DisplayStringLine(LINE(7), (uint8_t *)" Level 2 "); break; case 4: /* Displays MESSAGE on line 7 */ LCD_DisplayStringLine(LINE(7), (uint8_t *)" Level 3 "); break; case 5: /* Displays MESSAGE on line 7 */ LCD_DisplayStringLine(LINE(7), (uint8_t *)" Level 4 "); break; case 6: /* Displays MESSAGE on line 7 */ LCD_DisplayStringLine(LINE(7), (uint8_t *)" Level 5 "); break; case 7: /* Displays MESSAGE on line 7 */ LCD_DisplayStringLine(LINE(7), (uint8_t *)" Level 6 "); break; case 8: /* Displays MESSAGE on line 7 */ LCD_DisplayStringLine(LINE(7), (uint8_t *)" Level 7 "); break; case 9: /* Displays MESSAGE on line 7 */ LCD_DisplayStringLine(LINE(7), (uint8_t *)" Level 8 "); break; case 10: /* Displays MESSAGE on line 7 */ LCD_DisplayStringLine(LINE(7), (uint8_t *)" Level 9 "); break; case 11: /* Displays MESSAGE on line 7 */ LCD_DisplayStringLine(LINE(7), (uint8_t *)" Level 10 "); break; default : /* Displays MESSAGE on line 7 */ LCD_DisplayStringLine(LINE(7), (uint8_t *)" Level 0 "); ldrlevel = 1; break; } if(ldrlevelp != ldrlevel) { /* Set the LCD Text Color */ LCD_SetTextColor(LCD_COLOR_WHITE); /* Displays a rectangle on the LCD */ LCD_DrawRect(199, 311, 22, 302 ); /* Set the LCD Back Color */ LCD_SetBackColor(LCD_COLOR_BLACK); LCD_SetTextColor(LCD_COLOR_BLACK); LCD_DrawFullRect(200, 310, 300, 20); /* Set the LCD Text Color */ LCD_SetTextColor(LCD_COLOR_YELLOW); /* Set the LCD Back Color */ LCD_SetBackColor(LCD_COLOR_YELLOW); /* Displays a full rectangle */ tmp = 30 * (ldrlevel-1); if (tmp ==0) tmp = 5; LCD_DrawFullRect(200, 310, tmp , 20); } ldrlevelp = ldrlevel; ldrlevel = 11; /* Set the LCD Back Color */ LCD_SetBackColor(LCD_COLOR_BLACK); } }