2023-11-20 10:41 AM
Hello ,
I have initialized two timers , Timer3 and Timer9. Im using Timer9 with 137uS period , setting my gpio pin high , starting Tim3 Interrupt with 54uS period for now .When Tim3 Interrupt occurs I simply reset my gpio pin.My problem is that I should have a high pwm for 54uS and 137uS pwm in reset state.I have thoroughly checked individual timers to just toggle single gpio in 1 single interrupt , both give me a stable 137uS pwm and 54uS pwm . But as i use two different gpio's to set and reset my gpio. I get irregular pwm that has values randomly changing. My pwm will have a constant 137uS low and a variable high pwm state which is Max 122uS for now as I said its just fixed 54uS so I also did not enable autoload preload .This is the first time I'm doing it this way.
My Code :
Timer Setting :
void MX_TIM3_Init(void)
{
/* USER CODE BEGIN TIM3_Init 0 */
/* USER CODE END TIM3_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM3_Init 1 */
/* USER CODE END TIM3_Init 1 */
htim3.Instance = TIM3;
htim3.Init.Prescaler = 0;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 4700;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM3_Init 2 */
//HAL_TIM_Base_Start_IT(&htim3);
/* USER CODE END TIM3_Init 2 */
}
/* TIM9 init function */
void MX_TIM9_Init(void)
{
/* USER CODE BEGIN TIM9_Init 0 */
/* USER CODE END TIM9_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
/* USER CODE BEGIN TIM9_Init 1 */
/* USER CODE END TIM9_Init 1 */
htim9.Instance = TIM9;
htim9.Init.Prescaler = 0;
htim9.Init.CounterMode = TIM_COUNTERMODE_UP;
htim9.Init.Period = 11508;
htim9.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim9.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim9) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim9, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM9_Init 2 */
HAL_TIM_Base_Start_IT(&htim9);
/* USER CODE END TIM9_Init 2 */
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM3)
{
/* USER CODE BEGIN TIM3_MspInit 0 */
/* USER CODE END TIM3_MspInit 0 */
/* TIM3 clock enable */
__HAL_RCC_TIM3_CLK_ENABLE();
/* TIM3 interrupt Init */
HAL_NVIC_SetPriority(TIM3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM3_IRQn);
/* USER CODE BEGIN TIM3_MspInit 1 */
/* USER CODE END TIM3_MspInit 1 */
}
else if(tim_baseHandle->Instance==TIM9)
{
/* USER CODE BEGIN TIM9_MspInit 0 */
/* USER CODE END TIM9_MspInit 0 */
/* TIM9 clock enable */
__HAL_RCC_TIM9_CLK_ENABLE();
/* TIM9 interrupt Init */
HAL_NVIC_SetPriority(TIM1_BRK_TIM9_IRQn,0, 0);
HAL_NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn);
/* USER CODE BEGIN TIM9_MspInit 1 */
/* USER CODE END TIM9_MspInit 1 */
}
}
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM3)
{
/* USER CODE BEGIN TIM3_MspDeInit 0 */
/* USER CODE END TIM3_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM3_CLK_DISABLE();
/* TIM3 interrupt Deinit */
HAL_NVIC_DisableIRQ(TIM3_IRQn);
/* USER CODE BEGIN TIM3_MspDeInit 1 */
/* USER CODE END TIM3_MspDeInit 1 */
}
else if(tim_baseHandle->Instance==TIM9)
{
/* USER CODE BEGIN TIM9_MspDeInit 0 */
/* USER CODE END TIM9_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM9_CLK_DISABLE();
/* TIM9 interrupt Deinit */
HAL_NVIC_DisableIRQ(TIM1_BRK_TIM9_IRQn);
/* USER CODE BEGIN TIM9_MspDeInit 1 */
/* USER CODE END TIM9_MspDeInit 1 */
}
}
Interrupt Callback function :
volatile bool temp = true;
volatile bool Flag = true;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM3)
{
if(temp)
{
temp= false;
}
//HAL_TIM_Base_Start_IT(&htim9);
//__HAL_TIM_ENABLE_IT(htim, TIM_IT_UPDATE);
if(Flag)
{
HAL_GPIO_WritePin(GPIOA, weld_Pin,GPIO_PIN_RESET);
Flag = false;
__NOP();
}
}
if(htim->Instance == TIM9)
{
if(temp)
{
temp= false;
//HAL_TIM_Base_Init(&htim3);
HAL_TIM_Base_Start_IT(&htim3);
}
//HAL_GPIO_TogglePin(GPIOA,weld_Pin);
Flag = true;
HAL_GPIO_WritePin(GPIOA, weld_Pin,GPIO_PIN_SET);
__NOP();
}
}
Initially I got overconfident and defined the whole structure program which is much complicated hoping this will definitely work , but unfortunately I had to go back to the basics because I was not getting the desired Timer results.
I will add 2 photos which depict what I want and what I'm actually getting .
Any Help would be great . @Tesla DeLorean @TDK ,Can you help in this matter ?
Thanks.
Actual Picture
Original Idea
Solved! Go to Solution.
2023-11-22 01:59 AM
Yes , I Believe thats true but Update interrupt not interrupting in there own dedicated functions ? Weird. Trust me man I'm just trying to go along with HAL using basic libraries to get the job done ,otherwise I absolutely hate it . Will shift to LL Drivers but for now I have to get the job done .
@TDK can you help me with the above reply regarding interrupt ?
2023-11-22 07:15 AM
Depending on your channel configuration, CCx callbacks are done in one of the following callbacks:
HAL_TIM_IC_CaptureCallback(htim);
HAL_TIM_OC_DelayElapsedCallback(htim);
HAL_TIM_PWM_PulseFinishedCallback(htim);
You can see the logic used for callbacks within HAL_TIM_IRQHandler.
2023-11-22 10:07 PM
Thanks, Got it, Lastly regarding the Interrupt Priorities , I've I have 2 Timers , Timers 1 is the highest priority and Timer2 is the second highest priority , that means my Preempt Priority is 0 for Timer 1 and 1 for Timer 2 right ?
2023-11-23 07:28 AM
Yes. Priority 0 will pre-empt priority 1.
2023-11-23 08:10 AM
Thanks @TDK