
控制步进马达双四拍正反转.doc
4页/************************************************************************************** 标题: 步进电机试验二(双四拍方式) ** ** 通过本例程了解步进马达使用及驱动程序编写 **; 双四拍工作方式: **; 正转:A/B AB AB/ A/B/ ** 反转:A/B/ AB/ AB A/B* * UDN2916电流控制真值表:* I0 I1 输出电流* L L 最大* H L 最大*2/3* L H 最大*1/3* H H 0 ** 请学员一定要消化掉本例程 * J14短路冒需断开 ***************************************************************************************/ #include "reg52.h"#define speed 30 //Motorsbit PH1 = P1^0; //定义管脚sbit PH2 = P1^1;sbit I01 = P1^2;sbit I11 = P1^3;sbit I02 = P1^4;sbit I12 = P1^5;sbit k1 = P0^0;int a;void delay(int time);/***************************************函数功能:产生脉冲控制步进机正转:A/B AB AB/ A/B/ I0 I1 为低电平时,均以最大电流输出**************************************/void Go0(){ //A PH1 = 0; //PH1为0 则A线圈为反向电流 I01 = 0; I11 = 0; PH2 = 0; //PH2为1 则B线圈为反向电流 A/B/ AB/ AB A/B I02 = 0; I12 = 0; delay(speed); //0 PH1 = 1; //PH1为1 则A线圈为正向电流 I01 = 0; I11 = 0; PH2 = 0; //PH2为1 则B线圈为反向电流 I02 = 0; I12 = 0; delay(speed); //B PH1 = 1; //PH1为1 则A线圈为正向电流 I01 = 0; I11 = 0; PH2 = 1; //PH2为0 则B线圈为正向电流 I02 = 0; I12 = 0; //// delay(speed); //0 PH1 = 0; //PH1为0 则A线圈为反向电流 I01 = 0; I11 = 0; PH2 = 1; I02 = 0; I12 = 0; delay(speed); } void Go1(){ //A PH1 = 0; //PH1为0 则A线圈为反向电流 I01 = 0; I11 = 0; PH2 = 1; //PH2为1 则B线圈为正向电流 I02 = 0; I12 = 0; delay(speed); //0 PH1 = 1; //PH1为1 则A线圈为正向电流 A/B AB AB/ A/B/ I01 = 0; I11 = 0; PH2 = 1; //PH2为0 则B线圈为反向电流 I02 = 0; I12 = 0; delay(speed); //B PH1 = 1; //PH1为1 则A线圈为正向电流 I01 = 0; I11 = 0; PH2 = 0; //PH2为0 则B线圈为正向电流 I02 = 0; I12 = 0; //// delay(speed); //0 PH1 = 0; //PH1为0 则A线圈为反向电流 I01 = 0; I11 = 0; PH2 = 0; //PH2为1 则A线圈为正向电流 I02 = 0; I12 = 0; delay(speed); }/*******************延时函数****************************/void delay(int time){ int i,j; for(j=0; j <= time; j++) for(i =0 ; i <= 120; i++);}void main(){ while(1) { if(k1==0) a=0; else a=1; switch(a) { case 0 :Go0();break; case 1 : Go1();break; //步进电机运行 } }}。












