cancel
Showing results for 
Search instead for 
Did you mean: 

Event change state output pin in example TIM_OCToggle

Ariel Rivera
Associate II
Posted on March 26, 2018 at 12:53

Hi, i have implemented the example TIM_OCToggle from stm32f40xx to stm32f103xx, both work fine.

I want a event in the moment change state output pin of the channels OC, as occurs in the events:

HAL_TIM_PeriodElapsedCallback   or   HAL_TIM_PWM_PulseFinishedCallback

int main(void)

{

HAL_Init();

SystemClock_Config();

InitConfigTIM2_OC();

while (1)

{

}

}

static void InitConfigTIM2_OC(void)

{

TIM_ClockConfigTypeDef sClockSourceConfig;

TIM_MasterConfigTypeDef sMasterConfig;

TIM_OC_InitTypeDef sConfigOC;

uint32_t uhPrescalerValue = 0;

uhPrescalerValue = (uint32_t)(SystemCoreClock / 1000000) - 1;

hTIM2.Instance = TIM2;

//hTIM2.Init.Prescaler = 2000-1;

hTIM2.Init.Prescaler = uhPrescalerValue;

hTIM2.Init.CounterMode = TIM_COUNTERMODE_UP;

hTIM2.Init.Period = 50;

hTIM2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

hTIM2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

HAL_TIM_Base_Init(&hTIM2);

HAL_TIM_OC_Init(&hTIM2);

sConfigOC.OCMode = TIM_OCMODE_TOGGLE;

sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

sConfigOC.Pulse = uhCCR3_Val;

HAL_TIM_OC_ConfigChannel(&hTIM2, &sConfigOC, TIM_CHANNEL_3);

sConfigOC.Pulse = uhCCR4_Val;

HAL_TIM_OC_ConfigChannel(&hTIM2, &sConfigOC, TIM_CHANNEL_4);

HAL_TIM_MspPostInit(&hTIM2);

HAL_TIM_OC_Start_IT(&hTIM2, TIM_CHANNEL_3);

HAL_TIM_OC_Start_IT(&hTIM2, TIM_CHANNEL_4);

}

void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)

{

 if(htim->Instance == TIM2)

{

   if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3)

   {

         uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3);

         __HAL_TIM_SET_COMPARE(&hTIM2, TIM_CHANNEL_3, (uhCapture + uhCCR3_Val));

   }

   if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4)

   {

        uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4);

        __HAL_TIM_SET_COMPARE(&hTIM2, TIM_CHANNEL_4, (uhCapture + uhCCR4_Val));

   }

}

}

#stm32f103-event
1 ACCEPTED SOLUTION

Accepted Solutions
Ariel Rivera
Associate II
Posted on April 12, 2018 at 08:58

Resolved:

For mode Compare Output is absolutely necessary the value for Period(for HAL library) equal to 65535(filled registry of 32 bits)

View the schematic:

0690X0000060AZGQA2.png

int main(void)

{

HAL_Init();

SystemClock_Config();

InitConfigTIM2_OC();

while (1)

{

}

}

static void InitConfigTIM2_OC(void)

{

             TIM_ClockConfigTypeDef sClockSourceConfig;

             TIM_MasterConfigTypeDef sMasterConfig;

              TIM_OC_InitTypeDef sConfigOC;

uint32_t uhPrescalerValue = 0;

uhPrescalerValue = (uint32_t)(SystemCoreClock / 1000000) - 1;

hTIM2.Instance = TIM2;

hTIM2.Init.Prescaler = uhPrescalerValue;

hTIM2.Init.CounterMode = TIM_COUNTERMODE_UP;

hTIM2.Init.Period = 65350;     // <<<<-----------------------------------------------HERE!!!

hTIM2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

hTIM2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

HAL_TIM_Base_Init(&hTIM2);

HAL_TIM_OC_Init(&hTIM2);

sConfigOC.OCMode = TIM_OCMODE_TOGGLE;

sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

sConfigOC.Pulse = uhCCR3_Val;

HAL_TIM_OC_ConfigChannel(&hTIM2, &sConfigOC, TIM_CHANNEL_3);

sConfigOC.Pulse = uhCCR4_Val;

HAL_TIM_OC_ConfigChannel(&hTIM2, &sConfigOC, TIM_CHANNEL_4);

HAL_TIM_MspPostInit(&hTIM2);

HAL_TIM_OC_Start_IT(&hTIM2, TIM_CHANNEL_3);

HAL_TIM_OC_Start_IT(&hTIM2, TIM_CHANNEL_4);

}

void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)

{

 if(htim->Instance == TIM2)

{

   if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3)

   {

         BSP_LED_Toggle(LED_USB);               

 // for view on oscilloscope

         uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3);

         __HAL_TIM_SET_COMPARE(&hTIM2, TIM_CHANNEL_3, (uhCapture + uhCCR3_Val));

   }

   if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4)

   {

        m++;                                            // for view on Stm32Studio

        uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4);

        __HAL_TIM_SET_COMPARE(&hTIM2, TIM_CHANNEL_4, (uhCapture + uhCCR4_Val));

   }

}

}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

if(htim->Instance == TIM2)

{

 m4++;

}

}

View solution in original post

6 REPLIES 6
Posted on March 26, 2018 at 18:43

You probably want to enable the CC interrupt(s), but I don't use Cube/HAL so don't know what's the proper way to do it - maybe using HAL_TIM_OC_Start_IT()?

JW

Posted on March 26, 2018 at 19:20

Yes, this function

HAL_TIM_OC_Start_IT() i

s called on final of I

nitConfigTIM2_OC()

Posted on March 26, 2018 at 20:48

And without using the HAL, as it should be?

Posted on March 27, 2018 at 01:56

You are using HAL,

you can look inside that function and see what they have done.

I would go to the callback, and add some lines in the User section.

Posted on March 27, 2018 at 10:16

Well it's just setting the interrupt bits in TIMx_DIER; but if you say it's been called (and you can verify it by reading out and checking the TIMx_DIER in debugger) then the problem is elsewhere.

JW

Ariel Rivera
Associate II
Posted on April 12, 2018 at 08:58

Resolved:

For mode Compare Output is absolutely necessary the value for Period(for HAL library) equal to 65535(filled registry of 32 bits)

View the schematic:

0690X0000060AZGQA2.png

int main(void)

{

HAL_Init();

SystemClock_Config();

InitConfigTIM2_OC();

while (1)

{

}

}

static void InitConfigTIM2_OC(void)

{

             TIM_ClockConfigTypeDef sClockSourceConfig;

             TIM_MasterConfigTypeDef sMasterConfig;

              TIM_OC_InitTypeDef sConfigOC;

uint32_t uhPrescalerValue = 0;

uhPrescalerValue = (uint32_t)(SystemCoreClock / 1000000) - 1;

hTIM2.Instance = TIM2;

hTIM2.Init.Prescaler = uhPrescalerValue;

hTIM2.Init.CounterMode = TIM_COUNTERMODE_UP;

hTIM2.Init.Period = 65350;     // <<<<-----------------------------------------------HERE!!!

hTIM2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

hTIM2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

HAL_TIM_Base_Init(&hTIM2);

HAL_TIM_OC_Init(&hTIM2);

sConfigOC.OCMode = TIM_OCMODE_TOGGLE;

sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

sConfigOC.Pulse = uhCCR3_Val;

HAL_TIM_OC_ConfigChannel(&hTIM2, &sConfigOC, TIM_CHANNEL_3);

sConfigOC.Pulse = uhCCR4_Val;

HAL_TIM_OC_ConfigChannel(&hTIM2, &sConfigOC, TIM_CHANNEL_4);

HAL_TIM_MspPostInit(&hTIM2);

HAL_TIM_OC_Start_IT(&hTIM2, TIM_CHANNEL_3);

HAL_TIM_OC_Start_IT(&hTIM2, TIM_CHANNEL_4);

}

void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)

{

 if(htim->Instance == TIM2)

{

   if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3)

   {

         BSP_LED_Toggle(LED_USB);               

 // for view on oscilloscope

         uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3);

         __HAL_TIM_SET_COMPARE(&hTIM2, TIM_CHANNEL_3, (uhCapture + uhCCR3_Val));

   }

   if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4)

   {

        m++;                                            // for view on Stm32Studio

        uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4);

        __HAL_TIM_SET_COMPARE(&hTIM2, TIM_CHANNEL_4, (uhCapture + uhCCR4_Val));

   }

}

}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

if(htim->Instance == TIM2)

{

 m4++;

}

}