- #include "reg52.h"
- void delay(unsigned int t);
- //Motor
- sbit F1 = P1^0;
- sbit F2 = P1^1;
- sbit F3 = P1^2;
- sbit F4 = P1^3;
- sbit k1 =P3^0;
- sbit k2=P3^1;
- sbit RX=P0^6; //
- sbit TX=P0^7;
- sbit shuidi=P0^3;
- sbit hongyai=P0^0;
- sbit yali=P0^2;
- bit flag1=0;
- bit flag2=0;
- signed int time=0;
- unsigned int timer=0;
- unsigned long S=0;
- unsigned char code FFW[8]={0xfe,0xfc,0xfd,0xf9,0xfb,0xf3,0xf7,0xf6}; //反转
- unsigned char code FFZ[8]={0xf6,0xf7,0xf3,0xfb,0xf9,0xfd,0xfc,0xfe}; //正转
- void Conut(void)
- {
- time=TH0*256+TL0;
- TH0=0;
- TL0=0;
-
- S= (long)(time*0.17); //算出来是CM
- if((S>=4000)||flag1==1) //
- {
- flag1=0;
- }
- }
- void zd0() interrupt 1 //T0中断用来计数器溢出,超过测距范围
- {
- flag1=1; //中断溢出标志
- }
- void panduan()
- {
- TMOD=0x11; //设T0为方式1,GATE=1;
- TH0=0;
- TL0=0;
- TH1=0xf8; //2MS定时
- TL1=0x30;
- ET0=1; //允许T0中断
- ET1=1; //允许T1中断
- TR1=1; //开启定时器
- EA=1; //总中断
- {
- while(!RX); //当RX为零时等待
- TR0=1; //开启计数
- while(RX); //当RX为1计数并等待
- TR0=0; //关闭计数
- Conut(); //计算
- if(S<80)
- flag2=0;
- else
- flag2=0;
- }
- //调节转速
- }
- void motor_FFW()
- {
- unsigned char i;
- {
- for (i=0; i<8; i++)
- {
- if(hongyai==0) P1 = FFW[i]&0x1f; //调取数据
- if(k2==0) P1 = FFW[i]&0x1f;
- if(yali==0) P1 = FFW[i]&0x1f;
- if(flag2==0)P1 = FFW[i]&0x1f;
-
- delay(1);
- }
- }
- }
- void motor_FFZ()
- {
- unsigned char i;
- {
- for (i=0; i<8; i++)
- {
- if(k1==0) P1 = FFZ[i]&0x1f;
- if(shuidi==0) P1 = FFZ[i]&0x1f;
-
-
- delay(1); //调节转速
- }
- }
- }
- void main()
- {
- while(1)
- {
- if(flag2==0)
- motor_FFW();
-
- if(shuidi==0)
- motor_FFZ();
- if(hongyai==0)
- motor_FFW();
- if(yali==0)
- motor_FFW();
- }
- }
复制代码
编译没有问题,程序烧录后控制不了五线四相步进电机
求大神解决!
|