步進(jìn)電機(jī)LCD顯示可設(shè)定轉(zhuǎn)數(shù)程序
掃描二維碼
隨時(shí)隨地手機(jī)看文章
步進(jìn)電機(jī)LCD顯示可設(shè)定轉(zhuǎn)數(shù)程序
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
#definedelayNOP();{_nop_();_nop_();_nop_();_nop_();};
ucharcodeFFW[8]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9};
ucharcodeREV[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1};
sbitK1=P1^4;//運(yùn)行與停止
sbitK2=P1^5;//設(shè)定圈數(shù)
sbitK3=P1^6;//方向轉(zhuǎn)換
sbitK4=P1^7;//速率調(diào)整
sbitBEEP=P3^7;//蜂鳴器
sbitLCD_RS=P2^0;
sbitLCD_RW=P2^1;
sbitLCD_EN=P2^2;
biton_off=0;//運(yùn)行與停止標(biāo)志
bitdirection=1;//方向標(biāo)志
bitrate_dr=1;//速率標(biāo)志
bitsnum_dr=1;//圈數(shù)標(biāo)志
ucharcodecdis1[]={"STEPPINGMOTOR"};
ucharcodecdis2[]={"CONTROLPROCESS"};
ucharcodecdis3[]={"STOP"};
ucharcodecdis4[]={"NUM:RATE:"};
ucharcodecdis5[]={"RUNNING"};
ucharm,v=0,q=0;
ucharnumber=0,number1=0;
ucharsnum=5,snum1=5;//預(yù)設(shè)定圈數(shù)
ucharrate=8;//預(yù)設(shè)定速率
uchardata_temp,data_temp1,data_temp2;
/********************************************************/
/*
/*延時(shí)t毫秒
/*11.0592MHz時(shí)鐘,延時(shí)約1ms
/*
/********************************************************/
voiddelay(uintt)
{
uchark;
while(t--)
{
for(k=0;k<125;k++)
{}
}
}
/********************************************************/
voiddelayB(ucharx)//x*0.14MS
{
uchari;
while(x--)
{
for(i=0;i<13;i++)
{}
}
}
/********************************************************/
voidbeep()
{
ucharj;
for(j=0;j<100;j++)
{
delayB(4);
BEEP=!BEEP;//BEEP取反
}
BEEP=1;//關(guān)閉蜂鳴器
delay(170);
}
/********************************************************/
/*
/*檢查LCD忙狀態(tài)
/*lcd_busy為1時(shí),忙,等待。為0時(shí),閑,可寫指令與數(shù)據(jù)。
/*
/********************************************************/
bitlcd_busy()
{
bitresult;
LCD_RS=0;
LCD_RW=1;
LCD_EN=1;
delayNOP();
result=(bit)(P0&0x80);
LCD_EN=0;
return(result);
}
/********************************************************/
/*
/*寫指令數(shù)據(jù)到LCD
/*RS=L,RW=L,E=高脈沖,D0-D7=指令碼。
/*
/********************************************************/
voidlcd_wcmd(ucharcmd)
{
while(lcd_busy());
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
_nop_();
_nop_();
P0=cmd;
delayNOP();
LCD_EN=1;
delayNOP();
LCD_EN=0;
}
/********************************************************/
/*
/*寫顯示數(shù)據(jù)到LCD
/*RS=H,RW=L,E=高脈沖,D0-D7=數(shù)據(jù)。
/*
/********************************************************/
voidlcd_wdat(uchardat)
{
while(lcd_busy());
LCD_RS=1;
LCD_RW=0;
LCD_EN=0;
P0=dat;
delayNOP();
LCD_EN=1;
delayNOP();
LCD_EN=0;
}
/********************************************************/
/*
/*LCD初始化設(shè)定
/*
/********************************************************/
voidlcd_init()
{
delay(30);
lcd_wcmd(0x38);//16*2顯示,5*7點(diǎn)陣,8位數(shù)據(jù)
delay(5);
lcd_wcmd(0x38);
delay(5);
lcd_wcmd(0x38);
delay(5);
lcd_wcmd(0x0c);//顯示開,關(guān)光標(biāo)
delay(5);
lcd_wcmd(0x06);//移動(dòng)光標(biāo)
delay(5);
lcd_wcmd(0x01);//清除LCD的顯示內(nèi)容
delay(5);
}
/********************************************************/
/*
/*設(shè)定顯示位置
/*
/********************************************************/
voidlcd_pos(ucharpos)
{
lcd_wcmd(pos|0x80);//數(shù)據(jù)指針=80+地址變量
}
/********************************************************/
/*
/*LCD1602初始顯示子程序
/*
/********************************************************/
voidLCD_init_DIS()
{
delay(10);//延時(shí)
lcd_init();//初始化LCD
lcd_pos(0);//設(shè)置顯示位置為第一行的第1個(gè)字符
m=0;
while(cdis1[m]!=’