利用STM32控制直流电机正反转及转速,通过labview发送指令给STM32控制电机的正反转及转速,并将电机的转速实时显示在波形图中
实验器材:
MiniSTM32F103开发板V3版本
实验目的:
本实验为新建工程实验,仅供大家新建工程时参考。
新建工程详细步骤,请看《STM32不完全手册-库函数版本》第3.3节。
实验现象:
本实验下载后,B0,B1交替闪烁
注意事项:
无.
部分程序预览:
- #include "moter.h"
- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "lcd.h"
- #include "pwm.h"
- #include "key.h"
- #include "timer.h"
- #include "encoder.h"
- #include "control.h"
- //#include "usmart.h"
- #include "malloc.h"
- #include "MMC_SD.h"
- #include "ff.h"
- #include "exfuns.h"
- #include "fontupd.h"
- #include "text.h"
- int main(void)
- {
- u32 fontcnt;
- u8 i,j;
- u8 fontx[2];//gbk码
- u8 key,t;
- u8 KEY_TEMP;
- char buffer[4]; //浮点转字符数组
- delay_init(); //延时函数初始化
- KEY_Init(); //初始化与按键连接的硬件接口
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
- uart_init(9600); //串口初始化为9600
- MOTER_Init(); //初始化与电机连接的硬件接口
- TIM1_PWM_Init(899,0);//不分频。PWM频率=72000/(899+1)=80Khz
- LCD_Init();
- //usmart_dev.init(72); //usmart初始化
- mem_init(); //初始化内存池
- exfuns_init(); //为fatfs相关变量申请内存
- f_mount(fs[0],"0:",1); //挂载SD卡
- f_mount(fs[1],"1:",1); //挂载FLASH.
- font_init();
- TIM3_Int_Init(99,7199);//10Khz的计数频率,计数到5000为500ms
- Encoder_Init_TIM2(); //初始化编码器1
- AIN0=1;AIN1=0;
- /***************PI 参数**********************/
- Kp=1.2;
- Ki=0.0011;
- Kd=6;
- Encoder_set=0;
- USART_RX_BUF[0]='a';
- while(1)
- {
- KEY_TEMP=KEY_Scan(0); //得到键值
- switch(KEY_TEMP)
- {
- case KEY0_PRES:
- Encoder_set+=1; break;
- case KEY1_PRES:
- Encoder_set-=1; if(Encoder_set=0)Encoder_set=0; break;
- default:break;
- }
- if(USART_RX_STA&0x8000)
- {
- USART_RX_STA=0;
- // Encoder_set=(USART_RX_BUF[1]-'0')*100+(USART_RX_BUF[2]-'0')*10+(USART_RX_BUF[3]-'0');
- Encoder_Err_Sum=0;//OutPwm=0;
- if(USART_RX_BUF[0]=='a') {AIN0=1;AIN1=0;Encoder_set=(USART_RX_BUF[1]-'0')*100+(USART_RX_BUF[2]-'0')*10+(USART_RX_BUF[3]-'0');}
- if(USART_RX_BUF[0]=='b') {AIN0=0;AIN1=1;Encoder_set=(USART_RX_BUF[1]-'0')*100+(USART_RX_BUF[2]-'0')*10+(USART_RX_BUF[3]-'0');}
- }
- POINT_COLOR=BLUE;
- Show_Str(0,30,240,24,"基于PI的电机速度控制",24,0);
- POINT_COLOR=BLACK;
- Show_Str(1,70,200,16,"电机速度",16,0); LCD_ShowxNum(130 ,70,LABview_moter,5,16,0); Show_Str(180,70,200,16,"转/分",16,0);
- sprintf(buffer, "%10.2f", LABview_wheel);
- Show_Str(1,90,200,16,"轮子速度",16,0);LCD_ShowString(90,90,200,16,16,buffer);Show_Str(180,90,200,16,"转/分",16,0);
- sprintf(buffer, "%10.2f", pwm_precent);
- Show_Str(1,110,200,16,"PWM百分比",16,0);
- POINT_COLOR=BLACK;
- if(pwm_precent<=20)POINT_COLOR=BLUE;
- if(pwm_precent>=80)POINT_COLOR=RED;
- LCD_ShowString(90,110,200,16,16,buffer);
- POINT_COLOR=BLACK;
- Show_Str(190,110,200,16,"%",16,0);
- Show_Str(1,130,200,16,"电机状态",16,0);
- if( (USART_RX_BUF[0]-'a')==0 ){
- Show_Str(150,130,200,16,"正转",16,0);}
- else{
- Show_Str(150,130,200,16,"反转",16,0);}
- //LCD_ShowxNum(180,170,Encoder_moter,5,16,0);
- POINT_COLOR=RED;
- Show_Str(50,210,200,16,"1304105022 方立超",16,0);
- Show_Str(50,230,200,16,"1304105025 张 冯",16,0);
- Show_Str(50,250,200,16,"1304105023 陆 炀",16,0);
- Show_Str(50,270,200,16,"1304105024 沈 磊",16,0);
- delay_ms(10);
- printf("%f", LABview_wheel);
- printf("\r\n");//插入换行
- }
- }
复制代码
|