cancel
Showing results for 
Search instead for 
Did you mean: 

PWM Dim

SRAM.11
Associate III

I am trying a PWM output with jumper connectors.

when mains fail(input supply)

PWM is on indicated by a lamp and OFF when there is a supply .

in between if the supply is OFF and Lamp is in ON there is a jumper connect to PA6 (input)of stm32c011f6p6.if the input is set to 1 it should make the PWM with half duty cycle.

I couldn't understand that my half duty cycle part is blinking once the input supply OFF and PA6 is set till the input supply is 0.1 and below 0.1 its getting stable half duty cycle. can one find the error I made please.

 

Mains is input supply 1.5v Vbat is 0.9v 

12 Mhz internal clock

 

can one find the error I made please.

     if (Mains > 0.1)
	        	          {
	        	              if ((Mains < 1) && (Vbat > 0.9))
	        	              {
	        	                  TIM1->CCR4 = TIM1->ARR;
	        	                  HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
	        	                  Set_LED(0);
	        	                  //Batt_Chrge(0);
	        	                  HAL_Delay(100);
	        	              }
	        	              else if (Mains > 1)
	        	              {
	        	                  TIM1->CCR4 = 0;
	        	                  HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
	        	                  HAL_Delay(100);
	        	              }

	        	              if (Mains < 1)
	        	              {
	        	            	  if(HAL_GPIO_ReadPin(Dim_GPIO_Port, Dim_Pin) == GPIO_PIN_SET)
	        	            	  {
	        	                  // Code for dimming when Mains is below 1V or Dim_Pin is set
	        	                  TIM1->CCR4 = TIM1->ARR / 2; // Set PWM duty cycle to half
	        	                  HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
	        	                  //Batt_Chrge(0);
	        	            	  }
	        	            	  else
	        	            	  {
	        	                  // Code for turning off dimming when Mains is 1V or higher and Dim_Pin is not set
	        	                  TIM1->CCR4 = TIM1->ARR;
	        	                  HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
	        	                  Set_LED(0);
	        	                  //Batt_Chrge(0);
	        	                  HAL_Delay(100);
	        	              }
	        	                  }
	        	          }
***********************************************
TIMER CONFIG
static void MX_TIM1_Init(void)
{

  /* USER CODE BEGIN TIM1_Init 0 */

  /* USER CODE END TIM1_Init 0 */

  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};
  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};

  /* USER CODE BEGIN TIM1_Init 1 */

  /* USER CODE END TIM1_Init 1 */
  htim1.Instance = TIM1;
  htim1.Init.Prescaler = 0;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 25000;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim1.Init.RepetitionCounter = 0;
  htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 1000;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
  {
    Error_Handler();
  }
  sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
  sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
  sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
  sBreakDeadTimeConfig.DeadTime = 0;
  sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
  sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
  sBreakDeadTimeConfig.BreakFilter = 0;
  sBreakDeadTimeConfig.BreakAFMode = TIM_BREAK_AFMODE_INPUT;
  sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;
  sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
  sBreakDeadTimeConfig.Break2Filter = 0;
  sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT;
  sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
  if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM1_Init 2 */

  /* USER CODE END TIM1_Init 2 */

}

 

1 ACCEPTED SOLUTION

Accepted Solutions
Sarra.S
ST Employee

Hello @SRAM.11,

Could you please elaborate more on "my half duty cycle part is blinking once the input supply OFF and PA6 is set till the input supply is 0.1 and below 0.1 its getting stable half duty cycle." or provide a diagram of the circuit? 

 

 

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.

View solution in original post

3 REPLIES 3
Sarra.S
ST Employee

Hello @SRAM.11,

Could you please elaborate more on "my half duty cycle part is blinking once the input supply OFF and PA6 is set till the input supply is 0.1 and below 0.1 its getting stable half duty cycle." or provide a diagram of the circuit? 

 

 

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.

AScha.3
Chief III

> input supply 1.5v

supply is 1,5V ?? minimum is 2,0V !

If you feel a post has answered your question, please click "Accept as Solution".

Hi Sarah,

   I am trying to execute a small part from a project in lighting.The code is for emergency operation nif mains off the lamp on.Lamp on off operation is done with PWM.there is connection to VDD and PA6 with a 2 pin connector.if PA6 is ON(shorted with VDD) my lamp should dim(half duty cycle PWM) that part is not working I couldn't

if (Mains > 0.5)
	        	          {
	        	              if ((Mains < 1) && (Vbat > 0.9))
	        	              {
	        	                  TIM1->CCR4 = TIM1->ARR;
	        	                  HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
	        	                  Set_LED(0);
	        	                  //Batt_Chrge(0);
	        	                  HAL_Delay(100);
	        	              }
	        	              else if (Mains > 1)
	        	              {
	        	                  TIM1->CCR4 = 0;
	        	                  HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
	        	                  HAL_Delay(100);
	        	              }

	        	              if (Mains < 1)
	        	              {
	        	            	  if(HAL_GPIO_ReadPin(Dim_GPIO_Port, Dim_Pin) == GPIO_PIN_SET)
	        	            	  {
	        	                  // Code for dimming when Mains is below 1V or Dim_Pin is set
	        	                  TIM1->CCR4 = TIM1->ARR/2; // Set PWM duty cycle to half
	        	                  HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
	        	                  //Batt_Chrge(0);
	        	            	  }
	        	            	  else
	        	            	  {
	        	                  // Code for turning off dimming when Mains is 1V or higher and Dim_Pin is not set
	        	                  TIM1->CCR4 = TIM1->ARR;
	        	                  HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
	        	                  Set_LED(0);
	        	                  //Batt_Chrge(0);
	        	                  HAL_Delay(100);

	        	              }
	        	                  }
	        	          }

find what is wrong.this is my code to that