Skip to main content
Associate III
August 5, 2024
Question

Pwm one cycle output

  • August 5, 2024
  • 4 replies
  • 1062 views

Hello, I'm using STM32F030C8 and I have created 4 PWM waveforms.

I want to create PWM and stop it after one cycle.

I'm thinking of how to create PWM and stop directly after one cycle.

 

Is there any other way?

 

Thank you.

4 replies

waclawek.jan
Super User
August 5, 2024

waclawekjan_0-1722853785899.png

 

JW

giwonKIMAuthor
Associate III
August 6, 2024

Hi,

 

htim5.Instance->CR1 |= (1 << 3);

HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_1);

 

I wrote the code like this, but it doesn't work. -> The signal will remain at the High level only.

 

Do you happen to have an example code?

giwonKIMAuthor
Associate III
August 6, 2024

I'm changing pwm mode and opm mode using external buttons.

When entering opm mode, keep high level and generate pulse. I want to keep low level and generate pulse.

NewFile3aaaaa.png

 

 

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	switch(GPIO_Pin)
	{
		case GPIO_PIN_13 :
		{
	 if (opm_mode)
	 {
	 	StartPWM();

	 opm_mode = 0;
	 }
	 else
	 {
	 	StartOPM();

	 
	 }
		}
	}

}

void StartPWM(void)
{
 HAL_TIM_PWM_Stop(&htim5, TIM_CHANNEL_1); // ???��머�?? 먼�? 중�?
 htim5.Instance->CR1 &= ~TIM_CR1_OPM; // OPM 비트�??? ?��리어?��?�� 반복 모드 ?��?��
 if (HAL_TIM_Base_Init(&htim5) != HAL_OK)
 {
 Error_Handler();
 }
 if (HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_1) != HAL_OK)
 {
 Error_Handler();
 }
}

void StartOPM(void)
{
 HAL_TIM_PWM_Stop(&htim5, TIM_CHANNEL_1); // ???��머�?? 먼�? 중�?
 htim5.Instance->CR1 |= (1 << 3); // OPM 비트�??? ?��?��?��?�� ?��?�� ?��?�� 모드 ?��?��
 if (HAL_TIM_Base_Init(&htim5) != HAL_OK)
 {
 Error_Handler();
 }
 if (HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_1) != HAL_OK)
 {
 Error_Handler();
 }
}

 

 

waclawek.jan
Super User
August 6, 2024

> I want to keep low level

Then you have either use the other PWM mode, or invert the output signal using TIMx_CCER.CCxP.

You may need to change also TIMx_ARR and TIMx_CCRx between the PWM and one-pulse mode to fit your expected output waveform.

JW