51单片机控制步进电机的转动,加减速,停止,反转

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

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

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

资源描述

#includereg52.hsbitinc=P3^2;sbitdec=P3^3;sbitzhzhd=P3^6;sbitfazhd=P3^7;bitflag=1;unsignedchart=0x00;//表正反速度voiddelay(unsignedintt);voidmotor_ffw();unsignedcharcodeled7code[]={0x81,0xe7,0x92,0xc2,0xe4,0xc8,0x88,0xe3,0x00,0xc0};unsignedintnum=0;unsignedcharcodeFFW[8]={0x40,0x60,0x20,0x30,0x10,0x18,0x08,0x48};unsignedcharcodeFFZ[8]={0x48,0x08,0x18,0x10,0x30,0x20,0x60,0x40};//反转voidmain(){EA=1;IT0=1;EX0=1;IT1=1;EX1=1;TMOD=0x06;TL0=0xff;TH0=0xff;TR0=1;ET0=1;P3=0x3f;P0=led7code[num%10];while(1){motor_ffw();}}voidmotor_ffw()/*步进电机驱动*///{unsignedchari;intj;while(1){for(j=0;j12;j++)//12个周期转一圈{for(i=0;i8;i++)//一个周期转30度{if(flag==1)P2=FFW[i];//取数据elseP2=FFZ[i];delay(t);//t调节转速}}}}voidint0(void)interrupt0{EX0=0;delay(10);if(inc==0){num++;P0=led7code[num%10];if(num%10!=0&&flag){zhzhd=0;fazhd=1;}elseif(num%10==0){zhzhd=0;fazhd=0;}else{zhzhd=1;fazhd=0;}switch(num%10){case0:t=0x00;break;case1:t=0x12;break;case2:t=0x11;break;case3:t=0x10;break;case4:t=0x09;break;case5:t=0x08;break;case6:t=0x07;break;case7:t=0x06;break;case8:t=0x05;break;case9:t=0x04;break;}}while(!inc);EX0=1;}voidint1(void)interrupt2{EX1=0;delay(10);if(dec==0){num--;if(num==65535)num=65529;P0=led7code[num%10];if(num%10!=0&&flag){zhzhd=0;fazhd=1;}elseif(num%10==0){zhzhd=0;fazhd=0;}else{zhzhd=1;fazhd=0;}if(num==65535)num=65529;switch(num%10){case0:t=0x00;break;case1:t=0x12;break;case2:t=0x11;break;case3:t=0x10;break;case4:t=0x09;break;case5:t=0x08;break;case6:t=0x07;break;case7:t=0x06;break;case8:t=0x05;break;case9:t=0x04;break;}}while(!dec);EX1=1;}voidhuanx(void)interrupt1{ET0=0;TR0=0;delay(10);if(P3^4==0){if(flag==1){flag=0;zhzhd=1;delay(500);fazhd=0;}else{flag=1;fazhd=1;delay(500);zhzhd=0;}}while(!(P3^4));ET0=1;TR0=1;}//延时程序voiddelay(unsignedintt){unsignedintk;while(t--){for(k=0;k80;k++);}}

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

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

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

×
保存成功