Skip to main content
Visitor II
March 10, 2022
Question

How can this code be simplified?

  • March 10, 2022
  • 0 replies
  • 905 views

Can the TIM1 part of this tutorial be simplified as follows?

https://embedded-lab.com/blog/continuing-stm8-microcontroller-expedition/6/

Remove everything related to the the interrupt, because TIM1_GetCapture2() can be called at any time from main(). It will always contain the last capture duty cycle.

Reduce TIM1_setup(void) to:

TIM1_DeInit();

TIM1_TimeBaseInit(2000, TIM1_COUNTERMODE_UP, 55535, 1);

TIM1_PWMIConfig(TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING, TIM1_ICSELECTION_DIRECTTI,

.... TIM1_ICPSC_DIV1, 0);    

TIM1_SelectInputTrigger(TIM1_TS_TI1FP1);

TIM1_SelectSlaveMode(TIM1_SLAVEMODE_RESET);

TIM1_CCxCmd(TIM1_CHANNEL_1, ENABLE);

TIM1_CCxCmd(TIM1_CHANNEL_2, ENABLE);

TIM1_Cmd(ENABLE);

Because:

The second TIM1_PWMIConfig() is redundant because it simply overwrites the values set in the first call.

Reordered function calls to match the procedure described on page 170 in RM0016 manual.

    This topic has been closed for replies.