編碼器計數主要是對單片機的定時器經行配置,然后是讀取得到的值對其進行轉換為速度等
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "encoder.h"
- #include "timer.h"
- #include "pwm.h"
- #include "motor.h"
- #include "led.h"
- #include "key.h"
- int Encoder_Left=0 ,Encoder_Right=0; //左右編碼器的脈沖計數
- float speed_Left=0 ,speed_Right=0;
-
- int main(void)
- {
- delay_init(); //延時函數初始化
- uart_init(9600); //串口初始化為9600
- Encoder_Init_TIM3();
- Encoder_Init_TIM4();
- AIN_Init();
- KEY_Init();
- LED_Init();
- TIM1_PWM_Init(899,8);
- TIM5_Int_Init(99,7199); // 0.01S ((arr+1)(psc+1))/72000000
- while(1)
- {
- TIM_SetCompare1(TIM1,850);
- TIM_SetCompare4(TIM1,500);
- zuozhuan();
- /* 轉速(1秒鐘轉多少圈)=單位時間內的計數值/總分辨率*時間系數 */
- speed_Left=Encoder_Left/0.01*0.000181245;
- speed_Right=Encoder_Right/0.01*0.000181245;
- printf("左輪編碼器脈沖數為: %d\r\n",Encoder_Left);
- printf("右輪編碼器脈沖數為: %d\r\n",Encoder_Right);
- printf("speed_Left: %f\r\n",speed_Left);
- printf("speed_Right: %f\r\n",speed_Right);
-
- }
-
- }
-
-
復制代碼
代碼:
編碼器.7z
(194.87 KB, 下載次數: 41)
2021-8-11 15:57 上傳
點擊文件名下載附件
|