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

QQ登錄

只需一步,快速開始

搜索
查看: 2900|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

找stm32f1系列的編碼器測(cè)速例程 求分享

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:691720 發(fā)表于 2020-5-25 09:42 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
8黑幣
找stm32f1系列的編碼器測(cè)速例程
用的是那種帶編碼器的減速電機(jī)

最佳答案

查看完整內(nèi)容

#include "stm32f10x.h" #include "stm32f10x_gpio.h" #include "stm32f10x_rcc.h" #include "stm32f10x_tim.h" // 定義引腳 #define ENCODER_A_PIN GPIO_Pin_0 #define ENCODER_B_PIN GPIO_Pin_1 #define ENCODER_TIM TIM2 #define ENCODER_TIM_CH TIM_Channel_1 #define ENCODER_TIM_IRQn TIM2_IRQn volatile uint32_t encoder_count = 0; // 編碼器計(jì)數(shù)值 volatile uint16_t encoder_speed = ...
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:1097052 發(fā)表于 2020-5-25 09:42 | 只看該作者
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_tim.h"

// 定義引腳
#define ENCODER_A_PIN     GPIO_Pin_0
#define ENCODER_B_PIN     GPIO_Pin_1
#define ENCODER_TIM       TIM2
#define ENCODER_TIM_CH    TIM_Channel_1
#define ENCODER_TIM_IRQn  TIM2_IRQn

volatile uint32_t encoder_count = 0; // 編碼器計(jì)數(shù)值
volatile uint16_t encoder_speed = 0; // 編碼器速度

void Encoder_Init(void);
void TIM2_IRQHandler(void);

int main(void)
{
    uint8_t dir = 1; // 電機(jī)轉(zhuǎn)動(dòng)方向,1為正轉(zhuǎn),0為反轉(zhuǎn)
    float speed = 0; // 電機(jī)速度

    Encoder_Init(); // 初始化編碼器
    TIM_Cmd(ENCODER_TIM, ENABLE); // 使能定時(shí)器
    TIM_ITConfig(ENCODER_TIM, TIM_IT_Update, ENABLE); // 使能更新中斷
    NVIC_EnableIRQ(ENCODER_TIM_IRQn); // 使能定時(shí)器中斷

    while (1)
    {
        // 根據(jù)方向計(jì)算速度
        if (dir)
        {
            speed = (float)encoder_count * 60 / (float)(4 * TIM_GetCounter(ENCODER_TIM));
        }
        else
        {
            speed = (float)encoder_count * 60 / (float)(4 * TIM_GetCounter(ENCODER_TIM)) * -1;
        }
    }
}

void Encoder_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); // 使能GPIOA和AFIO時(shí)鐘
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); // 使能TIM2時(shí)鐘

    // 配置GPIOA.0和GPIOA.1為輸入模式,浮空輸入,上拉電阻關(guān)閉
    GPIO_InitStructure.GPIO_Pin = ENCODER_A_PIN | ENCODER_B_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // 配置定時(shí)器2為編碼器模式,計(jì)數(shù)器向上計(jì)數(shù),自動(dòng)重裝載值為0xFFFF,預(yù)分頻值為72M/72=1,溢出時(shí)產(chǎn)生中斷,時(shí)鐘源為內(nèi)部時(shí)鐘源
    TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(ENCODER_TIM, &TIM_TimeBaseStructure);

    // 配置定時(shí)器2通道1為復(fù)用功能模式,選擇輸入捕獲通道作為輸入源,上升沿觸發(fā)捕獲,不濾波,預(yù)分頻值為72M/72=1,計(jì)數(shù)器向上計(jì)數(shù),溢出時(shí)產(chǎn)生中斷,時(shí)鐘源為內(nèi)部時(shí)鐘源,禁用PWM輸出和死區(qū)控制,允許更新中斷和復(fù)位中斷,使能主從JK觸發(fā)器和邊沿對(duì)齊模式,使能高級(jí)控制寄存器的復(fù)位功能和SMS位清零功能,使能預(yù)分頻器的復(fù)位和SMS位清零功能,使能定時(shí)器2的復(fù)位和SMS位清零功能,使能定時(shí)器2的中斷和使能全局中斷
}
回復(fù)

使用道具 舉報(bào)

板凳
ID:1064841 發(fā)表于 2023-6-4 10:49 | 只看該作者
void EncoderT3_Init(void)
{
        //編碼器1
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
       
        //編碼器A相B相初始化
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        //定時(shí)器3初始化
        TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
        TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
        TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseInitStructure.TIM_Period = 65536 - 1;                //ARR
        TIM_TimeBaseInitStructure.TIM_Prescaler = 1 - 1;                //PSC
        TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
        TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
       
        //定時(shí)器3編碼器模式初始化
        TIM_ICInitTypeDef TIM_ICInitStructure;
        TIM_ICStructInit(&TIM_ICInitStructure);
        TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
        TIM_ICInitStructure.TIM_ICFilter = 0xF;
        TIM_ICInit(TIM3, &TIM_ICInitStructure);
        TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
        TIM_ICInitStructure.TIM_ICFilter = 0xF;
        TIM_ICInit(TIM3, &TIM_ICInitStructure);
       
        TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
       
        TIM_Cmd(TIM3, ENABLE);
}
僅供參考
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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