Skip to main content
Explorer
December 6, 2023
Question

Timer_PWM

  • December 6, 2023
  • 2 replies
  • 2966 views

Hi people,

I am looking for a help please

I am working on a boosting up a voltage and works in a closed control if it exceeds the Max_UC_volt the duty cycle should vary..

void run_ucboost(void)
	{
	uint16_t mainsfail=1;
	uint16_t uctimer = 0;
	uint16_t pwm_ucboost = 0;
		 uint16_t regulate = 0;

		/* mains off so boost */
		if(mainsfail == 1)
		{
			if(uctimer == 0)
			{
				/* start pwm at preset value*/
				if(regulate == 0)
				{
					pwm_ucboost = DEFAULT_UC_PWM;
					Start_UCboostPWM(pwm_ucboost);
					uctimer = UCSTART_TIME;
					regulate = 1;
				}
				/* regulate */
				else
				{
					if(Battmon > MAX_UC_VOLT)
					{
						if(pwm_ucboost != 0)
						{
							pwm_ucboost--;
						}
					}
					else if(Battmon < MIN_UC_VOLT)
					{
						if(MAX_UC_PWM > pwm_ucboost)
						{
							pwm_ucboost++;
						}
					}
					Play_boostPWM(pwm_ucboost);
				}

			}


			else
			{
				uctimer--;
			}
		}
		/* mains ok so stop PWM boost */
		else
		{
			Stop_UCboostPWM();
			regulate = 0;
			uctimer = 0;
		}
	}
void Start_UCboostPWM(uint16_t pwmvalue)
	{
	 TIM3->ARR=pwmvalue;
	 TIM3->CCR2= pwmvalue*0.2;
	 HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
	}
void Play_boostPWM(uint16_t boostval)
{

	TIM3->ARR=boostval;
		 TIM3->CCR2= boostval*0.2;
		 HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);

}
void Stop_UCboostPWM(void)
	{
	TIM3->CCR2 = 0;
	HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
	}

I am getting my boost voltage but duty cycle doesn't have the control  on loading.how can i use Set compare on this 

    This topic has been closed for replies.

    2 replies

    ST Employee
    December 6, 2023

    Hello @meena 
    Can you add breakpoints in your regulate loop and visualize the content of your register TIM3_CCR2 contains the new computation value that you apply in the Play_boostPWM() function?
    Also, restarting the TIM3 at each iteration (HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2)) is this really useful? You could simply update your duty cycle using the API, __HAL_TIM_SET_COMPARE() without restarting the TIM3.

    Regrads,

    Romain,

    meenaAuthor
    Explorer
    December 6, 2023

    if I use this I am getting an error undefined reference

     

    ST Employee
    December 6, 2023

    Without more detail, it will be difficult to help you.
    However, can you provide more informations related to your STM32G0 firmware environment?
    Do you use STM32Cube_G0_FW? do you include STM32 all used peripherals headers in your code such stm32g0xx_hal_tim.h?
    Are you working on custom board? Nucleo? Other...?

    Regards.

    Romain, 

    meenaAuthor
    Explorer
    December 6, 2023

    i am working on a prototype which uses stm32g030k8  

    meenaAuthor
    Explorer
    December 6, 2023

    I AM IN TRANSLATING STM8 CODE TO STM32 STM8 CODE IS HERE

    void run_uc_boost(void)
    	{
    		static uint8_t uctimer = 0;
    		static uint16_t pwm_ucboost = 0;
    		static bool regulate = FALSE;
    		
    		/* mains off so boost */
    		if(mainsfail == TRUE) 
    		{
    			if(uctimer == 0)
    			{
    				/* start pwm at preset value*/
    	void StartUCBoostPWM(uint16_t pwmval)
    	{
    		TIM1_SetCompare1(pwmval);
    		TIM1_CCxCmd(TIM1_CHANNEL_1, ENABLE);
    	}
    	
    	/**
    	
    	void PlayUCPWM(uint16_t pwmvl)
    	{
    		TIM1_SetCompare1(pwmvl);
    	}
    	
    	/**
    	**
    	void StopUCBoostPWM(void)
    	{
    		TIM1_SetCompare1(0);
    		TIM1_CCxCmd(TIM1_CHANNEL_1, DISABLE);
    	}
    	
    	
    				if(regulate == FALSE)
    				{
    					pwm_ucboost = DEFAULT_UC_PWM;
    					StartUCBoostPWM(pwm_ucboost);
    					uctimer = UCSTART_TIME;
    					regulate = TRUE;
    				}
    				/* regulate */
    				else
    				{
    					if(getadcvalue(UC_BOOST_SEL) > MAX_UC_VOLT)
    					{
    						if(pwm_ucboost != 0)
    						{
    							pwm_ucboost--;
    						}
    					}
    					else if(getadcvalue(UC_BOOST_SEL) < MIN_UC_VOLT)
    					{
    						if(MAX_UC_PWM > pwm_ucboost)
    						{
    							pwm_ucboost++;
    						}
    					}
    					PlayUCPWM(pwm_ucboost);
    				}
    				
    			}
    			else
    			{
    				uctimer--;
    			}
    		}
    		/* mains ok so stop PWM boost */
    		else
    		{
    			StopUCBoostPWM();
    			regulate = FALSE;
    			uctimer = 0;
    		}
    	}
    ST Employee
    December 6, 2023

    Thank you, but the code for STM8 doesn't help me to understand what problems you are facing.

    You wrote "if I use this I am getting an error undefined reference", I suppose you have compiler error when you are using this API: __HAL_TIM_SET_COMPARE()

    Is it correct?