蓝桥嵌入式之 输入捕获(二)

工程可见Github<传送门>


一、先要声明的

繁琐的库函数调用,如果放在蓝桥比赛中,选择记忆库函数,个人觉得不是明智之举,那么有没有什么巧妙的方法呢?


Of course!那就是固件库的例程移植!

当然这并不是说就不需要去了解从零开始写的过程了,不然等出现了问题只能GG,所以务必在看完上一篇输入捕获并理解全部程序的基础上再来看这一篇。

输入捕获例程对应路径为:
..\STM32固件库v3.5\stm32f10x_stdperiph_lib\STM32F10x_StdPeriph_Lib_V3.5.0 \Project\STM32F10x_StdPeriph_Examples\TIM\InputCapture文件夹下便是对应的TIM3_CH2的一个输入捕获例程,我们可以从给这里移植改写,为己所用。

当然还用到了其他部分例程中的初始化…

二、主要代码

main.c

/*******************************************************************************
* 文件名:main.c
* 描  述:
* 作  者:CLAY
* 版本号:v1.0.0
* 日  期: 2019年2月16日
* 备  注:该例程用于测量输入信号的频率 
*         输入管脚PA7(TIM3_CH2) 大于1K,会有些许的抖动误差
*******************************************************************************
*/

#include "config.h"

u8 str[20];
extern __IO uint32_t TIM3Freq;
extern u8 flagFreq;

int main(void)
{
	u32 i = 0;
	
	STM3210B_LCD_Init();
	LCD_Clear(Blue);//别忘了这一句总体清除
	LCD_SetBackColor(Blue);
	LCD_SetTextColor(White);
	
	TIM3_ICInit();
	
	while(1)
	{
		if(flagFreq)
		{
			flagFreq = 0;
			
			sprintf((char *)str, "TIM3Freq = %d           ", TIM3Freq);
			LCD_DisplayStringLine(Line1, str);
			for(i=0; i<500000; i++);//刷新延迟
		}	
	}
}


pwm_ic.c

#include "pwm_ic.h"

void TIM3_IOInit(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	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);
}

void TIM3_Init(void)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
	TIM_TimeBaseStructure.TIM_Prescaler = 72-1;
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);//傻了吧。。。
}

void TIM3_NVICInit(void)
{
	NVIC_InitTypeDef NVIC_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);	
}

void TIM3_ICInit(void)
{
	TIM_ICInitTypeDef  TIM_ICInitStructure;

	TIM3_IOInit();
	TIM3_Init();
	TIM3_NVICInit();
	
	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_ITConfig(TIM3, TIM_IT_CC2, ENABLE);	
	TIM_Cmd(TIM3, ENABLE);
}





pwm_ic.h

#ifndef _PWM_IC_H
#define _PWM_IC_H

#include "config.h"

void TIM3_ICInit(void);

#endif


stm32f10x_it.c

...
__IO uint16_t IC2ReadValue1 = 0, IC2ReadValue2 = 0;
__IO uint16_t CaptureNumber = 0;
__IO uint32_t Capture = 0;
__IO uint32_t TIM3Freq = 0;

u8 flagFreq = 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 */
      IC2ReadValue1 = TIM_GetCapture2(TIM3);
      CaptureNumber = 1;
    }
    else if(CaptureNumber == 1)
    {
      /* Get the Input Capture value */
      IC2ReadValue2 = TIM_GetCapture2(TIM3); 
      
      /* Capture computation */
      if (IC2ReadValue2 > IC2ReadValue1)
      {
        Capture = (IC2ReadValue2 - IC2ReadValue1); 
      }
      else
      {
        Capture = ((0xFFFF - IC2ReadValue1) + IC2ReadValue2); 
      }
      /* Frequency computation */ 
      TIM3Freq = 1000000 / Capture;//注意这里是72M频率72分频之后的值
	  
	  flagFreq = 1;
      CaptureNumber = 0;
    }
  }
}
...

三、实验现象

蓝桥嵌入式之 输入捕获(二)_第1张图片

你可能感兴趣的:(蓝桥杯之嵌入式)