/********************************************************************************                        
*                                                 *;   单双八拍工作方式:                                                        
*;   A-AB-B-BC-C-CD-D-DA (即一个脉冲,转 3.75 度)    *******************************************************************************/
  
#include "reg52.h"

void delay();

 //Motor
sbit F1 = P0^0;
sbit F2 = P0^1;
sbit F3 = P0^2;
sbit F4 = P0^3;

unsigned char code FFW[8]={0xfe,0xfc,0xfd,0xf9,0xfb,0xf3,0xf7,0xf6}; //反转
unsigned char code FFZ[8]={0xf6,0xf7,0xf3,0xfb,0xf9,0xfd,0xfc,0xfe}; //正转
unsigned int  K, rate;


/**********************************************************
*                                                          *
*                 步进电机驱动                              *
***********************************************************/
void  motor_ffw()
 { 
   unsigned char i;

      for (i=0; i<8; i++)       //一个周期转30度
        {
          P0 = FFW[i]&0x1f;  //取数据
        
          delay();           //调节转速
        }
     } 

/********************************************
     延时程序
*********************************************/

 void delay()   
{                           
   unsigned int k,t;
    t=rate;
   while(t--)
   {
     for(k=0; k<150; k++)
     { }
   }
}


/********************************************************
*                                                       
*步进电机运行                                               
*                                                      
*********************************************************/
void  motor_turn()
{ 
   unsigned char x;
   rate=0x0a;
   x=0x40;
   do
     {
      motor_ffw();          //加速
      rate--;
     }while(rate!=0x01);
   do
     {        
       motor_ffw();        //匀速
       x--;
     }while(x!=0x01);
     
   do
     {
      motor_ffw();         //减速
      rate++;
     }while(rate!=0x0a);    
}

main() 
 { 
    while(1)
    {
    
     motor_turn();
    

     }
}

c语言

Last modification:February 14th, 2021 at 09:49 pm
如果觉得我的文章对你有用,请随意赞赏