Skip to main content
Zheng Liang
Associate III
Mar 2, 2018 5:25 AM UTC
Question

How to configure the starting direction of center-aligned Timer

Posted on March 02, 2018 at 06:25

Device: STM32F103

I wanna set the direction(counting up or down) when starting a center-aligned Timer, but once I set the DIR and then set the CMS to center-aligned mode, the DIR bit will be cleared by hardware.

I found this description in datasheet:

When starting in center-aligned mode, the current up-down configuration is used. It

means that the counter counts up or down depending on the value written in the DIR bit

in the TIMx_CR1 register. Moreover, the DIR and CMS bits must not be changed at the

same time by the software.

How could I start the center-aligned mode with down direction?

If someone knows, please tell me.

thanks.

#stm32
    This topic has been closed for replies.

    3 replies

    Jan Waclawek
    Visitor II
    Mar 2, 2018 9:41 AM UTC
    Posted on March 02, 2018 at 10:41

    Show us code.

    Read out the timer registers immediately before enabling it and check/post.

    What's the CNT startng position?

    JW

    Zheng Liang
    Associate III
    Mar 3, 2018 9:00 AM UTC
    Posted on March 03, 2018 at 09:00

    Hi Jan, here are the codes:

    --------------------------------------------------------------------

    //Enbale Timer1 clock

    RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;

    //Set single pulse mode

    TIM1->CR1 |= TIM_CR1_OPM;

    //Set Center-aligned mode 3

    TIM1->CR1 |= TIM_CR1_CMS;

    //Set prescaler

    TIM1->PSC = 0;

    //Set auto-reaload value

    TIM1->ARR = 71 * 4;

    //Set repetition

    TIM1->RCR = 2;

    //Channel 1 at Toggle Mode

    TIM1->CCMR1 |= 3 << 4;

    TIM1->CCR1 = TIM1->ARR >> 1;

    //Enable channel 1 output

    TIM1->CCER |= TIM_CCER_CC1E;

    TIM1->BDTR |= TIM_BDTR_MOE;

    //Set direction to downcounting

    TIM1->CR1 |= TIM_CR1_DIR;

    //Set counter register to max value

    TIM1->CNT = TIM1->ARR;

    //Enable Timer1

    TIM1->EGR |= TIM_EGR_UG;

    TIM1->CR1 |= TIM_CR1_CEN;

    --------------------------------------------------------------------

    The problem is when  Center-aligned mode 3 is set, the code to set direction bit in CR1 register to downcounting will not work at all.

    Is the Center-aligned mode direction could only be set by hardware? means the counter could only start with upcounting mode from the first time?

    Thanks,

    Zheng

    waclawek.jan
    Super User
    Mar 4, 2018 2:32 AM UTC
    Posted on March 04, 2018 at 02:32

    Zheng,

    Your problem is in that you set up the bits in CR1 separately. Then, when you get to set DIR, the following note below DIR's description in RM applies:

    Note: This bit is read only when the timer is configured in Center-aligned mode or Encoder

    mode.

    Set CR1 at once, as the last line:

    TIM1->CR1 = TIM_CR1_DIR | TIM_CR1_OPM | TIM_CR1_CMS | TIM_CR1_CEN;

    Note also that setting TiMx_EGR.UG has quite an aggressive effect: it resets TIMx_CNT to 0, and clears the DIR bit.

    JW