Skip to main content
Associate III
August 1, 2023
Solved

stm32c011f6p6

  • August 1, 2023
  • 8 replies
  • 4995 views

I needed a help how to implement shutdown mode and wakeup from external reset in my application

 

 

 while (1)
 {
	 HAL_Delay(100);
	 	 // MAINS INPUT
	 	 	 Adc_Mains();
	 	 	 HAL_ADC_Start(&hadc1);
	 	 	 HAL_ADC_PollForConversion(&hadc1,1000);
	 	 	 Mains=HAL_ADC_GetValue(&hadc1);
	 	 	 Hands=Hands*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);
	 	 	 		 	 }

	 	 	 	}
	 	 	if(Mains<1)
	 	 	{
	 	 		 // OCD=HAL_ADC_GetValue(&hadc1);
	 	 		 if(OCD>0.3||OCD<0.6)
	 	 		 {

	 	 			Set_LED(0);

	 	 		 }
	 	 		 else if(OCD<0.3 || OCD>0.6)
	 	 			{

	 	 		Double_Flash();
	 	 			}
	 	 	 }

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

	 	 				}
	 	 		//BATTERY TEMPARATURE CHECK

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

	 	 	 }
	 	 	}

 

 

.if my mains is off for 30 seconds it should be in shutdown mode.I have a option using wakeup4 pin but my config is not working.Can anyone pls give an idea for the implementation 

This topic has been closed for replies.
Best answer by Sarra.S

Hello @meena

Configure wkup4 pin as EXTI and connect it to an external interrupt source (ex: button or sensor) 

after enabling the wakeup, test if mains off (exp: mains<0.1)

 

 

 if (Mains < 0.1) {
 HAL_Delay(30000); // Wait for 30 seconds
 if (Mains < 0.1) {
 // Mains is still off, enter shutdown mode
 HAL_PWR_EnterShutdownMode();
 }

 

 

 

8 replies

Sarra.SBest answer
ST Employee
August 1, 2023

Hello @meena

Configure wkup4 pin as EXTI and connect it to an external interrupt source (ex: button or sensor) 

after enabling the wakeup, test if mains off (exp: mains<0.1)

 

 

 if (Mains < 0.1) {
 HAL_Delay(30000); // Wait for 30 seconds
 if (Mains < 0.1) {
 // Mains is still off, enter shutdown mode
 HAL_PWR_EnterShutdownMode();
 }

 

 

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
meenaAuthor
Associate III
August 1, 2023

hi this worked thank you so much 

if (Mains < 0.1) {
 HAL_Delay(30000); // Wait for 30 seconds
 if (Mains < 0.1) {
 // Mains is still off, enter shutdown mode
 HAL_PWREx_EnterSHUTDOWNMode();
 }
meenaAuthor
Associate III
August 1, 2023

hi thanks for the reply I have this but this is entirely making my module shutdown its not waiting for 30 seconds

ST Employee
August 1, 2023

Hello @meena

yes right the HAL_Delay() will block your code,  instead, the best approach is to use a timer that interrupts every 1s for example, in its callback, you read the value of mains for a period of 30s, once it reaches 30s, you set a flag, in while(1) loop you test on that flag and enter shutdown accordingly 

Is that somehow clear?

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
meenaAuthor
Associate III
August 3, 2023

hi I am trying to code it in a way ,but standby mode is working its not waking up as in my previous post but I have to the below one now its not going to standby mode .I couldnt find whats wrong

 __HAL_RCC_PWR_CLK_ENABLE();
 /* Check if the system was resumed from Standby mode */
 if (__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET)
 {
 /* Clear Standby flag */
 __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
 /* Check and Clear the Wakeup flag */
 if (__HAL_PWR_GET_FLAG(PWR_FLAG_WUF4) != RESET)
 {
 __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WUF4);
 }
 }

 //HAL_Delay(5000);

 count++;
 if(count>=30)
 {
 	 HAL_PWR_EnterSTANDBYMode();
 }

 HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN4_HIGH);
Piranha
Principal III
August 4, 2023
	 	 	 if(Temp<0.17 || Temp>0.83)
	 	 	 {
	 	 		Batt_Charge(0);
	 	 		Triple_Flash();
	 	 	 }
	 	 	 else if((Temp>0.17 || Temp<0.83))
	 	 	 {
	 	 		Batt_Charge(1);

	 	 	 }
  1. The second if condition should use && logical operator.
  2. The code will do nothing if the Temp is exactly 0.17 or 0.83. One of the if conditions should use >= and <= comparison operators.
  3. The second if condition is useless, because a simple else does what is necessary.
  4. The 0.17 and 0.83 constants are of a double (64-bit) type, not float (32-bit). Use the suffix f like 0.17f and 0.83f.
meenaAuthor
Associate III
August 7, 2023

thank you,Could you have any solution for exiting low power modes using wakeup pins