Skip to main content
Ariel Rivera
Associate II
March 26, 2018
Solved

Event change state output pin in example TIM_OCToggle

  • March 26, 2018
  • 2 replies
  • 1597 views
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
This topic has been closed for replies.
Best answer by Ariel Rivera
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++;

}

}

2 replies

waclawek.jan
Super User
March 26, 2018
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

Ariel Rivera
Associate II
March 26, 2018
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()

Ariel Rivera
Ariel RiveraAuthorBest answer
Associate II
April 12, 2018
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++;

}

}