微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 51单片机控制步进电机转动以及用lcd1602显示

51单片机控制步进电机转动以及用lcd1602显示

时间:11-28 来源:互联网 点击:
#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; //设定圈数

sbit K3 = P1^6; //方向转换

sbit K4 = P1^7; //速率调整

sbit BEEP = P3^7; //蜂鸣器

sbit LCD_RS = P2^0;

sbit LCD_RW = P2^1;

sbit LCD_EN = P2^2;

bit on_off=0; //运行与停止标志

bit direction=1; //方向标志

bit rate_dr=1; //速率标志

bit snum_dr=1; //圈数标志

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; //预设定圈数

uchar rate=8; //预设定速率

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; //关闭蜂鸣器

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位数据

delay(5);

lcd_wcmd(0x38);

delay(5);

lcd_wcmd(0x38);

delay(5);

lcd_wcmd(0x0c); //显示开,关光标

delay(5);

lcd_wcmd(0x06); //移动光标

delay(5);

lcd_wcmd(0x01); //清除LCD的显示内容

delay(5);

}

void lcd_pos(uchar pos)

{

lcd_wcmd(pos | 0x80); //数据指针=80+地址变量

}

void LCD_init_DIS()

{

delay(10); //延时

lcd_init(); //初始化LCD

lcd_pos(0); //设置显示位置为第一行的第1个字符

m = 0;

while(cdis1[m] != )

{ //显示字符

lcd_wdat(cdis1[m]);

m++;

}

lcd_pos(0x40); //设置显示位置为第二行第1个字符

m = 0;

while(cdis2[m] != )

{

lcd_wdat(cdis2[m]); //显示字符

m++;

}

delay(3000); //延时

lcd_pos(0); //设置显示位置为第一行的第1个字符

m = 0;

while(cdis3[m] != )

{ //显示字符

lcd_wdat(cdis3[m]);

m++;

}

lcd_pos(0x40); //设置显示位置为第二行第1个字符

m = 0;

while(cdis4[m] != )

{

lcd_wdat(cdis4[m]); //显示字符

m++;

}

for(m=0;m<2;m++)

{

lcd_pos(0x0c+m); //显示方向符号

lcd_wdat(0x3e);

}

}

void data_conv()

{

data_temp1=data_temp/10; //高位

if(data_temp1==0)

{data_temp1=0x20;} //高位为0不显示

else

{data_temp1=data_temp1+0x30;}

data_temp2=data_temp; //低位

data_temp2=data_temp2+0x30;

}

void data_dis()

{

data_temp = snum; //显示圈数

data_conv();

lcd_pos(0x44);

lcd_wdat(data_temp1);

lcd_pos(0x45);

lcd_wdat(data_temp2);

data_temp = rate; //显示速率

data_conv();

lcd_pos(0x4d);

lcd_wdat(data_temp1);

lcd_pos(0x4e);

lcd_wdat(data_temp2);

}

void motor_DR()

{

if(direction==1) //正转方向标志

{ for(m=0;m<2;m++)

{

lcd_pos(0x0c+m); //显示方向符号

lcd_wdat(0x3e);

}

}

else

{ for(m=0;m<2;m++) //反转方向标志

{

lcd_pos(0x0c+m); //显示方向符号

lcd_wdat(0x3c);

}

}

}

void motor_RUN()

{

if(on_off==1)

{ TR0=1;

lcd_pos(0); //设置显示位置为第一行的第1个字符

m = 0;

while(cdis5[m] != )

{ lcd_wdat(cdis5[m]); //RUNNING

m++; }

motor_DR(); //

}

else

{ TR0=0; P1 =0xf0;

lcd_pos(0); //设置显示位置为第一行的第1个字符

m = 0;

while(cdis3[m] != )

{ lcd_wdat(cdis3[m]); //STOP

m++; }

motor_DR(); //

snum=snum1; //

number1=0; //清圈数计数器

}

}

main()

{

LCD_init_DIS();

TMOD = 0x01; //T0定时方式1

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top