欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 1795|回復(fù): 0
收起左側(cè)

關(guān)于單片機(jī)與編碼器的問(wèn)題求教

[復(fù)制鏈接]
ID:613834 發(fā)表于 2020-9-1 09:41 | 顯示全部樓層 |閱讀模式
你好,我在STM32中把TIM1配置為編碼器模式,編碼器的AB兩項(xiàng)連到了TIM1的CH1和CH2,為什么沒(méi)有讀數(shù)啊
下邊是我找的代碼,麻煩大神看看哪里不對(duì),謝謝

#include "decoder.h"
#include <stm32f4xx_tim.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_rcc.h>


uint16_t con_speed_period=20;

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;
        
     
        re_dat=(float)((((int16_t)tmp)*1000/(1024.0*con_speed_period))*60);
        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);/////////////////3333
        

         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);        /////////////123

        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;
}

這是編碼器

這是編碼器
回復(fù)

使用道具 舉報(bào)

無(wú)效樓層,該帖已經(jīng)被刪除

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表