型号:CHR-GM37-520
参数:
?电机轴转一圈可以产生11个脉冲信号;
电机减速比为1:90;
实际产生脉冲数:90*11 = 990;
采用4倍频后脉冲数:990 * 4 =?3,960;
测数方法:在一定时间内,捕获脉冲数。
实际速度:一圈对应3960个脉冲,物理计算出一圈的长度,除以时间,得到实际速度。
code : timer?
#include "timer.h"
#include "sys.h"
#include "delay.h"
float cnt;
int Tim3_Arr = 5999;
int Tim3_Psc = 0;
void Encoder_Init_TIM3(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure);
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = Tim3_Arr;
TIM_TimeBaseInitStructure.TIM_Prescaler = Tim3_Psc;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 10; //滤波器值
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //允许TIM3溢出中断
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_SetCounter(TIM3, 0); //TIM3 ->CNT = 0
TIM_Cmd(TIM3, ENABLE);
}
void Timer_Init_TIM4(u16 arr, u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); ///使能TIM4时钟
TIM_TimeBaseInitStructure.TIM_Period = arr; //自动重装载值
TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //定时器分频
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);//初始化TIM3
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE); //允许定时器4更新中断
TIM_Cmd(TIM4,ENABLE); //使能定时器4
NVIC_InitStructure.NVIC_IRQChannel=TIM4_IRQn; //定时器3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00; //抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET)//溢出中断
{
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //清除中断标志位
}
void TIM4_IRQHandler(void)
{
cnt=TIM_GetCounter(TIM3);
TIM_SetCounter(TIM3,0);
TIM_SetCounter(TIM4,0);
if(TIM_GetITStatus(TIM4,TIM_IT_Update)==SET) //溢出中断
{
}
TIM_ClearITPendingBit(TIM4,TIM_IT_Update); //清除中断标志位
TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //清除中断标志位
}
code: main
#include "stm32f10x.h"
#include "sys.h"
#include "timer.h"
#include "delay.h"
float zheng;
float fu;
int main(void)
{
Timer_Init_TIM4(999, 7199);
Encoder_Init_TIM3();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
delay_init();
while(1)
{
if(cnt < Tim3_Arr/2)
{
zheng = cnt/3960;
}
else if(cnt > Tim3_Arr/2)
{
fu = (cnt - Tim3_Arr)/3960;
}
}
}
|