本程序是一个51单片机控制步进电机转动的程序,通过键盘设置转的圈数,电机能自动转设置的圈数,并且转的圈数和已经转过的圈数都能在液晶上显示出来。求大神帮忙写个程序流程图,先谢谢大佬们。以下是程序:
- #include <reg52.h>
- #include <intrins.h>
- #include "LCD1602.h" //添加液晶头文件
- #include "key.h" //添加1602头文件
- #define uchar unsigned char
- #define uint unsigned int
- uchar code FFW[8]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9};
- uchar code REV[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1};
- unsigned char speed=2; //档位
- unsigned char speed_1=30;//速度延迟
- unsigned char key_X=0; //0 运行,1 设置
- unsigned char set=0; //0 运行,1 设置
- /********************************************************/
- /*
- /* 延时t毫秒
- /* 11.0592MHz时钟,延时约1ms
- /*
- /********************************************************/
- void delay(uint t)
- {
- uint k;
- while(t--)
- {
- for(k=0; k<125; k++)
- { }
- }
- }
- /********************************************************/
- /*
- /*步进电机正转
- /*
- /********************************************************/
- void motor_ffw(uint n)
- {
- uchar i;
- uint j;
- for (j=0; j<n; j++) //转1×n圈
- {
- for (i=0; i<8; i++) //一个周期转30度
- {
- P1 = FFW[i]; //取数据
- delay(30); //调节转速
- }
- }
- }
- /********************************************************/
- /*
- /*步进电机反转
- /*
- /********************************************************/
- void motor_rev(uint n)
- {
- uchar i;
- uint j;
- for (j=0; j<n; j++) //转1×n圈
- {
- for (i=0; i<8; i++) //一个周期转30度
- {
- P1 = REV[i]; //取数据
- delay(30); //调节转速
- }
- }
- }
- void disp()
- {
- if(set==0)
- {
- write_1602_c(0x87);
- write_1602_d(key_X/100+0x30);
- write_1602_d(key_X%100/10+0x30);
- write_1602_d('.');
- write_1602_d(key_X%10+0x30);
- write_1602_c(0xc8);
- write_1602_d(speed%10+0x30);
- }
- if(set==1)
- {
- }
- }
- unsigned char k=16; //矩阵键盘值
- unsigned char k1=17; //矩阵键盘值
- void main(){
- unsigned char x1=0; //光标位置
- unsigned char quan=0; //圈数
- unsigned char xiaoshudian=0;//小数点
- unsigned char x2=0; //整数
- unsigned char x3=0; //小数
- LCD_init();
- write_1602_c(0x80);
- write_1602_d('M');
- write_1602_d('O');
- write_1602_d('T');
- write_1602_d('O');
- write_1602_d('R');
- write_1602_d(' ');
- write_1602_d(' ');
- write_1602_c(0x8C);
- write_1602_d(' ');
- write_1602_d('0');
- write_1602_d('.');
- write_1602_d('0');
- write_1602_c(0xC0);
- write_1602_d('S');
- write_1602_d('P');
- write_1602_d('E');
- write_1602_d('E');
- write_1602_d('D');
- while(1)
- {
- k=scan(); //矩阵键盘
- if(k!=k1)
- {
- if(set==1)
- if(k<10) //数字
- {
- if(x1==0)
- x2=k;
- if(x1==2)
- x3=k;
- write_1602_c(0x8D+x1);
- write_1602_d(k+'0');
- x1=x1+2;
- write_1602_c(0x8D+x1);
- write_1602_c(0x0f);//让光标闪烁
- }
- if(k==14) //开阀
- {
- set=0;
- write_1602_c(0x0C); //让光标停
- key_X=key_X+(x2*10+x3);
- disp();
- motor_rev(x2*10+x3); //电机转
- }
- if(k==15) //关阀
- {
- set=0;
- write_1602_c(0x0C); //让光标停
- if(key_X>=(x2*10+x3))
- {
- key_X=key_X-(x2*10+x3);
- disp();
- motor_ffw(x2*10+x3); //电机转
- }
- }
- if(k==10) //设置键
- {
- if(set==0)
- {
- set=1;
- x1=0;
- write_1602_c(0x8C);
- write_1602_d(' ');
- write_1602_d('0');
- write_1602_d('.');
- write_1602_d('0');
- write_1602_c(0x8D);
- write_1602_c(0x0f);//让光标闪烁
- }
- else
- {
- write_1602_c(0x0c);//让光标消失
- set=0;
- }
- }
-
- k1=k;
- if(k==12) //按加速
- {
- if(speed<4)
- {
- speed++;
- }
- if(speed==4)
- speed_1=30;
- if(speed==3)
- speed_1=50;
- if(speed==2)
- speed_1=70;
- if(speed==1)
- speed_1=90;
- }
- if(k==13) //按减速
- {
- if(speed>1)
- {
- speed--;
- }
- if(speed==4)
- speed_1=30;
- if(speed==3)
- speed_1=50;
- if(speed==2)
- speed_1=70;
- if(speed==1)
- speed_1=90;
- }
- }
- disp();
- }
- }
复制代码
|