標(biāo)題: 步進(jìn)電機(jī)控制代碼 [打印本頁]

作者: lizhengyi    時間: 2018-1-7 20:24
標(biāo)題: 步進(jìn)電機(jī)控制代碼
#include <STC89C52RC.H>
#include<reg52.h>
#include<intrins.h>       
#define uchar unsigned char
#define uint  unsigned int
#define MotorData P3                    //步進(jìn)電機(jī)控制接口定義
uchar phasecw[4] ={0x08,0x04,0x02,0x01};//正轉(zhuǎn) 電機(jī)導(dǎo)通相序 D-C-B-A
uchar phaseccw[4]={0x01,0x02,0x04,0x08};//反轉(zhuǎn) 電機(jī)導(dǎo)通相序 A-B-C-D
unsigned char code Disp_Tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
sbit K1=P1^4;                          //位操作
sbit K2=P1^5;
sbit K3=P1^6;
sbit K4=P1^7;
sbit K5=P3^5;
sfr ISP_DATA = 0xe2;   
sfr ISP_ADDRH = 0xe3;     
sfr ISP_ADDRL = 0xe4;   
sfr ISP_CMD = 0xe5;   
sfr ISP_TRIG = 0xe6;      
sfr ISP_CONTR = 0xe7;
void  cc(uint addr);
void  xcx(uint addr,uchar dat);
uchar dcx(uint addr);
void  Q0();

void Delay_xms(uint x)
{
uint i,j;
for(i=0;i<x;i++)
  for(j=0;j<112;j++);
}
void MotorCW(void)                 //順時針轉(zhuǎn)動
{
uchar i;
for(i=0;i<4;i++)
  {
   MotorData=phasecw[i];
   Delay_xms(4);                  //轉(zhuǎn)速調(diào)節(jié)
  }
}
void MotorCCW(void)                //逆時針轉(zhuǎn)動
{
uchar i;
for(i=0;i<4;i++)
  {

   MotorData=phaseccw[i];
   Delay_xms(4);                //轉(zhuǎn)速調(diào)節(jié)
  }
}
void MotorStop(void)        //停止轉(zhuǎn)動
{
MotorData=0x00;
}
//主函數(shù)
void main(void)
{
uint i,a;
Delay_xms(50);//等待系統(tǒng)穩(wěn)定
P0 = 0x3f;                //上電數(shù)碼管顯示0
P1 = 0xff;                //位選打開
a=0;
if(K1==0)
        {a=a+1;
     P0=Disp_Tab[a];
          for(i=0;i<520;i++)
        {
           MotorCW();   //順時針轉(zhuǎn)動
          }
         MotorStop();  //停止轉(zhuǎn)動
         Delay_xms(500);
        }
if(K2==0)
        {
        for(i=0;i<520;i++)
                 {
                  MotorCCW();  //逆時針轉(zhuǎn)動
                     }
         MotorStop();  //停止轉(zhuǎn)動
         Delay_xms(500);
    }
if(K3==0)
        {
        for(i=0;i<10;i++)
                 {
                  MotorCW();         //順時針轉(zhuǎn)動
                     }
         MotorStop();    //停止轉(zhuǎn)動
         Delay_xms(500);
    }
if(K4==0)
        {
        for(i=0;i<10;i++)
                 {
                   MotorCCW();   //逆時針轉(zhuǎn)動
                     }
         MotorStop();  //停止轉(zhuǎn)動
         Delay_xms(500);
         }
         
}






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1