51单片机如何用C语言控制DC电机正反转
# include & ltreg52.h & gt
#定义uint无符号整数
#定义uchar无符号字符
sbit in1=p1^0;
sbit ena=p1^1;//左轮1
sbit in2=p1^2;//左轮2
sbit in3=p1^3;//右轮1
sbit enb=p1^4;
sbit in4=p1^5;//右滚轮2
sbit left=p0^0;
sbit right=p0^1;
//sbit middle=p0^2;
无效延迟(单位z)
{
uint x,y;
for(x = z;x & gt0;x -)
for(y = 110;y & gt0;y-);
}
void初始化()
{
ENA = 0;// 1
ENB = 0;//
in 1 = 0;// 0
IN2 = 0;// 2
IN3 = 0;
IN4 = 0;
}
无效pwm()
{
ENA = 1;
ENB = 1;
}
虚空典纪()
{
if(left = = 0 & amp;& ampRight==0) // Forward
{
in 1 = 1;
IN2 = 0;
IN3 = 1;
IN4 = 0;
PWM();
}
else if(left = = 0 & amp;& ampRight==1) //右转。
{
in 1 = 1;
IN2 = 0;
IN3 = 0;
IN4 = 1;
PWM();
} else if(left = 1 & amp;& ampRight==0)//左转
{
in 1 = 0;
IN2 = 1;
IN3 = 1;
IN4 = 0;
PWM();
}
}
void main()
{
init();
while(1)
{
殿基();
}
}