|
采用80c51單片機進行測速調(diào)速。
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) //延時函數(shù)
{
unsigned char j;
while(ms--)
{
for(j=0;j<125;j++);
}
}
void delay()//延時函數(shù)
{
_nop_();
_nop_();
_nop_();
}
bit du()//LCD讀忙狀態(tài)
{
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寫數(shù)據(jù)
{
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)//延時函數(shù)
{
unsigned char i,j;
for(i=0;i<time;i++) ;
e=0;
for(j=100;j>1;j--) ;
}
void tiaosu(unsigned int shi1)//電機調(diào)速
{
kong=0;
delayms(5);
kong=1;
delayms(shi1);
}
unsigned char keycan()//按鍵返回值函數(shù)
{
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() //電機轉(zhuǎn)速數(shù)據(jù)處理
{
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 //中斷 刷新數(shù)據(jù)
{
unsigned char i;
TH0=0x3c;
TL0=0xb0; //50ms中斷一次
shi--;
if(shi<1)//1s取數(shù)據(jù)一次
{
shi=20;
y=(TH1*256+TL1);//紅外對管中斷次數(shù)
TH1=0x00;
TL1=0x00;
zhuanhuan();//數(shù)據(jù)轉(zhuǎn)換
xie(0xc6);
for(i=0;i<4;i++)//LCD數(shù)據(jù)處理顯示
{
xieshuju(zhuan[ i]);
delay();
}
}
}
void zhongd() interrupt 3//紅外對管中斷次數(shù)
{
TH1=0x00;
TL1=0x00;
}
電路圖
Snap4.jpg (134.84 KB, 下載次數(shù): 138)
下載附件
2015-4-23 13:41 上傳
|
評分
-
查看全部評分
|