Example #1
0
/**
  * @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);
}
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);
}
Example #3
0
/**
  * @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();
      }
    }
  }
}