野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12192|回复: 1

TIM2捕获模块

[复制链接]
发表于 2015-10-19 09:46:26 | 显示全部楼层 |阅读模式
求大神指教  !!!!!!!!!!!

//第一步:GPIO 初始化

GPIO_InitTypeDef GPIO_InitStructure;        //TIM2 通道 1  通道 2 PA0、PA1

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

//使用 PA0、PA1 做为上拉输入,用来测频率

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU ;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);


//第二步:中断向量初始化

NVIC_InitTypeDef NVIC_InitStructure;

NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;        //NVIC 配置

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);


//第三步:定时器初始化

//测频率配置,捕获两个上升沿,计算频率

void TIM2_Freq_Config(void)

{

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

TIM_ICInitTypeDef  TIM_ICInitStructure;

TIM_OCInitTypeDef  TIM_OCInitStructure;


RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);  // TIM2 clock enable

TIM_DeInit(TIM2);

/*TIM2 时钟配置*/               
TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) (72000000        / 2000000)        - 1 ; // 预分频 ( 时钟分
频)72M/2M        //以 2MHz 计数               
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;        //向上计数
TIM_TimeBaseStructure.TIM_Period = 65535;                //装载值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;               
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;               
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);               
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;        //上升沿触发
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;               
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;        //管脚与寄存器对应关系

//输入预分频。意思是控制在多少个输入周期做一次捕获,如果

//输入的信号频率没有变,测得的周期也不会变。比如选择 4 分频,


//则每四个输入周期才做一次捕获,这样在输入信号变化不频繁的情况下,

//可以减少软件被不断中断的次数。

TIM_ICInitStructure.TIM_ICFilter = 0x01;        //滤波设置,经历几个周期跳变

认定波形稳定 0x0~0xF


/*TIM2 的通道四设置为 输入捕获 模式*/ TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM_ICInit(TIM2, &TIM_ICInitStructure);

TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;

TIM_ICInit(TIM2, &TIM_ICInitStructure);

]
TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);        //使能中断

TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);

]
TIM_Cmd(TIM2, ENABLE);        //启动 TIM2

}


//第四步:中断服务程序

/*********** TIM2 CH1~4***************/

__IO uint16_t TIM2IC_ReadValue1[4] , TIM2IC_ReadValue2[4];        //记录各通道的捕获值

__IO uint16_t TIM2CaptureNumber[4] ;        //捕获次数

__IO uint32_t TIM2Capture[4] ;        //最终计算捕获差值

void TIM2_IRQHandler(void)

{

u8 i=0;

if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)

{

TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

}

else if(TIM_GetITStatus(TIM2, TIM_IT_CC1) == SET)

{

/* Clear TIM3 TIM2IC_ReadValue compare interrupt pending bit */
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
if(TIM2CaptureNumber[0] == 0)

{

/* Get the Input TIM2IC_ReadValue value */
TIM2IC_ReadValue1[0] = TIM_GetCapture1(TIM2);
TIM2CaptureNumber[0] = 1;


}

else if(TIM2CaptureNumber[0] == 1)

{

/* Get the Input TIM2IC_ReadValue value */ TIM2IC_ReadValue2[0] = TIM_GetCapture1(TIM2);

/* TIM2IC_ReadValue computation */

if (TIM2IC_ReadValue2[0] > TIM2IC_ReadValue1[0])

{

TIM2Capture[0] = (TIM2IC_ReadValue2[0] - TIM2IC_ReadValue1[0]);

}

else

{

TIM2Capture[0] = ((0xFFFF - TIM2IC_ReadValue1[0]) + TIM2IC_ReadValue2[0]);

}

/* Frequency computation */

Frequency0= 2000000/ TIM2Capture[0];// 计算频率=2MHz/时钟计数

IM2CaptureNumber[0] = 0;

TIM_ITConfig(TIM2, TIM_IT_CC1, DISABLE);

}

}

else if(TIM_GetITStatus(TIM2, TIM_IT_CC2) == SET)

{

/* Clear TIM3 TIM2IC_ReadValue compare interrupt pending bit */ TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);

if(TIM2CaptureNumber[1] == 0)

{

/* Get the Input TIM2IC_ReadValue value */ TIM2IC_ReadValue1[1] = TIM_GetCapture2(TIM2); TIM2CaptureNumber[1] = 1;

}

else if(TIM2CaptureNumber[1] == 1)

{

/* Get the Input TIM2IC_ReadValue value */ TIM2IC_ReadValue2[1] = TIM_GetCapture2(TIM2);

/* TIM2IC_ReadValue computation */

if (TIM2IC_ReadValue2[1] > TIM2IC_ReadValue1[1])

{

TIM2Capture[1] = (TIM2IC_ReadValue2[1] - TIM2IC_ReadValue1[1]);

}

else

{


TIM2Capture[1] = ((0xFFFF - TIM2IC_ReadValue1[1]) + TIM2IC_ReadValue2[1]);

}

/* Frequency computation */

Frequency1= 2000000/ TIM2Capture[1];// 计算频率=2MHz/时钟计数

TIM2CaptureNumber[1] = 0;

TIM_ITConfig(TIM2, TIM_IT_CC2, DISABLE);

}

}



这段捕获程序可对??????要是把捕获值显示在液晶屏上,用LCD_DisNum(0,280,  TIM2Capture[0] , BLACK)显示的话,该变量 TIM2Capture[0]又出现重定义,到底该怎么办   ?????????????、

我主要目的是:当电机转动时,用光电传感器捕获两个叶片经过的值,然后算出其之间相差的时间!!!!!!!!
回复

使用道具 举报

发表于 2015-12-13 11:38:29 | 显示全部楼层
帮顶                          
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2025-1-11 05:58 , Processed in 0.028226 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表