采用80c51单片机进行测速调速。
nclude <reg51.h>
#include <intrins.h>
unsigned char code lcd[]={"speed0k"};
unsigned char code table[]= {"0123456789"};
unsigned char code pin[]={"Hz"};
unsigned char zhuan[5];
unsigned char shi=20;
unsigned int y,jia;
sbit rd=P2^7;
sbit rw=P2^6;
sbit e=P2^5;
sbit kong=P3^0;
void delay1(unsigned char ms) //延时函数
{
unsigned char j;
while(ms--)
{
for(j=0;j<125;j++);
}
}
void delay()//延时函数
{
_nop_();
_nop_();
_nop_();
}
bit du()//LCD读忙状态
{
bit s;
rw=1;
rd=0;
delay();
e=1;
delay();
s=(bit)(P0&0x80);
e=0;
return s;
}
void xie(unsigned char com)//LCD写命令
{
while (du());
rd=0;
rw=0;
delay();
e=0;
P0=com;
e=1;
delay();
// delay1(20);
}
void xieshuju(unsigned char dat)//LCD写数据
{
while (du());
rw=0;
rd=1;
delay();
e=0;
P0=dat;
e=1;
delay();
e=0;
}
void chushihua()//LCD初始化
{
xie(0x38);
xie(0x0c);
xie(0x01);
xie(0x06);
}
void delayms (unsigned char time)//延时函数
{
unsigned char i,j;
for(i=0;i<time;i++) ;
e=0;
for(j=100;j>1;j--) ;
}
void tiaosu(unsigned int shi1)//电机调速
{
kong=0;
delayms(5);
kong=1;
delayms(shi1);
}
unsigned char keycan()//按键返回值函数
{
static unsigned char key;
if(P3!=0xff)
{
delay1(10);
if(P3!=0xff)
key=P3&0Xff;
if(P3==0xfd)
jia=jia+5;
if(P3==0xfb)
jia=jia-5;
if(jia<=5)
jia=3;
}
while(P3!=0xff);
return(key);
}
void zhuanhuan() //电机转速数据处理
{
zhuan[0]=table[y/1000%10];
zhuan[1]=table[y/100%10];
zhuan[2]=table[y/10%10];
zhuan[3]=table[y%10];
}
void main ()
{
unsigned char wei;
unsigned char w=0;
EA=1;
TR0=1;
ET0=1;
TR1=1;
ET1=1;
TH0=0x3c;
TL0=0xb0;
TH1=0x00;
TL1=0x00;
TMOD=0x51;
chushihua();
delay1(20); //在这里如果时间不过长的话 控制位置无用
xie(0x84);
delay();
while (lcd[w]!='\0')//LCD显示cesuji
{
xieshuju(lcd[w]);
w++;
delay();
}
w=0;
xie(0xca);
while (pin[w]!='\0')//LCD显示hz
{
xieshuju(pin[w]);
w++;
delay();
}
while (1)
{
wei=keycan();
switch (wei)
{
case 0xfe: kong=0; break;//电机全速启动
case 0xfd:
case 0xfb: tiaosu(jia);break;
case 0xf7: kong=1; break;//电机停止
}
}
}
void zhongduan() interrupt 1 //中断 刷新数据
{
unsigned char i;
TH0=0x3c;
TL0=0xb0; //50ms中断一次
shi--;
if(shi<1)//1s取数据一次
{
shi=20;
y=(TH1*256+TL1);//红外对管中断次数
TH1=0x00;
TL1=0x00;
zhuanhuan();//数据转换
xie(0xc6);
for(i=0;i<4;i++)//LCD数据处理显示
{
xieshuju(zhuan[ i]);
delay();
}
}
}
void zhongd() interrupt 3//红外对管中断次数
{
TH1=0x00;
TL1=0x00;
}
电路图
|