步进电机驱动程序,5线

1 设置,
8051 & ULN2003
步进电机驱动程序,5线_第1张图片
2. 正反转控制
uchar phasecw[4] ={0x08,0x04,0x02,0x01};正转,电机导通顺序 D-C-B-A
uchar phaseccw[4]={0x01,0x02,0x04,0x08};//反转,电机导通顺序 A-B-C-D

3. 基本程序如下
ULN2003Çý¶¯5V¼õËÙ²½½øµç»ú³ÌÐò
Target:STC89C52RC-40C
Crystal:12MHz
Author:Õ½Éñµ¥Æ¬»ú¹¤×÷ÊÒ
Platform:51&avrµ¥Æ¬»ú×îСϵͳ°å+ULN2003²½½øµç»úÇý¶¯Ì×¼þ
http://zsmcu.taobao.com QQ:284083167


½ÓÏß·½Ê½:
IN1 ---- P00
IN2 ---- P01
IN3 ---- P02
IN4 ---- P03

  • ---- +5V
  • ---- GND
    *********************/
    #include
    #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
    //msÑÓʱº¯Êý
    void Delay_xms(uint x)
    {
    uint i,j;
    for(i=0;i for(j=0;j<112;j++);
    }
    //˳ʱÕëת¶¯
    void MotorCW(void)
    {
    uchar i;
    for(i=0;i<4;i++)
    {
    MotorData=phasecw[i];
    Delay_xms(4);//תËÙµ÷½Ú
    }
    }
    //ÄæʱÕëת¶¯
    void MotorCCW(void)
    {
    uchar i;
    for(i=0;i<4;i++)
    {
    MotorData=phaseccw[i];
    Delay_xms(4);//תËÙµ÷½Ú
    }
    }
    //ֹͣת¶¯
    void MotorStop(void)
    {
    MotorData=0x00;
    }
    //Ö÷º¯Êý
    void main(void)
    {
    uint i;
    Delay_xms(50);//µÈ´ýϵͳÎȶ¨
    while(1)
    {
    for(i=0;i<500;i++)
    {
    MotorCW(); //˳ʱÕëת¶¯
    }
    MotorStop(); //ֹͣת¶¯
    Delay_xms(500);
    for(i=0;i<500;i++)
    {
    MotorCCW(); //ÄæʱÕëת¶¯
    }
    MotorStop(); //ֹͣת¶¯
    Delay_xms(500);
    }
    }
    4. 加,减速度,改变延时时间
    void MotorCW(void)
    {
    uchar i;
    for(i=0;i<4;i++)
    {
    MotorData=phasecw[i];
    Delay_xms(4);//תËÙµ÷½Ú
    }
    }

你可能感兴趣的:(FW)