上传者: 35501225
|
上传时间: 2022-03-18 16:14:04
|
文件大小: 2KB
|
文件类型: -
一次控制两个电机#include
#define GPIO_MOTOR P1
sbit K1=P3^6;
sbit K2=P3^5;
sbit K3=P3^4;
sbit K4=P3^3;
unsigned char code ZHENG1 [8]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9}; //正转顺序编码
unsigned char code FAN1 [8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1}; //反转顺序编码
unsigned char code ZHENG2 [8]={0x1f,0x3f,0x2f,0x6f,0x4f,0xcf,0x8f,0x9f}; //正转顺序编码
unsigned char code FAN2 [8]={0x9f,0x8f,0xcf,0x4f,0x6f,0x2f,0x3f,0x1f}; //反转顺序编码
char Motor1_Step,Motor2_Step,Speed,Speed2;
void Delay(unsigned int t);
void Motor1_zheng();
void Motor1_fan();
void Motor2_zheng();
void Motor2_fan();
void main()
{
unsigned int i;
Motor1_Step=1;
Motor2_Step=3;
Speed=10;
Speed2=40;
while(1)
{
while(K1==0)
{
for(i=0;i<10;i++)
{
Motor1_zheng();
}
}
while(K2==0)
{
for(i=0;i<10;i++)
{
Motor1_fan();
}
}
while(K3==0)
{
for(i=0;i<10;i++)
{
Motor2_zheng();
}
}
while(K4==0)
{
for(i=0;i<10;i++)
{
Motor2_fan();
}
}
}
}
void Motor1_fan()
{
unsigned int i;
for(i=0;i<8;i++)
{
//GPIO_MOTOR = FFW[i]&0x1f; //取数据
GPIO_MOTOR = FAN1[i];
Delay(Speed); //调节转速
}
}
void Motor1_zheng()
{
unsigned int i;
for(i=0;i<8;i++)
{
//GPIO_MOTOR = FFZ[i]&0x1f;
GPIO_MOTOR = ZHENG1[i];
Delay(Speed); //调节转速
}
}
void Motor2_fan()
{
unsigned int i;
if(Motor1_Step==0)
{
for(i=0;i<8;i++)
{
//GPIO_MOTOR = FFW2[i]&0xf1; //取数据
GPIO_MOTOR = FAN2[i];
Delay(Speed2); //调节转速
}
}
Motor1_Step=1;
}
void Motor2_zheng()
{
unsigned int i;
if(Motor1_Step==1)
{
for(i=0;i<8;i++)
{
//GPIO_MOTOR = FFZ2[i]&0xf1;
GPIO_MOTOR = ZHENG2[i];
Delay(Speed2); //调节转速
}
}
Motor1_Step=0;
}
void Delay(unsigned int t)
{
unsigned int k;
while(t--)
{
for(k=0; k<80; k++)
{ }
}
}