decoder.c
#include "decoder.h"
#include <stm32f4xx_tim.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_rcc.h>
//开发者只需要调用Decoder_Init()进行初始化
//然后调用Decoder_GetCnt或者Decoder_GetSpeed获得数据即可
uint16_t con_speed_period=20;//20ms采集一次数据,根据实际修改
float Decoder_GetSpeed(void)
{
static int16_t tmp;
static float re_dat=0;
tmp=(int16_t)Decoder_GetCnt();
if(tmp>20000)tmp=20000; //限幅
else if(tmp<-20000)tmp=-20000;
//编码器是1转输出256个正交脉冲,采用边缘计数,总共有256*4=1024
//进行单位换算
re_dat=(float)(((int16_t)tmp)*1000/(1024.0*con_speed_period));
return re_dat;
}
void Decoder_Init(void)
{
Decoder_GPIO_Init();
Decoder_TIM_Init();
}
void Decoder_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_O