原创 单片机驱动2相双极性步进电机

2009-4-25 15:33 4365 4 4 分类: MCU/ 嵌入式

6ee01481-d27e-4d48-a5b6-50b6d21951f4.JPG


这是电路图。


在定时器T0的中断服务程序里将驱动信号送P1口,通过设置定时器T0的溢出率来实现调速,短小精悍。


以下原程序:


#include <stc.h>


typedef unsigned char uchar;
typedef unsigned int uint;
static uchar direct,step;
static uchar speed;
unsigned  char code pusle[]={0x69,0x99,0x96,0x66};
extern void PutString(unsigned char *);
extern void ComSend(unsigned char);



void DelayMS(unsigned int MS)   //延时MS毫秒函数
{
  unsigned char us,usn;
  while(MS!=0)           //for 12M
      { usn = 2;
          while(usn!=0)
              {
                  us="0xf5";
                  while (us!=0){us--;};
                  usn--;
              }
          MS--;
      }
}


void TimerInit()
{
 SCON=0x50; 


 TMOD=0x21;
 PCON=0x80;
 TH1=0xf3;      //串口配置:4800@12M
 TR1=1;
 SM2=1;
 EA=1;
 ET0=1;
 TR0=1;
 }
void main()
{
TimerInit();
direct=0;
speed=250;
PutString("Motor Running...");
while(1)
{direct=~direct;
 DelayMS(400);


}
}


 


void timer0() interrupt 1 using 1
{TH0=speed;
 P1=pusle[step];
 if(direct)
 {step++ ;
  if(step>3)step=0;
 }else
 {
 step--;
 if(step>3)
 step=3;
 }


}


 

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
4
关闭 站长推荐上一条 /1 下一条