cancel
Showing results for 
Search instead for 
Did you mean: 

stm32c011

SRAM.11
Associate III

please let me know my coding is correct ,whether usage of my exti is correct 

  while (1)
  {
	  HAL_Delay(300);
	  	  //  MAINS INPUT
	  	  	  Adc_Mains();
	  	  	  HAL_ADC_Start(&hadc1);
	  	  	  HAL_ADC_PollForConversion(&hadc1,1000);
	  	  	  Mains=HAL_ADC_GetValue(&hadc1);
	  	  	  Mains=Mains*2/4095;
	  	  	  HAL_ADC_Stop(&hadc1);
	           HAL_Delay(100);

	       //Battery operation
	           Adc_Vbat();
	            HAL_ADC_Start(&hadc1);
	            HAL_ADC_PollForConversion(&hadc1,1000);
	            Vbat=HAL_ADC_GetValue(&hadc1);
	            Vbat=Vbat*2/4095;
	            HAL_ADC_Stop(&hadc1);
	            HAL_Delay(100);

	       //OPEN CIRCUIT DETECT
	        Adc_OCD();
	  	  	HAL_ADC_Start(&hadc1);
	  	    HAL_ADC_PollForConversion(&hadc1,1000);
	  	    OCD=HAL_ADC_GetValue(&hadc1);
	  	    OCD=OCD*2/4095;
	  	    HAL_ADC_Stop(&hadc1);
	  	    HAL_Delay(100);
	  	  //BATTERY TEMPARATURE
	  	    		Adc_Temp();
	  	  	  	  	HAL_ADC_Start(&hadc1);
	  	  	  	    HAL_ADC_PollForConversion(&hadc1,1000);
	  	  	  	    Temp=HAL_ADC_GetValue(&hadc1);
	  	  	  	    Temp=Temp*2/4095;
	  	  	  	    HAL_ADC_Stop(&hadc1);
	  	  	  	    HAL_Delay(100);

	  	  	  	// Check mains and mains fail

	  	  	  	if(Mains>0.1)
	  	  	  	{
	  	  	  		   if((Mains<1) &&(Vbat>0.9))
	  	  	  		   	   {
	  	  	  			   	   En_Emergency(1);
	  	  	  			   	Set_LED(0);

	  	  	  		   	   }

	  	  	  		   else
	  	  	  		   	   {
	  	  	  			   En_Emergency(0);
	  	  	  		   	   }
	  	  	  	 mainsOffTimer = 0;
	  	  	  	}
	  	  	//   MAINS ON
	  	  	if(Mains>1.2)
	  	  	{
	  	  		if(Vbat>1.5 || Vbat<0.5)     //OPEN CIRCUIT OR ZERO BATT LEVEL
	  	  				{
	  	  				Set_LED(0);
	  	  				Batt_Chrge(1);
	  	  				}
	  	  		else if(Vbat <0.94 )     // START CHARGING LEVEL
	  	  				{
	  	  				Set_LED(1);
	  	  				Batt_Chrge(1);
	  	  				}
	  	  		else if(Vbat >0.98 )   // STOP CHARGING LEVEL
	  	  				{
	  	  				Set_LED(1);
	  	  				Batt_Chrge(0);

	  	  				}
	  	  		//BATTERY TEMPARATURE CHECK

	  	  	 if(Temp<0.17  || Temp>0.83)
	  	  	 {
	  	  		Batt_Chrge(0);
	  	  		Triple_Flash();
	  	  	 }
	  	  	 else if((Temp>0.17  || Temp<0.83))
	  	  	 {
	  	  		Batt_Chrge(1);

	  	  	 }
	  	  	}
	  	  	if(Mains<0.1)
	  	  	{
	  	  	 mainsOffTimer++;

	  	  	            // Enter Shutdown mode if mains is off for 30 seconds
	  	  	            if (mainsOffTimer >= 300)
	  	  	            {
	  	  	                // Shutdown mode
	  	  	                // Disable EXTI for PA2 to prevent immediate wake-up
	  	  	                HAL_NVIC_DisableIRQ(EXTI2_3_IRQn);

	  	  	                // Enter deep sleep mode
	  	  	                SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
	  	  	                __WFI();

	  	  	                // Re-enable EXTI for PA2 after wake-up
	  	  	                HAL_NVIC_EnableIRQ(EXTI2_3_IRQn);
	  	  	            }
	  	  	}


    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
*
*
*
**
*
**
**
**
**
*
  * @brief GPIO Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, EMERGENCY_Pin|LED_Pin|BATTCHRG_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin : PA2 */
  GPIO_InitStruct.Pin = GPIO_PIN_2;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pins : EMERGENCY_Pin LED_Pin BATTCHRG_Pin */
  GPIO_InitStruct.Pin = EMERGENCY_Pin|LED_Pin|BATTCHRG_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI2_3_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI2_3_IRQn);

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @PAram  file: pointer to the source file name
  * @PAram  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
0 REPLIES 0