#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define MotorData P2 //步進電機控制接口定義
uchar phasecw[8] ={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09};//正轉 電機導通相序 D-C-B-A
uchar phaseccw[8]={0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08};//反轉 電機導通相序 A-B-C-D
//ms延時函數(shù)
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<8;i++)
{
MotorData=phasecw[i];
Delay_xms(1);//轉速調節(jié)
}
}
//逆時針轉動
void MotorCCW(void)
{
uchar i;
for(i=0;i<8;i++)
{
MotorData=phaseccw[i];
Delay_xms(1);//轉速調節(jié)
}
}
//停止轉動
void MotorStop(void)
{
MotorData=0x00;
}
//主函數(shù)
void main(void)
{
uint i;
Delay_xms(50);//等待系統(tǒng)穩(wěn)定
while(1)
{
MotorCW(); //順時針轉動
}
}
|