找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2676|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

步進(jìn)電機(jī)控制代碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:272891 發(fā)表于 2018-1-7 20:24 | 只看該作者 回帖獎勵 |倒序?yàn)g覽 |閱讀模式
#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);
         }
         
}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表