步进电机控制及其LCD显示

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includeAT89X51.hintdelay();voidinti_lcd();voidshow_lcd(int);voidcmd_wr();voidShowState();voidclock(unsignedintDelay);voidDoSpeed();//计算速度//正转值#defineRIGHT_RUN1//反转值#defineLEFT_RUN0sbitRS=0xA0;sbitRW=0xA1;sbitE=0xA2;charSpeedChar[]=SPEED(n/min):;charStateChar[]=RUNSTATE:;charSTATE_CW[]=CW;charSTATE_CCW[]=CCW;charSPEED[3]=050;unsignedintRunSpeed=50;//速度unsignedcharRunState=RIGHT_RUN;//运行状态main(){/*定时器设置*/TMOD=0x66;//定时器0,1都为计数方式;方式2;EA=1;//开中断TH0=0xff;//定时器0初值FFH;TL0=0xff;ET0=1;TR0=1;TH1=0xff;//定时器1初值FFH;TL1=0xff;ET1=1;TR1=1;IT0=1;//脉冲方式EX0=1;//开外部中断0:加速IT1=1;//脉冲方式EX1=1;//开外部中断1:减速inti_lcd();DoSpeed();ShowState();while(1){clock(RunSpeed);P0_1=P0_1^0x01;}}//定时器0中断程序:正转voidt_0(void)interrupt1{RunState=RIGHT_RUN;P0_0=1;P1=0x01;cmd_wr();ShowState();}//定时器1中断:反转voidt_1(void)interrupt3{RunState=LEFT_RUN;P0_0=0;P1=0x01;cmd_wr();ShowState();}//中断0:加速程序voidSpeedUp()interrupt0{if(RunSpeed=12)RunSpeed=RunSpeed-2;DoSpeed();P1=0x01;cmd_wr();ShowState();}//中断1:减速程序voidSpeedDowm()interrupt2{if(RunSpeed=100)RunSpeed=RunSpeed+2;DoSpeed();P1=0x01;cmd_wr();ShowState();}intdelay()//判断LCD是否忙{inta;start:RS=0;RW=1;E=0;for(a=0;a2;a++);E=1;P1=0xff;if(P1_7==0)return0;elsegotostart;}voidinti_lcd()//设置LCD方式{P1=0x38;cmd_wr();delay();P1=0x01;//清除cmd_wr();delay();P1=0x0f;cmd_wr();delay();P1=0x06;cmd_wr();delay();P1=0x0c;cmd_wr();delay();}voidcmd_wr()//写控制字{RS=0;RW=0;E=0;E=1;}voidshow_lcd(inti)//LCD显示子程序{P1=i;RS=1;RW=0;E=0;E=1;}voidShowState()//显示状态与速度{inti=0;while(SpeedChar[i]!='\0'){delay();show_lcd(SpeedChar[i]);i++;}delay();P1=0x80|0x0d;cmd_wr();i=0;while(SPEED[i]!='\0'){delay();show_lcd(SPEED[i]);i++;}delay();P1=0xC0;cmd_wr();i=0;while(StateChar[i]!='\0'){delay();show_lcd(StateChar[i]);i++;}delay();P1=0xC0|0x0A;cmd_wr();i=0;if(RunState==RIGHT_RUN)while(STATE_CW[i]!='\0'){delay();show_lcd(STATE_CW[i]);i++;}elsewhile(STATE_CCW[i]!='\0'){delay();show_lcd(STATE_CCW[i]);i++;}}voidclock(unsignedintDelay)//1ms延时程序{unsignedinti;for(;Delay0;Delay--)for(i=0;i124;i++);}voidDoSpeed(){SPEED[0]=(1000*6/RunSpeed/100)+48;SPEED[1]=1000*6/RunSpeed%100/10+48;SPEED[2]=1000*6/RunSpeed%10+48;}

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功