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

QQ登錄

只需一步,快速開始

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

STM32mini板編碼器 源程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
編碼器計(jì)數(shù)主要是對(duì)單片機(jī)的定時(shí)器經(jīng)行配置,然后是讀取得到的值對(duì)其進(jìn)行轉(zhuǎn)換為速度等
  1. #include "sys.h"
  2. #include "delay.h"
  3. #include "usart.h"
  4. #include "encoder.h"
  5. #include "timer.h"
  6. #include "pwm.h"
  7. #include "motor.h"
  8. #include "led.h"
  9. #include "key.h"

  10.         int Encoder_Left=0 ,Encoder_Right=0;             //左右編碼器的脈沖計(jì)數(shù)
  11.         float speed_Left=0 ,speed_Right=0;
  12.        
  13.         int main(void)
  14. {

  15.                 delay_init();                     //延時(shí)函數(shù)初始化
  16.       uart_init(9600);                 //串口初始化為9600
  17.                 Encoder_Init_TIM3();
  18.                 Encoder_Init_TIM4();
  19.                 AIN_Init();
  20.                 KEY_Init();
  21.                 LED_Init();
  22.       TIM1_PWM_Init(899,8);
  23.                 TIM5_Int_Init(99,7199);   // 0.01S        ((arr+1)(psc+1))/72000000
  24.                                 while(1)
  25.     {   
  26.                           TIM_SetCompare1(TIM1,850);
  27.                          TIM_SetCompare4(TIM1,500);
  28.                          zuozhuan();
  29.                 /* 轉(zhuǎn)速(1秒鐘轉(zhuǎn)多少圈)=單位時(shí)間內(nèi)的計(jì)數(shù)值/總分辨率*時(shí)間系數(shù) */
  30.                     speed_Left=Encoder_Left/0.01*0.000181245;
  31.                          speed_Right=Encoder_Right/0.01*0.000181245;
  32.                            printf("左輪編碼器脈沖數(shù)為: %d\r\n",Encoder_Left);
  33.                                 printf("右輪編碼器脈沖數(shù)為: %d\r\n",Encoder_Right);
  34.                                 printf("speed_Left: %f\r\n",speed_Left);
  35.                                 printf("speed_Right:   %f\r\n",speed_Right);

  36.                         }
  37.                                                
  38. }
  39.        

  40.        
復(fù)制代碼



代碼: 編碼器.7z (194.87 KB, 下載次數(shù): 41)

評(píng)分

參與人數(shù) 1黑幣 +20 收起 理由
admin + 20 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏2 分享淘帖 頂1 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:891312 發(fā)表于 2021-10-29 12:50 | 只看該作者
請(qǐng)問(wèn)這用的什么編碼器,怎么收到的編碼器信號(hào)
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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