Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 9 months ago.
What is wrong in this code for Nucleo L432KC?
Hello,
I try to set up TIM2 with encoder input on a Nucleo L432KC board. No matter what I tried, I could only get the timestamp in microseconds from TIM2->CNT On F401RE or F411RE this code works fine.
void EncoderInitialise_TIM2(void) { configure GPIO PA0 & PA1 as inputs for Encoder GPIOA_CLK_ENABLE(); Enable clock for GPIOA
GPIOA->MODER |= GPIO_MODER_MODER0_1 | GPIO_MODER_MODER1_1 ; PA1 & PA1 as Alternate Function /*!< GPIO port mode register, Address offset: 0x00 */ GPIOA->OTYPER |= GPIO_OTYPER_OT_0 | GPIO_OTYPER_OT_1 ; PA0 & PA1 as Inputs /*!< GPIO port output type register, Address offset: 0x04 */ GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR0 | GPIO_OSPEEDER_OSPEEDR1 ; Low speed /*!< GPIO port output speed register, Address offset: 0x08 */ GPIOA->PUPDR |= GPIO_PUPDR_PUPDR0_1 | GPIO_PUPDR_PUPDR1_1 ; Pull Down /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */ GPIOA->AFR[0] |= 0x00000011 ; AF01 for PA0 & PA1 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ GPIOA->AFR[1] |= 0x00000000 ; /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */
configure TIM2 as Encoder input TIM2_CLK_ENABLE(); Enable clock for TIM2
TIM2->CR1 = 0x0003; CEN(Counter ENable)='1' < TIM control register 1 TIM2->SMCR = 0x0003; SMS='011' (Encoder mode 3) < TIM slave mode control register TIM2->CCMR1 = 0x5151; CC1S='01' CC2S='01' < TIM capture/compare mode register 1 TIM2->CCMR2 = 0x0000; < TIM capture/compare mode register 2 TIM2->CCER = 0x0011; CC1P CC2P < TIM capture/compare enable register TIM2->PSC = 0x0000; Prescaler = (0+1) < TIM prescaler TIM2->ARR = 0x09; reload at 0xfffffff < TIM auto-reload register
TIM2->CNT = 0x0000; reset the counter before we use it }