标题STM32Cube配置等精度测频和测相位差
一、测量精度:
1、频率精度
频率测量、周期测量的信号频率范围扩展为1Hz~20MHz(这里由于信号发生器最高产生20MHz频率,所以更高频率未尝试,预计更高可达到50MHz),频率测量、周期测量的测量误差为0.000001.
2、相位差精度
相位差测量,可以达到量程:0~360°;测量准确度:1°;分辨率:0.1°的题目要求
二、项目配置
1、时钟配置
stm32F429最高主频为168MHz 配置时钟源
2、定时器配置
- Time1配置
- Time2配置
- Time3配置
- Time4配置
- Time5配置
- Time8配置
- Time9配置
- Time12配置
- 定时器优先级配置
3、生成工程配置
3、串口配置
配置两个串口,一个收一个发,防止数据传输时大彩屏卡死;
三、Keil5编写代码
1、main函数代码
#include "main.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "base.h"
#include "stdio.h"
#include "head_define.h"
#include "Algorithm.h"
#include "arm_math.h"
#include "hmi_user_uart.h"
#include "cmd_process.h"
#include "outputdata.h"
#define Period_Num 1
void SystemClock_Config(void);
uint8_t TIM4CH1_CAPTURE_STA=0X80;
uint32_t TIM5_COUNTER_VAL[Period_Num];
uint32_t TIM2_COUNTER_VAL[Period_Num];
float TIM5_COUNTER_Val;
float TIM2_COUNTER_Val;
float TIM_FREQ;
float TIM_period;
uint8_t TIM4CH1_CAPTURE_GATE=0;
uint8_t TIM4CH1_CAPTURE_STB=0;
static uint8_t i,j=0;
uint32_t TIM5_COUNTER_TEMP;
uint32_t TIM2_COUNTER_TEMP;
float TIM_FREQ2;
float TIM_FREQ3;
float TIM_FERQ[5];
uint32_t capture_period=0,capture_sta=0,capture_period1=0,capture_sta1=0;
float Phase=0,capture_period2=0;
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM1_Init();
MX_TIM2_Init();
MX_TIM3_Init();
MX_TIM4_Init();
MX_TIM5_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
MX_TIM8_Init();
MX_TIM9_Init();
MX_TIM12_Init();
TFT_Init();
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
HAL_TIM_IC_Start_IT(&htim4,TIM_CHANNEL_1);
__HAL_TIM_ENABLE_IT(&htim4, TIM_CHANNEL_1);
HAL_TIM_Base_Start_IT(&htim1);
HAL_TIM_IC_Start_IT(&htim9,TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim12,TIM_CHANNEL_1);
__HAL_TIM_SET_COUNTER(&htim5,0);
__HAL_TIM_SET_COUNTER(&htim2,0);
TIM4CH1_CAPTURE_GATE = 1;
while (1)
{
capture_period2=capture_period/2.0;
Phase=((capture_period1-capture_period2)/capture_period1)*360.0+21;
SetTFTText(0,3,"%.1f度",Phase);
SetTFTText(0,5,"%.2f",10.2);
HAL_Delay(100);
if(TIM4CH1_CAPTURE_STB)
{
TIM5_COUNTER_Val = (float) TIM5_COUNTER_TEMP / Period_Num;
TIM2_COUNTER_Val = (float) TIM2_COUNTER_TEMP / Period_Num;
TIM_FREQ = 1000.0*(TIM2_COUNTER_Val*3000.0)/TIM5_COUNTER_Val;
if(TIM_FREQ<=300)
{
TIM_FREQ2=TIM_FREQ*1.00020;
TIM_period=1.0/TIM_FREQ2;
SetTFTText(0,1,"%.6fHz",TIM_FREQ2);
SetTFTText(0,2,"%.6fs",TIM_period);
}else
{
TIM_FREQ2=TIM_FREQ*0.9999480027038594;
TIM_period=1.0/TIM_FREQ2;
SetTFTText(0,1,"%.6fMHz",TIM_FREQ2/1000000.0);
SetTFTText(0,2,"%.6fus",TIM_period*1000000.0);
}
TIM5_COUNTER_TEMP = 0;
TIM2_COUNTER_TEMP = 0;
TIM4CH1_CAPTURE_STB = 0;
TIM4CH1_CAPTURE_STA |= 0X80;
HAL_TIM_IC_Start_IT(&htim4,TIM_CHANNEL_1);
}
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(TIM4CH1_CAPTURE_GATE==0)
{
if(TIM4CH1_CAPTURE_STA&0X40)
{
TIM4CH1_CAPTURE_STA = 0X80;
TIM5_COUNTER_VAL[i] = __HAL_TIM_GET_COUNTER(&htim5);
TIM2_COUNTER_VAL[i] = __HAL_TIM_GET_COUNTER(&htim2);
TIM5_COUNTER_TEMP += TIM5_COUNTER_VAL[i];
TIM2_COUNTER_TEMP += TIM2_COUNTER_VAL[i];
i++;
if(i == Period_Num)
{
i = 0;
TIM4CH1_CAPTURE_STB = 1;
TIM4CH1_CAPTURE_STA = 0;
HAL_TIM_IC_Stop_IT(&htim4,TIM_CHANNEL_1);
}
HAL_TIM_Base_Stop(&htim5);
HAL_TIM_Base_Stop(&htim2);
__HAL_TIM_SET_COUNTER(&htim5,0);
__HAL_TIM_SET_COUNTER(&htim2,0);
}
}
if(TIM4CH1_CAPTURE_GATE==1)
{
if(TIM4CH1_CAPTURE_STA&0X80)
{
TIM4CH1_CAPTURE_STA = 0X40;
HAL_TIM_Base_Start(&htim5);
HAL_TIM_Base_Start(&htim2);
}
}
if(htim->Instance==TIM9)
{
if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_1)
{
if(!capture_sta)
{
__HAL_TIM_SET_COUNTER(&htim9,0);
}
}
}
if(htim->Instance==TIM12)
{
if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_1)
{
HAL_TIM_IC_Stop_IT(&htim9,TIM_CHANNEL_1);
capture_period=__HAL_TIM_GET_COMPARE(&htim9,TIM_CHANNEL_1);
if(!capture_sta1)
{
__HAL_TIM_SET_COUNTER(&htim12,0);
capture_sta1=1;
}else
{
HAL_TIM_IC_Start_IT(&htim9,TIM_CHANNEL_1);
__HAL_TIM_SET_COUNTER(&htim9,0);
capture_period1=__HAL_TIM_GET_COMPARE(&htim12,TIM_CHANNEL_1);
capture_sta1=0;
}
}
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(TIM1 == htim->Instance)
{
PCout(9)=~PCout(9);
if(TIM4CH1_CAPTURE_GATE)
{
TIM4CH1_CAPTURE_GATE = 0;
}
else
{
TIM4CH1_CAPTURE_GATE = 1;
}
}
}
void Error_Handler(void)
{
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
2、大彩屏
3、项目接线连接
需要将Time3的CH3连接到Time4中,否则无法测量。
4、项目工程代码
如需项目工程代码,请关注公众号:漂流小江,在公众号中即可获取代码
|