直流电机带光电编码器。
单片机源程序如下:
- #include "delay.h"
- #include "stm32f10x.h"
- #include "sys.h"
- #include "timer.h"
- #include "usart.h"
- #include "encoder.h"
- #include "oled.h"
- #include "pwm.h"
- #include "control.h"
- #include "led.h"
- #include "key.h"
- extern int Target_velocity;
- extern u16 Encoder; //左右编码器的脉冲计数
- extern u16 Moto1; //电机PWM变量
- u8 t=0;
- float speed;
- int main(void)
- {
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
-
- delay_init(); //延时函数初始化
- KEY_Init(); //初始化与按键连接的硬件接口
- OLED_Init();
- uart_init(9600); //=====串口初始化
- Encoder_Init_TIM2(); //=====编码器接口
-
- TIM3_Int_Init(99,7199); //10ms
-
- IN2=1;
- IN1=0;
-
- LED_Init(); //初始化与LED连接的硬件接口
-
- TIM1_PWM_Init(7199,0);//不分频。PWM频率=72000/(899+1)=80Khz
- while(1)
- {
-
- speed = Encoder*100/4/13/30;
- printf("speed:%0.2f rad/s\r\n",speed); //x100/4/13/30 //13线 30减速比 4分频
-
- OLED_ShowString(0,0,"speed:",16);
- OLED_ShowNum( 16,0,speed,3,16);
- OLED_ShowString(48,0,"rad/s",16);
- OLED_Refresh_Gram();//更新显示到OLED
-
-
- t=KEY_Scan(0); //得到键值
- switch(t)
- {
- case KEY0_PRES: Target_velocity++; break;
- case KEY1_PRES: Target_velocity--; break;
- default: delay_ms(10);
- }
- }
- }
复制代码
所有资料51hei提供下载:
a基于PID的调速直流电机成品.rar
(317.7 KB, 下载次数: 256)
|