STM32F302R8。 TIM2 作为输出和 TIM15 作为输入对我不起作用

STM32F302R8. TIM2 as output and TIM15 as input does not work for me

我正在学习使用STM32F302R8板子,遇到这个问题

TIM2 在PA0 上配置了切换输出CH1。 (这很好用)。

TIM15配置为PA2上的输入捕捉CH1。

我在 PA0 和 PA2 之间有跳线。

目的是当 TIM2 达到 CCR1 的值时,它会触发 PA0 引脚,这会发生,并且将跳线与 PA2 引脚连接应该会触发 TIM15 输入,但这并没有发生。

虽然检查 CC1IF 标志永远不会结束,但它没有检测到任何东西。

它可以是什么?

while (1)
{
  // wait until input edge is captured
  while ( !(TIM15->SR & TIM_SR_CC1IF)) {} 
  timestamp = TIM15->CCR1;      // read captured counter value
}

void mi_GPIO_Init ( void ) {
  RCC->AHBENR  |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN;
  GPIOA->MODER  &= ~GPIO_MODER_MODER0;   // clear PA0 mode
  GPIOA->MODER  |=  GPIO_MODER_MODER0_1; // set pin to alt function
  GPIOA->AFR[0] &= ~GPIO_AFRL_AFRL0;     // clear pin AF bits
  GPIOA->AFR[0] |=  0x0000001;           // set pin to AF1 for TIM2_CH1

  GPIOA->MODER  &= ~GPIO_MODER_MODER2;   // clear PA2 mode
  GPIOA->MODER  |=  GPIO_MODER_MODER2_1; // set pin to alt function
  GPIOA->AFR[0] &= ~GPIO_AFRL_AFRL2;     // clear pin AF bits
  GPIOA->AFR[0] |=  0x0000900;           // set pin to AF9 for TIM15_CH1

  // Configure TIM2 to wrap around at 1 Hz and toggle CH1 output when the counter value is 0
  RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;   // enable TIM2 clock
  TIM2->PSC = 800 - 1;          // divided by 800
  TIM2->ARR = 10000 -1;         // divided by 10000
  TIM2->CCMR1 = TIM_CCMR1_OC1M_0 | TIM_CCMR1_OC1M_1; // set output to toggle on match
                            // The rest of the bits are set to 0.
  TIM2->CCR1 = 0;               // set match value
  TIM2->CCER |= TIM_CCER_CC1E;  // enable CH1 compare mode
  TIM2->CNT = 0;                // clear timer counter
  TIM2->CR1 |= TIM_CR1_CEN;     // enable TIM2

  // Configure TIM15 to do input capture
  RCC->APB2ENR |= RCC_APB2ENR_TIM15EN;  // enable TIM15 clock
  TIM15->PSC = 8000 - 1;        // divided by 8000
  TIM15->ARR = 0xFFFF;          // count until ARR
  TIM15->CCMR1 &= ~TIM_CCMR1_CC1S; // set CH1 to capture at every edge
  TIM15->CCMR1 |= TIM_CCMR1_CC1S_0; // CC1 as input, IC1 is mapped on TI1
  TIM15->CCER |= TIM_CCER_CC1E; // enable CH1 capture rising edge
  TIM15->CR1 |= TIM_CR1_CEN;    // enable TIM15
}

我将时钟触发线移动到代码的开头,我使用 PB14 而不是 PA2,现在它工作正常。