cancel
Showing results for 
Search instead for 
Did you mean: 

I have problem in 'generate PWM signal's timing between after reset and switched On-Off '

Young Gwon Shin
Associate
Posted on July 10, 2018 at 12:26

Hello everybody thanks for click this one.

I making PWM signal squance but PWM generate time were different to my thought.

I using one button with interrupt to use start PWM.

After reset and press the button, PWM start. press the button again, PWM stop. 

After that press the button, PWM start again.

After reset take 700ns for PWM generate start but switched on-off take 560ns. 

I think call function in press button all the same. I don't know what is difference of after reset and switched on-off.

Please give me tip, whatever you can supposed.

attached my code and marked with 'BOLD'

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) // handling button click. generate PWM controlled by button.

{

if (GPIO_Pin == GPIO_PIN_2)

{

   if(0 == nCTRL_SHDN)    // ButtonState variable

{

   nCTRL_SHDN = 1;

   printf('STATE: %d \r\n', nCTRL_SHDN);

   DUTY = 50;

   PWM_CONF_SETUP(FREQ, DUTY); // already Initialized. just changing parameters 

   HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_9);

   PWM_START(); // PWM start function

}

else

{

nCTRL_SHDN = 0;

HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_9);

printf('STATE: %d \r\n', nCTRL_SHDN);

}

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void TIM4_IRQHandler(void) // handling PWM stop. this timer interrupt based on PWM at channel 2.

{

if ((0 == nCTRL_SHDN)&&(0 == release)) // Button OFF, 'release' is variable for generate pulse after another channel stopped

{

PWM_CHARGE_STOP1();

release++;

}

else if ((0 == nCTRL_SHDN)&&(0 != release))

{

release++;

if ( RELEASETIME + 1 == release )

{

PWM_CHARGE_STOP2();

release = 0;

}

}

else 

{

PWM_3KHZ.Instance->CCR2 = (333)*DUTY/100 -1;

}

__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2);

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void PWM_CONF_SETUP(uint8_t freq, uint8_t duty) // already Initialized. just changing parameters 

{

uint32_t PERIOD = 10000*50/freq;

TimMasterHandle.Init.Period = PERIOD - 1;

if (HAL_TIM_PWM_Init(&TimMasterHandle) != HAL_OK)

{

/* Initialization Error */

Error_Handler();

}

// Using 5 channel but 4 channel for COMBINED PWM 

sOCConfig.OCMode = TIM_OCMODE_PWM1;

sOCConfig.Pulse = (PERIOD-410/2) -1;

if (HAL_TIM_PWM_ConfigChannel(&TimMasterHandle, &sOCConfig, TIM_CHANNEL_1) != HAL_OK)

{

/* Configuration Error */

Error_Handler();

}

sOCConfig.OCMode = TIM_OCMODE_PWM1;

sOCConfig.Pulse = (PERIOD-410/2) -1;

if (HAL_TIM_PWM_ConfigChannel(&TimMasterHandle, &sOCConfig, TIM_CHANNEL_2) != HAL_OK)

{

/* Configuration Error */

Error_Handler();

}

sOCConfig.OCMode = TIM_OCMODE_COMBINED_PWM2;

sOCConfig.Pulse = (PERIOD-408/2) -1;

if (HAL_TIM_PWM_ConfigChannel(&TimMasterHandle, &sOCConfig, TIM_CHANNEL_3) != HAL_OK)

{

/* Configuration Error */

Error_Handler();

}

sOCConfig.OCMode = TIM_OCMODE_PWM1;

sOCConfig.Pulse = (PERIOD-388/2) -1;

if (HAL_TIM_PWM_ConfigChannel(&TimMasterHandle, &sOCConfig, TIM_CHANNEL_4) != HAL_OK)

{

/* Configuration Error */

Error_Handler();

}

sOCConfig_3KHZ.OCMode = TIM_OCMODE_PWM1;

sOCConfig_3KHZ.Pulse = (PERIOD_3KHZ)*duty/100 -1;

if (HAL_TIM_PWM_ConfigChannel(&PWM_3KHZ, &sOCConfig_3KHZ, TIM_CHANNEL_2) != HAL_OK)

{

/* Configuration Error */

Error_Handler();

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void PWM_CHARGE_STOP1(void)

{

if (HAL_TIM_PWM_Stop(&TimMasterHandle, TIM_CHANNEL_2) != HAL_OK)

{

/* PWM generation Error */

Error_Handler();

}

if (HAL_TIM_PWM_Stop(&PWM_3KHZ, TIM_CHANNEL_2) != HAL_OK)

{

/* PWM generation Error */

Error_Handler();

}

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void PWM_CHARGE_STOP2(void)

{

if (HAL_TIM_PWM_Stop(&TimMasterHandle, TIM_CHANNEL_3) != HAL_OK)

{

/* PWM generation Error */

Error_Handler();

}

// if (HAL_TIM_PWM_Stop_IT(&TimMasterHandle, TIM_CHANNEL_2) != HAL_OK)

// {

// /* PWM generation Error */

// Error_Handler();

// }

__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2);

__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void PWM_START(void)

{

if (HAL_TIM_PWM_Start_IT(&TimMasterHandle, TIM_CHANNEL_2) != HAL_OK)

{

/* PWM generation Error */

Error_Handler();

}

if (HAL_TIM_PWM_Start(&TimMasterHandle, TIM_CHANNEL_3) != HAL_OK)

{

/* PWM generation Error */

Error_Handler();

}

if (HAL_TIM_PWM_Start(&PWM_3KHZ, TIM_CHANNEL_2) != HAL_OK)

{

/* PWM generation Error */

Error_Handler();

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#pwm #timing #reset-cause
1 REPLY 1
T J
Lead
Posted on July 11, 2018 at 02:48

this is an interrupt ?

HAL_GPIO_EXTI_Callback

you cannot print from an interrupt.

you must set a flag and save into a table  ( global if you need to)

from a worker thread, check for the flag and table contents.

that being said, you don't need RTOS to have a worker thread..