标题: 单片机控制ULN2003步进电机C语言 [打印本页]

作者: ghg123456    时间: 2019-8-20 10:57
标题: 单片机控制ULN2003步进电机C语言
#include<reg52.h>
#define uchar unsigned char
#define uint  unsigned int
#define MotorData P0                    //步进电机控制接口定义
uchar phasecw[4] ={0x08,0x04,0x02,0x01};//正转 电机导通相序 D-C-B-A
uchar phaseccw[4]={0x01,0x02,0x04,0x08};//反转 电机导通相序 A-B-C-D
sbit key1 = P3^5;
sbit key2 = P3^4;
//ms延时函数
void Delay_xms(uint x)
{
uint i,j;
for(i=0;i<x;i++)
  for(j=0;j<112;j++);
}
//顺时针转动
void MotorCW(void)
{
uchar i;
for(i=0;i<4;i++)
  {
   MotorData=phasecw[i];
   Delay_xms(8);//转速调节
  }
}
//逆时针转动
void MotorCCW(void)
{
uchar i;
for(i=0;i<4;i++)
  {
   MotorData=phaseccw[i];
   Delay_xms(8);//转速调节
  }
}
//停止转动
void MotorStop(void)
{
MotorData=0x00;
}
//主函数
void main(void)
{
uint i;
Delay_xms(50);//等待系统稳定
while(1)
{
        if(key2 == 1)
        {
                for(i=0;i<20;i++)
                {
                        MotorCW();   //顺时针转动
                }
                            MotorStop();  //停止转动
            Delay_xms(10);


        }

  else if(key1 == 0)
  {
        for(i=0;i<20;i++)
        {
           MotorCCW();  //逆时针转动
        }
                    MotorStop();  //停止转动
            Delay_xms(100);

  }
  else
  {
           MotorStop();  //停止转动
            Delay_xms(10);
  }
  // MotorStop();  //停止转动
//  Delay_xms(500);  

}

}







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1