新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 51單片機控制步進電機轉(zhuǎn)動以及用lcd1602顯示

51單片機控制步進電機轉(zhuǎn)動以及用lcd1602顯示

作者: 時間:2016-11-28 來源:網(wǎng)絡(luò) 收藏
#include
#include
#define uchar unsigned char
#define uintunsigned int
#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};
uchar code FFW[8]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9};//30度
uchar code REV[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1};//30度
sbit K1 = P1^4; //運行與停止
sbit K2 = P1^5; //設(shè)定圈數(shù)
sbit K3 = P1^6; //方向轉(zhuǎn)換
sbit K4 = P1^7; //速率調(diào)整
sbit BEEP = P3^7; //蜂鳴器
sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_EN = P2^2;
bit on_off=0; //運行與停止標(biāo)志
bit direction=1; //方向標(biāo)志
bit rate_dr=1; //速率標(biāo)志
bit snum_dr=1; //圈數(shù)標(biāo)志
uchar code cdis1[ ] = {" STEPPING MOTOR "};
uchar code cdis2[ ] = {"CONTROL PROCESS"};
uchar code cdis3[ ] = {" STOP "};
uchar code cdis4[ ] = {"NUM: RATE: "};
uchar code cdis5[ ] = {" RUNNING "};
uchar m,v=0,q=0;
uchar number=0,number1=0;
uchar snum=5,snum1=5; //預(yù)設(shè)定圈數(shù)
uchar rate=8; //預(yù)設(shè)定速率
uchar data_temp,data_temp1,data_temp2;
void delay(uint t)
{
uchar k;
while(t--)
{
for(k=0; k<125; k++)
{ }
}
}
void delayB(uchar x) //x*0.14MS
{
uchar i;
while(x--)
{
for (i=0; i<13; i++)
{ }
}
}
void beep()
{
uchar j;
for (j=0;j<180;j++)
{
delayB(5);
BEEP=!BEEP; //BEEP取反
}
BEEP=1; //關(guān)閉蜂鳴器
delay(170);
}
bit lcd_busy()
{
bit result;
LCD_RS = 0;
LCD_RW = 1;
LCD_EN = 1;
delayNOP();
result = (bit)(P0&0x80);
LCD_EN = 0;
return(result);
}
void lcd_wcmd(uchar cmd)
{
while(lcd_busy());
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
_nop_();
_nop_();
P0 = cmd;
delayNOP();
LCD_EN = 1;
delayNOP();
LCD_EN = 0;
}
void lcd_wdat(uchar dat)
{
while(lcd_busy());
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 0;
P0 = dat;
delayNOP();
LCD_EN = 1;
delayNOP();
LCD_EN = 0;
}
void lcd_init()
{
delay(30);
lcd_wcmd(0x38); //16*2顯示,5*7點陣,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); //移動光標(biāo)
delay(5);
lcd_wcmd(0x01); //清除LCD的顯示內(nèi)容
delay(5);
}
void lcd_pos(uchar pos)
{
lcd_wcmd(pos | 0x80); //數(shù)據(jù)指針=80+地址變量
}
void LCD_init_DIS()
{
delay(10); //延時
lcd_init(); //初始化LCD
lcd_pos(0); //設(shè)置顯示位置為第一行的第1個字符
m = 0;
while(cdis1[m] !=