91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 1KB
    文件類型: .zip
    金幣: 2
    下載: 0 次
    發布日期: 2021-06-14
  • 語言: 其他
  • 標簽:

資源簡介

stm32F407光電編碼器代碼,親測能用,可測量電機角度和轉速

資源截圖

代碼片段和文件信息

#include?“decoder.h“
#include?
#include?
#include?

//

//開發者只需要調用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_OType?=?GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd?=?GPIO_PuPd_NOPULL;
?? GPIO_InitStructure.GPIO_Speed?=?GPIO_Speed_100MHz;
?? GPIO_Init(GPIOE?&GPIO_InitStructure);

GPIO_PinAFConfig(GPIOE?GPIO_PinSource11?GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE?GPIO_PinSource9?GPIO_AF_TIM1);
}

void?Decoder_TIM_Init(void)
{
TIM_TimebaseInitTypeDef??TIM_TimebaseStructure;
TIM_ICInitTypeDef?TIM_ICInitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1??ENABLE);

TIM_DeInit(TIM1);
TIM_TimebaseStructInit(&TIM_TimebaseStructure);

TIM_TimebaseStructure.TIM_Period?=?ENCODER_TIM_PERIOD-1;
TIM_TimebaseStructure.TIM_Prescaler?=?0x00;
TIM_TimebaseStructure.TIM_ClockDivision?=?TIM_CKD_DIV1;
TIM_TimebaseStructure.TIM_CounterMode?=?TIM_CounterMode_Up;
TIM_TimebaseInit(TIM1?&TIM_TimebaseStructure);?
TIM_EncoderInterfaceConfig(TIM1?TIM_EncoderMode_TI12?TIM_ICPolarity_Falling?TIM_ICPolarity_Falling);

TIM_ICStructInit(&TIM_ICInitStructure);
TIM_SetCounter(TIM1?0);
TIM_Cmd(TIM1?ENABLE);
}?

//返回計數值
int?Decoder_GetCnt()
{
static?int?Decoder_cnt=0;
Decoder_cnt=(short)(TIM1?->?CNT);
TIM1?->?CNT=0;
return?Decoder_cnt;
}


?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????2177??2017-05-14?22:48??Encoder\decoder.c
?????文件?????????285??2017-05-14?22:36??Encoder\decoder.h
?????目錄???????????0??2019-07-03?17:05??Encoder\

評論

共有 條評論