#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit IN1 = P1^0; //P20与电机驱动IN1相连
sbit IN2 = P1^1; //P21与电机驱动IN2相连
sbit IN3 = P1^2; //P22与电机驱动IN3相连
sbit IN4 = P1^3; //P23与电机驱动IN4相连
//P00左边模块
sbit D1 = P0^0; //P01中间模块
sbit D3 = P0^3; //P02右边模块
sbit D6 = P0^6;
uint temp;
void left_zheng(void);
void left_fan(void);
void right_zheng(void);
void right_fan(void);
void ting(void);
void go(void);
void back(void);
void delayms(uint z);
void main(void)
{
while(1)
{
// temp = P0&0xff;
if(D1 == 0 && D3 == 0 && D6 == 0)
{
ting();
}
if(D1 == 0 && D3 == 0 && D6 == 1)
{
left_zheng();
}
if(D1 == 0 && D3 == 1 && D6 == 0)
{
go();
}
if(D1 == 0 && D3 == 1 && D6 == 1)
{
left_zheng();
}
if(D1 == 1 && D3 == 0 && D6 == 0)
{
right_zheng();
}
if(D1 == 1 && D3 == 0 && D6 == 1)
{
go();
}
if(D1 == 1 && D3 == 1 && D6 == 0)
{
right_zheng();
}
if(D1 == 1 && D3 == 1 && D6 == 1)
{
ting();
}
/* switch(temp)
{
case 0x00: ting(); break;
case 0x01: right_zheng(); break;
case 0x02: go(); break;
case 0x03: go(); break;
case 0x04: left_zheng(); break;
case 0x05: go(); break;
case 0x06: go(); break;
case 0x07: ting(); break;
default: break;
} */
}
}
void left_zheng(void) //IN1 = 1, IN2 = 0;反转
{ //IN1 = 0, IN2 = 1;正转
IN1 = 0; //IN3 = 1, IN4 = 0;反转
IN2 = 0; //IN3 = 0, IN4 = 1;正转
IN3 = 0;
IN4 = 1;
delayms(100);
}
void left_fan(void)
{
IN1 = 0;
IN2 = 0;
IN3 = 1;
IN4 = 0;
delayms(50);
}
void right_zheng(void)
{
IN1 = 0;
IN2 = 1;
IN3 = 0; //IN1 = 0, IN2 = 0;制动
IN4 = 0;
delayms(100); //IN1 = 1, IN2 = 1;制动
}
void right_fan(void)
{
IN1 = 1;
IN2 = 0;
IN3 = 0;
IN4 = 0;
delayms(50);
}
void go(void)
{
IN1 = 0;
IN2 = 1;
IN3 = 0;
IN4 = 1;
delayms(30);
}
void back(void)
{
IN1 = 1;
IN2 = 0;
IN3 = 1;
IN4 = 0;
delayms(50);
}
void ting(void)
{
IN1 = 0;
IN2 = 0;
IN3 = 0;
IN4 = 0;
delayms(50);
}
void delayms(uint z)
{
uint i,j;
for(i = 0; i < z; i++)
for(j = 0; j < 110; j++);
}
|