实验材料:STM32F407IGT6,STM32CUBEMX,keil5
一.配置RCC
二.配置时钟
三.配置滴答计时器
四.配置定时器2
下面是设计原理,如下:
注意图中红色标记的地方。这样设置之后,IC1,IC2可以对同一信号进行捕获,IC1在上升沿捕获,每次上升沿触发中断,读取对应捕获寄存器的数据就可以计算频率,读取IC2的捕获寄存器可以计算占空比。
配置串口
五.main.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
#define TIM_PSC_1MHz 83
#define TIM_PSC_8400KHz 9
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
__IO STRUCT_CAPTURE strCapture = {
0, 1, 0, 0, 0};
extern __IO uint32_t uwTick;
int32_t Toggle_Pulse = 0; // 脉宽控制
__IO int32_t ulCcr;
double fFreq;
int32_t ulTimClk;
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main