哪位大侠有51单片机控制直流电机的源程序
展开全部
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit PWM11 = P1^0; //motora
sbit PWM12 = P1^1;
sbit PWM21 = P1^2; //motorb
sbit PWM22 = P1^3;
sbit MC1 = P1^4; //motora enable
sbit MC2 = P1^5; //motorb enable
sbit LED = P1^6; //指示灯
sbit FMQ = P1^7; //蜂鸣器
sbit key_qt = P0^0; //motor2 引脚
sbit key_sph = P0^1; //升速键
sbit key_spl = P0^2; //降速键
sbit limita1 = P0^3; //电机正a限位
sbit limita2 = P0^4; //电机负a限位
sbit limitb1 = P0^5; //电机正b限位
sbit limitb2 = P0^6; //电机负b限位
uchar keyval,cnt=0,cnt1=0,pwm=50,num;
uchar m_mode=0; //电机运行模式,1-电机正向;2-电机b正反向;3=电机b反向;4-电机a反向;
bit flag=0; //延时时间到标志
uchar runflag=0; //电机运行标志
void delaynms(uint z)
{
uint i,j;
for(i = z;i > 0;i--)
for(j = 110;j > 0;j--);
}
void keychk(void)
{
keyval=0xff;
if(key_qt==0)
{
delaynms(5); //消抖
if(key_qt==0) //确定按键按下
{
while(key_qt==0);
keyval=1;
}
}
if(!key_sph)
{
delaynms(5); //消抖
if(!key_sph) //确定按键按下
{
while(!key_sph);
keyval=2;
}
}
if(!key_spl)
{
delaynms(5); //消抖
if(!key_spl) //确定按键按下
{
while(!key_spl);
keyval=3;
}
}
}
void t0isr() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
if(runflag==1) //正常运行
{
cnt1++;
if(cnt1>=10){cnt1=0;LED=~LED;}
}
if(runflag==2) //限位延时
{
LED=1;
cnt++;
if(cnt>=10)
{
flag=1;
cnt=0;
}
}
}
void t1isr() interrupt 3
{
num++;
if(num<pwm)
{
switch(m_mode)
{
case 1:PWM11=1;PWM12=0;break; //a电机正转
case 2:PWM21=1;PWM22=0;break; //b电机正转
case 3:PWM21=0;PWM22=1;break; //b电机反转
case 4:PWM11=0;PWM12=1;break; //a电机反转
default:break;
}
}
else if(num>=pwm)
{
PWM11=0;PWM12=0;PWM21=0;PWM22=0;
}
if(num>=100)num=0;
}
void main(void)
{
bit qt=0;
TMOD=0x21;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=236; //改变该值调整PWM频率
TL1=236;
TR0=1;
TR1=1;
ET1=1;
ET0=1;
EA=1;
P1|=0xc0;
flag=0;
while(1)
{
keychk();
switch(keyval)
{
case 1: qt=~qt;
if(qt)
{
runflag=1;
switch(m_mode)
{
case 0: m_mode=1; //刚开始启动,置模式1
MC1=1;
FMQ=0; //蜂鸣器响一下
delaynms(100);
FMQ=1;
break;
case 1:MC1=1;break; //电机a正转
case 2:MC2=1;break; //电机b正转
case 3:MC2=1;break; //电机b反转
case 4:MC1=1;break; //电机a反转
default:m_mode=0;break;
}
}
if(!qt)
{
MC1=0;
MC2=0;
LED=1;
runflag=0;
break;
}
break;
case 2:pwm+=5;if(pwm>100)pwm=100;break; //升速5个单位
case 3:pwm-=5;if(pwm<10)pwm=10;break; //降速5个单位
default:break;
}
if(limita1==0 && m_mode==1) //电机a正向限位
{
delaynms(1);
if(limita1==0)
{
MC1=0;
runflag=2;
flag=0;
while(flag==0); //等待延时0.5S结束
m_mode=2;
runflag=1;
MC2=1;
}
}
if(limitb1==0 && m_mode==2) //电机b正向限位
{
delaynms(1);
if(limitb1==0)
{
MC2=0;
runflag=2;
flag=0;
while(flag==0);
qt=0;
m_mode=3;
}
}
if(limitb2==0 && m_mode==3) //电机b反向限位
{
delaynms(1);
if(limitb2==0)
{
MC2=0;
runflag=2;
flag=0;
while(flag==0);
m_mode=4;
runflag=1;
MC1=1;
}
}
if(limita2==0 && m_mode==4) //电机a反向限位
{
delaynms(1);
if(limita2==0)
{
MC1=0;
runflag=2;
flag=0;
while(flag==0);
flag=0;
m_mode=0;
runflag=0;
qt=0;
LED=1;
}
}
}
}
#define uchar unsigned char
#define uint unsigned int
sbit PWM11 = P1^0; //motora
sbit PWM12 = P1^1;
sbit PWM21 = P1^2; //motorb
sbit PWM22 = P1^3;
sbit MC1 = P1^4; //motora enable
sbit MC2 = P1^5; //motorb enable
sbit LED = P1^6; //指示灯
sbit FMQ = P1^7; //蜂鸣器
sbit key_qt = P0^0; //motor2 引脚
sbit key_sph = P0^1; //升速键
sbit key_spl = P0^2; //降速键
sbit limita1 = P0^3; //电机正a限位
sbit limita2 = P0^4; //电机负a限位
sbit limitb1 = P0^5; //电机正b限位
sbit limitb2 = P0^6; //电机负b限位
uchar keyval,cnt=0,cnt1=0,pwm=50,num;
uchar m_mode=0; //电机运行模式,1-电机正向;2-电机b正反向;3=电机b反向;4-电机a反向;
bit flag=0; //延时时间到标志
uchar runflag=0; //电机运行标志
void delaynms(uint z)
{
uint i,j;
for(i = z;i > 0;i--)
for(j = 110;j > 0;j--);
}
void keychk(void)
{
keyval=0xff;
if(key_qt==0)
{
delaynms(5); //消抖
if(key_qt==0) //确定按键按下
{
while(key_qt==0);
keyval=1;
}
}
if(!key_sph)
{
delaynms(5); //消抖
if(!key_sph) //确定按键按下
{
while(!key_sph);
keyval=2;
}
}
if(!key_spl)
{
delaynms(5); //消抖
if(!key_spl) //确定按键按下
{
while(!key_spl);
keyval=3;
}
}
}
void t0isr() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
if(runflag==1) //正常运行
{
cnt1++;
if(cnt1>=10){cnt1=0;LED=~LED;}
}
if(runflag==2) //限位延时
{
LED=1;
cnt++;
if(cnt>=10)
{
flag=1;
cnt=0;
}
}
}
void t1isr() interrupt 3
{
num++;
if(num<pwm)
{
switch(m_mode)
{
case 1:PWM11=1;PWM12=0;break; //a电机正转
case 2:PWM21=1;PWM22=0;break; //b电机正转
case 3:PWM21=0;PWM22=1;break; //b电机反转
case 4:PWM11=0;PWM12=1;break; //a电机反转
default:break;
}
}
else if(num>=pwm)
{
PWM11=0;PWM12=0;PWM21=0;PWM22=0;
}
if(num>=100)num=0;
}
void main(void)
{
bit qt=0;
TMOD=0x21;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=236; //改变该值调整PWM频率
TL1=236;
TR0=1;
TR1=1;
ET1=1;
ET0=1;
EA=1;
P1|=0xc0;
flag=0;
while(1)
{
keychk();
switch(keyval)
{
case 1: qt=~qt;
if(qt)
{
runflag=1;
switch(m_mode)
{
case 0: m_mode=1; //刚开始启动,置模式1
MC1=1;
FMQ=0; //蜂鸣器响一下
delaynms(100);
FMQ=1;
break;
case 1:MC1=1;break; //电机a正转
case 2:MC2=1;break; //电机b正转
case 3:MC2=1;break; //电机b反转
case 4:MC1=1;break; //电机a反转
default:m_mode=0;break;
}
}
if(!qt)
{
MC1=0;
MC2=0;
LED=1;
runflag=0;
break;
}
break;
case 2:pwm+=5;if(pwm>100)pwm=100;break; //升速5个单位
case 3:pwm-=5;if(pwm<10)pwm=10;break; //降速5个单位
default:break;
}
if(limita1==0 && m_mode==1) //电机a正向限位
{
delaynms(1);
if(limita1==0)
{
MC1=0;
runflag=2;
flag=0;
while(flag==0); //等待延时0.5S结束
m_mode=2;
runflag=1;
MC2=1;
}
}
if(limitb1==0 && m_mode==2) //电机b正向限位
{
delaynms(1);
if(limitb1==0)
{
MC2=0;
runflag=2;
flag=0;
while(flag==0);
qt=0;
m_mode=3;
}
}
if(limitb2==0 && m_mode==3) //电机b反向限位
{
delaynms(1);
if(limitb2==0)
{
MC2=0;
runflag=2;
flag=0;
while(flag==0);
m_mode=4;
runflag=1;
MC1=1;
}
}
if(limita2==0 && m_mode==4) //电机a反向限位
{
delaynms(1);
if(limita2==0)
{
MC1=0;
runflag=2;
flag=0;
while(flag==0);
flag=0;
m_mode=0;
runflag=0;
qt=0;
LED=1;
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询