我正在学习使用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
}
我将时钟触发行移到代码的开头,而不是PA2,我使用PB14,现在它工作得很好。