您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 其它行业文档 > 51单片机控制(按键)2相步进电机-L298N驱动
/*本程序用于2相步进电机控制L298N驱动通过独立按键进行控制转速和转向*/#includereg52.h//52系列单片机头文件#defineucharunsignedchar//宏定义#defineuintunsignedint//宏定义sbitkey1=P3^4;//解除锁定sbitkey2=P3^5;//反转2圈sbitkey3=P3^6;//反转4圈sbitkey4=P3^7;//正转4圈//延时voiddelay(uintxms){uinti,j;for(i=xms;i0;i--)//i=xms即延时约xms毫秒for(j=110;j0;j--);}//主函数voidmain(){uinti=200;P3=0xff;while(1){if(key1==0){delay(10);if(key1==0){while(i--){P1=0X08;delay(2);P1=0X0a;delay(2);P1=0X02;delay(2);P1=0X06;delay(2);P1=0X04;delay(2);P1=0X05;delay(2);P1=0X01;delay(2);P1=0X09;delay(2);}while(!key1);}}i=200;if(key2==0){delay(10);if(key2==0){while(i--){P1=0X09;delay(2);P1=0X01;delay(2);P1=0X05;delay(2);P1=0X04;delay(2);P1=0X06;delay(2);P1=0X02;delay(2);P1=0X0a;delay(2);P1=0X08;delay(2);}while(!key2);}}i=50;if(key3==0){delay(10);if(key3==0){while(i--){P1=0X08;delay(5);P1=0X0a;delay(5);P1=0X02;delay(5);P1=0X06;delay(5);P1=0X04;delay(5);P1=0X05;delay(5);P1=0X01;delay(5);P1=0X09;delay(5);}while(!key3);}}if(key4==0){delay(10);if(key4==0){P1=0Xff;while(!key4);}}}}
本文标题:51单片机控制(按键)2相步进电机-L298N驱动
链接地址:https://www.777doc.com/doc-4633692 .html