2018-03-01 09:25 PM
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 bitin the TIMx_CR1 register. Moreover, the DIR and CMS bits must not be changed at thesame time by the software.How could I start the center-aligned mode with down direction?
If someone knows, please tell me.
thanks.
#stm322018-03-02 01:41 AM
Show us code.
Read out the timer registers immediately before enabling it and check/post.
What's the CNT startng position?
JW
2018-03-03 01:00 AM
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
2018-03-03 06:32 PM
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