MCU并联控制步进电机(电梯模拟)stc89c51单片机主控
电机转1圈为1楼层,电机运行:... 电机到站:up! new1:第一个电机对于的楼层 new2:第二个电机对于的楼层
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include "main.h"
- #include "Motor.h"
- #include "stdio.h"
- uint Key_Scan(void);
- uint res=0;
- void Motor_Muen(void)//菜单界面
- {
- LCD_Init();
- LCD_ShowString(1,1,"new1:01");
- LCD_ShowString(1,10,"new2:01");
- LCD_ShowString(2,6,"00");
- LCD_ShowString(2,10,"00");
- LCD_ShowString(2,1,"up!");
- LCD_ShowString(2,14,"up!");
- }
- //定时器初始化函数
- void Timer0Init(void) //1毫秒@22.1184MHz
- {
- TMOD = 0x01; //设置定时器模式
- TL0 = 0xCD; //设置定时初始值
- TH0 = 0xF8; //设置定时初始值
- TF0 = 0; //清除TF0标志
- ET0=1;
- // TR0 = 1; //定时器0开始计时
- EA=1;
- }
- int main(void) //主函数
- {
- uint keynum=0, //按键值
- enter=0, //确认
- Key_timer=0; //按键计时
- buzz1=0;buzz2=0;
- Motor_Muen();
- Timer0Init();
- while(1)
- {
- LCD_ShowString(2,1,"up!");
- LCD_ShowString(2,14,"up!");
- LCD_ShowNum(2,6,MotorKZ.Motor1_floor,2); //显示选择楼层
- LCD_ShowNum(2,10,MotorKZ.Motor2_floor,2);//显示选择楼层
- keynum=Key_Scan();
- switch(keynum)//按键标志位
- {
- case 1:MotorKZ.Motor1_floor++; //楼层加一
- break;
- case 2:MotorKZ.Motor1_floor--;//楼层减一
- break;
- case 3:enter=1;
- break;
- case 4:buzz1=~buzz1;
- break;
- case 5:MotorKZ.Motor2_floor++;//楼层加一
- break;
- case 6:MotorKZ.Motor2_floor--;//楼层减一
- break;
- case 7:enter=2;
- break;
- case 8:buzz2=~buzz2;
- break;
- }
- if(MotorKZ.Motor2_floor<0||MotorKZ.Motor1_floor<0){
- MotorKZ.Motor1_floor=0;MotorKZ.Motor2_floor=0;
- }
- if(enter==1)//电梯运行
- {
- enter=0;
- Motor1_circle();
- buzz1=1;Delay(200);buzz1=0;
- }else if(enter==2)//电梯运行
- {
- enter=0;
- Motor2_circle();
- buzz2=1;Delay(200);buzz2=0;
- }else if(enter==0);
-
- if(keynum==0)
- {
- if(Key_timer++>100)//按键时间记录
- {Key_timer=0;
- TR0=1;
- }
- }else TR0=0;
- if(res==0x0f)
- {res=0;
- while(MotorKZ.Motor1_new--)//回到初始化楼层
- {
- if(MotorKZ.Motor1_new<2)break;
- Motor_REV(1);
- LCD_ShowNum(1,6,MotorKZ.Motor1_new,2);
- }
- while(MotorKZ.Motor2_new--)//回到初始化楼层
- {
- if(MotorKZ.Motor2_new<2)break;
- Motor_REV(0);
- LCD_ShowNum(1,15,MotorKZ.Motor2_new,2);
- }
- // MotorKZ.Motor2_new=1; //初始化楼层
- // MotorKZ.Motor1_new=1; //初始化楼层
- LCD_ShowNum(1,6,MotorKZ.Motor1_new,2);
- LCD_ShowNum(1,15,MotorKZ.Motor2_new,2);
- }
- }
- }
- void Timer0(void) interrupt 1
- {
- static uint t=0,time=0,min=0;
-
- TL0 = 0xCD; //设置定时初始值
- TH0 = 0xF8; //设置定时初始值
- if(t++==599)//毫秒
- {
- t=0;
- if(time++==59) //秒
- {
- time=0;
- if(min==999)//分
- {
- min=0;
- }
- }
- }
- if(time==10)//设置超时时间time是秒,min是分钟这里设置的是30秒
- {
- time=0;
- TR0=0;res=0x0f;
-
- }
- }
- uint Key_Scan(void)//按键扫描
- {
- static uint keyflg=0;
- if((P3&0XFF)!=0XFF)
- {
- Delay(1);
- if((P3&0XFF)!=0XFF&&keyflg==0)
- {
- keyflg=1;
- switch(P3&0XFF)
- {
- case 0xfe:return 1;//按键1
- case 0xfd:return 2;//按键2
- case 0xfb:return 3;//按键3
- case 0xf7:return 4;//按键4
- case 0xef:return 5;//按键5
- case 0xdf:return 6;//按键6
- case 0xbf:return 7;//按键7
- case 0x7f:return 8;//按键8
- }
- }
- }else keyflg=0;
- return 0;
- }
复制代码
Keil代码与Proteus8.13仿真下载:
并联控制步进电机(电梯模拟).zip
(318.2 KB, 下载次数: 50)
|