有关STM32F4输入捕捉模式的问题

问题描述

我正在使用NUCLEO-F411RE板(STM32F411RET)研究STM32 MCU编程。我最近观看了有关输入捕获计时器的教程,并对代码有疑问。下面是源代码

    /*
Write a program to generate 1Hz frequency in PA5 as an output using TIMER 2
and read the frequency using input capture in PA6 using TIMER 3
*/

#include "stm32f4xx.h"                  // Device header

int timeStamp = 0;

int main()
{
    //GPIO A configuration
    RCC->AHB1ENR |= 1;      //enable GPIO A
    GPIOA->MODER |= (0b10<<10)|(0b10<<12);      //set GPIO A pin 5 and 6 to alternate function mode
    GPIOA->AFR[0] |= (0b0001<<20)|(0b0010<<24);     //SET pin 5 as AF1 and pin 6 as AF2
    
    //TIMER 2 configuration
    
    //output compare mode
    RCC->APB1ENR |= 1;  //enable TIMER 2
    TIM2->PSC = 1600-1;
    TIM2->ARR = 10000-1;    //set a 1Hz clock
    TIM2->CCMR1 |= (0b011<<4)|(0b11<<8);    //set output to toggle on match
    TIM2->CCR1 = 0;     //set the CCR value to 0 (toggle when CNT = CCR1)
    TIM2->CCER |= 1;  //enable CH1 compare mode
    TIM2->CNT = 0;      //reset the count value to 0;
    TIM2->CR1 = 1;      //enable TIM2
    
    //input capture mode
    RCC->APB1ENR |= 2;      //ENABLE TIMER 3
    TIM3->PSC = 1600;       //divide by 16000
    TIM3->CCMR1 |= (0b01<<0)|(0b0100<<4);   //set CH1 to capture every edge
    TIM3->CCER = 1;     //enable CH1 input capture
    TIM3->CR1 = 1;      //enable TIM3
    
    
    while (1)
    {
        while (!(TIM3->SR & 2));
        timeStamp = TIM3->CCR1;
    }
}

问题1:为什么我们需要将CC2S位设置为0b11?

TIM2->CCMR1 |= (0b011<<4)|(0b11<<8);    //set output to toggle on match

我阅读了参考手册,但解释不清楚。

enter image description here

问题2:为什么我们需要将预分频器设置为输入捕捉模式?

TIM3->PSC = 1600;       //divide by 16000

问题3:如何在输入捕获模式下选择合适的采样频率?上面的源代码的采样频率是多少?

enter image description here

期待您的反馈,非常感谢您的支持

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)