编码器源码

本文档提供了关于STM32解码器的初始化代码,包括TIM和GPIO的初始化函数Decoder_TIM_Init()和Decoder_GPIO_Init(),以及获取计数器值的Decoder_GetCnt()函数和计算速度的Decoder_GetSpeed()函数。解码器定时器周期定义为65535,确保不超过限制。
摘要由CSDN通过智能技术生成

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
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值