專注電子技術學習與研究
當前位置:單片機教程網 >> MCU設計實例 >> 瀏覽文章

步進電機驅動程序

作者:佚名   來源:本站原創(chuàng)   點擊數:  更新時間:2013年11月04日   【字體:
 #include <REG52.H>

#define uchar unsigned char

#define uint unsigned int  

#define ulong unsigned long

#define CLK_WISE 0//順時針方向轉動

#define INVERSE 1//逆時針方向轉動

bit direction=CLK_WISE;/***方向標志,取值為CLK_WISE INVERSE*/

static uchar speedcount=0;//加速標志,越大轉速越快,最大到7,然后回歸到0,循環(huán)。

//程序中可以依據它來改變占空比

uchar step[8]={0x01,0x09,0x08,0x0c,0x04,0x06,0x02,0x03};//8個步

uchar th_0[8]={0x5D,0x85,0x9E,0xAE,0xBA,0xC2,0xC9,0xCF};//8個定時器值,高8

uchar tl_0[8]={0x3D,0xEE,0x58,0x9E,0x3E,0xF7,0xBF,0x2C};//8個定時器值,低8

int step_i=0;//當前處于哪一步

/***定時器t0***/

void time0(void) interrupt 1 using 1

{

P2=step[step_i];//輸出電機控制信號

if(direction==CLK_WISE) //順時針轉

step_i++;

else

step_i--;//逆時針轉

if (step_i>7)//順時針到最后一步,需要調整到第一步

step_i=0;

if (step_i<0)//順時針到第一步,需要調整到最后一步

step_i=7;

TH0=th_0[speedcount];//根據當前速度設定定時器初值

TL0=tl_0[speedcount];

}

 

/****改變轉向標志*****/

void int1_srv (void) interrupt 2 using 2

{

if(INT1==0)

{

while(!INT1);

 direction=!direction;

}

}

 

/*******加速********/

void change(void) interrupt 0 using 0

{

if(INT0==0)

{

while(!INT0);

  speedcount++;//記錄加速次數

if(speedcount>7)

speedcount=0;//最大為7,然后從0開始循環(huán)。

}

}

void main()

{

EA=1;

TMOD=0x01;

ET0=1;//定時器0初始化

TR0=1;

 

EX0=1;

IT0=1;//外部中斷0

 

EX1=1;

IT1=1;//外部中斷1

 

TH0=th_0[0];

TL0=th_0[0];//定時器0初始值

 

while(1)

{

;

}

}

 

關閉窗口

相關文章