蓝桥嵌入式之 TIM的输入捕获_Again(一)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ReCclay/article/details/87684163

该例程只可捕获输入信号的频率,若还想捕获对应的占空比,可参考这里<蓝桥嵌入式之 TIM的输入捕获_Again(二)>

工程可见Github1<传送门>


一、主要代码

main.c

/*******************************************************************************
* 文件名:main.c
* 描  述:
* 作  者:CLAY
* 版本号:v1.0.0
* 日  期: 2019年2月19日
* 备  注:修改后的LCD例程
*         PA7 (TIM3_CH2) 输入捕获外来信号频率(两次上升沿)
*******************************************************************************
*/

#include "stm32f10x.h"
#include "lcd.h"
#include "e2prom.h"
#include "stdio.h"
#include "i2c.h"
#include "adc.h"
#include "rtc.h"
#include "usart2.h"
#include "pwm.h"
#include "pwm_oc.h"
#include "pwm_ic.h"

u32 TimingDelay = 0;
u8 RxdCnt = 0;
u8 RxdOver = 0;
u8 RxdBuf[20];
void Delay_Ms(u32 nTime);
u8 RTC_Flag = 0;

extern __IO uint32_t TIM3Freq;

//Main Body
int main(void)
{	
	u8 str[20];
	
	STM3210B_LCD_Init();
	LCD_Clear(Blue);
	LCD_SetBackColor(Blue);
	LCD_SetTextColor(White);
	
	SysTick_Config(SystemCoreClock/1000);
	
	//PWM_Init(500, 60);//500Hz 60%方波
	PWM_OC_Init(500, 60);//500Hz 60%方波
	
	
	PWM_IC_Init();
	while(1)
	{
		sprintf((char *)str, "  Freq = %d     ", TIM3Freq);//注意加点空格,避免残留数据影响
		LCD_DisplayStringLine(Line5, str);
		Delay_Ms(200);
	}
}

//
void Delay_Ms(u32 nTime)
{
	TimingDelay = nTime;
	while(TimingDelay != 0);	
}

pwm_ic.c

#include "stm32f10x.h"

void PWM_IC_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_ICInitTypeDef  TIM_ICInitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	//注意仍需要定时器的配置
	TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
	TIM_TimeBaseStructure.TIM_Prescaler = 71;
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);//注意是哪个定时器(从别的地方复制的)
	
	
	TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
	TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
	TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
	TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
	TIM_ICInitStructure.TIM_ICFilter = 0x0;
	TIM_ICInit(TIM3, &TIM_ICInitStructure);
	
	TIM_Cmd(TIM3, ENABLE);
	TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
}

pwm_ic.h

#ifndef _PWM_IC_H
#define _PWM_IC_H

void PWM_IC_Init(void);

#endif

stm32f10x_it.c

...
__IO uint16_t IC3ReadValue1 = 0, IC3ReadValue2 = 0;
__IO uint16_t CaptureNumber = 0;
__IO uint32_t Capture = 0;
__IO uint32_t TIM3Freq = 0;

void TIM3_IRQHandler(void)
{ 
	if(TIM_GetITStatus(TIM3, TIM_IT_CC2) == SET) 
	{
		/* Clear TIM3 Capture compare interrupt pending bit */
		TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
		if(CaptureNumber == 0)
		{
			/* Get the Input Capture value */
			IC3ReadValue1 = TIM_GetCapture2(TIM3);
			CaptureNumber = 1;
		}
		else if(CaptureNumber == 1)
		{
			/* Get the Input Capture value */
			IC3ReadValue2 = TIM_GetCapture2(TIM3); 

			/* Capture computation */
			if (IC3ReadValue2 > IC3ReadValue1)
			{
				Capture = (IC3ReadValue2 - IC3ReadValue1); 
			}
			else
			{
				Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2); 
			}
			/* Frequency computation */ 
			TIM3Freq = 1000000 / Capture;
			CaptureNumber = 0;
		}
	}
}
...

二、注意事项

例程参考路径:嵌入式设计与开发项目加密资料\嵌入式设计与开发\STM32固件库v3.5\stm32f10x_stdperiph_lib\STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\TIM\InputCapture中的main.cstm32f10x_it.c

1、输入捕获没有定时器的配置,需要另加上!另外注意加的时候,不要复制过来就直接用,看清楚自己用的是哪个TIM

2、例程的中断里的TIM3Freq = (uint32_t) SystemCoreClock / Capture;要改为TIM3Freq = 1000000 / Capture;

3、主函数里面的数据显示要避免上一次长数据对本次短数据进行覆盖的影响,后面加了空格,这是个不错的操作。
展开阅读全文

没有更多推荐了,返回首页