跟大家分享一个程序,用单片机控制电机的转速
时间:10-02
整理:3721RD
点击:
跟大家分享一个程序,用单片机控制电机的转速
#include<reg51.h>
#include<intrins.h>
sbit input1=P2^0;
sbit input2=P2^1;
sbit K1=P3^0;
sbit K2=P3^1;
sbit K3=P3^2;
sbit K4=P3^3;
sbit ENA=P2^2;
bit m,n;
unsigned char PWM=0x7f;
void delayms(unsigned char ms)
{
unsigned char i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
void init()
{
P1=0xff;
TMOD=0x21;
TH0=0xfe;
TL0=0xff;
TH1=PWM;
TL1=0;
EA=1;
ET0=1;
ET1=1;
TR0=1;
m=0;
n=0;
}
void main()
{
init();
while(1)
{
if(K4==0)
{m=~m;while(!K4);}
if(m==1)
{
if(K3==0)
{n=~n;while(!K3);}
if(n==0)
{
input1=1;
input2=0;
}
else
{
input1=0;
input2=1;
}
}
else
{
input1=0;
input2=0;
n=0;
}
do{
if(PWM!=0xff)
{PWM++;delayms(5);}
}
while(K1==0);
do{
if(PWM!=0x01)
{PWM--;delayms(5);}
}
while(K2==0);
}
}
void timer0() interrupt 1
{
TR1=0;
TH0=0xfe;
TL0=0xff;
TH1=PWM;
TR1=1;
ENA=0;
}
void timer1() interrupt 3
{
TR1=0;
ENA=1;
}
#include<reg51.h>
#include<intrins.h>
sbit input1=P2^0;
sbit input2=P2^1;
sbit K1=P3^0;
sbit K2=P3^1;
sbit K3=P3^2;
sbit K4=P3^3;
sbit ENA=P2^2;
bit m,n;
unsigned char PWM=0x7f;
void delayms(unsigned char ms)
{
unsigned char i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
void init()
{
P1=0xff;
TMOD=0x21;
TH0=0xfe;
TL0=0xff;
TH1=PWM;
TL1=0;
EA=1;
ET0=1;
ET1=1;
TR0=1;
m=0;
n=0;
}
void main()
{
init();
while(1)
{
if(K4==0)
{m=~m;while(!K4);}
if(m==1)
{
if(K3==0)
{n=~n;while(!K3);}
if(n==0)
{
input1=1;
input2=0;
}
else
{
input1=0;
input2=1;
}
}
else
{
input1=0;
input2=0;
n=0;
}
do{
if(PWM!=0xff)
{PWM++;delayms(5);}
}
while(K1==0);
do{
if(PWM!=0x01)
{PWM--;delayms(5);}
}
while(K2==0);
}
}
void timer0() interrupt 1
{
TR1=0;
TH0=0xfe;
TL0=0xff;
TH1=PWM;
TR1=1;
ENA=0;
}
void timer1() interrupt 3
{
TR1=0;
ENA=1;
}
bit和sbit各是什么意思?