标题:
请问C语言运行完第一次后再运行第二次时case4和case5后面的mg函数为何不能正常运行
[打印本页]
作者:
58877guang
时间:
2020-4-30 23:14
标题:
请问C语言运行完第一次后再运行第二次时case4和case5后面的mg函数为何不能正常运行
#include<reg51.h>
#include<math.h> //包含单片机寄存器的头文件
#define uchar unsigned char
#define uint unsigned int
#define MotorData P0 //水平控制接口定义
#define MG90 P2 // 竖直控制接口定义
uchar phasecw[4] ={0x08,0x04,0x02,0x01};//正转 x导通相序 D-C-B-A
uchar phaseccw[4]={0x01,0x02,0x04,0x08};
uchar mg9cw[4] ={0x08,0x04,0x02,0x01};//正转 y导通相序 D-C-B-A
uchar mg9ccw[4]={0x01,0x02,0x04,0x08};
sbit K5=P3^5;
sbit L0=P1^0;
sbit L1=P1^1;
sbit L2=P1^2;
sbit L3=P1^3;
sbit L4=P1^4;
sbit L5=P1^5;
int z;z=0 ;//将z初始化为0
int g;
int h;
int a;
a=0;
int b;
b=0;
/*****************************
函数功能:延时一段时间
*****************************/
void Delay_xms(uint x)
{
uint i,j;
for(i=0;i<x;i++)
for(j=0;j<112;j++);
}
void MotorCW(void)//x顺时针转动
{
uchar i;
for(i=0;i<4;i++) //循环输出脉冲
{
MotorData=phasecw[i];
Delay_xms(2);//转速调节
}
}
mg90CW() //y顺时针
{
if (a==0)
{
uchar i;
for(i=0;i<4;i++)
{
MG90=mg9cw[i];
g++;
//角度计数,0-512
Delay_xms(2);//转速调节
a=(g>512);
}
}
else
{
h=512;
}
return g;
}
void MotorCCW(void)//逆时针转动
{
uchar i;
for(i=0;i<4;i++)
{
MotorData=phaseccw[i];
Delay_xms(2);//转速调节
}
}
mg90CCW()//y逆时针
{
if (b==0)
{
uchar i;
for(i=0;i<4;i++)
{
MG90=mg9ccw[i];
h--; //角度计数 512-0
Delay_xms(2);//转速调节
b=(h<0);
}
}
else
{
g=0;
}
return h;
}
//停止转动
void MotorStop(void)
{
MotorData=0x00;
}
void mg90Stop(void)//y停止
{
MG90=0x00;
}
/*****************************
函数功能:主函数
*****************************/
main()
{
while(1)
{
if(K5==0) //如果键按下
{
Delay_xms(1000); //延时一段时间 按键消抖
if(K5==0)//如果再次检测到S1键按下
z++; //z自增1
if(z==7) //如果z=7,重新将其置为1
z=1;
}
switch(z) //使用多分支选择语句
{
case 1: MotorCCW(),L0=0,L1=1,L2=1; //第一个LED亮
break;
case 2: MotorCW(),L1=0,L0=1,L2=1; //第二个LED亮
break;
case 3: MotorStop(),L2=0,L1=1,L0=1,h=512;
break; //第三个LED亮
case 4: mg90CCW(),L3=0,L4=1,L5=1,g=0; //第四个LED亮 ,y逆时针
break;
case 5: mg90CW(),L4=0,L3=1,L5=1; //第五个LED亮 ,y顺时针
break;
case 6: mg90Stop(),L5=0,L3=1,L4=1,h=512,g=0; //第六个LED亮
break;
}
}
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1