⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 motor.c

📁 用C写的步进电机程序
💻 C
字号:
#include<reg52.h>

#define BYTE unsigned char   
BYTE	count;					// 1 cycle pace counter
BYTE	pace;					// motor control counter
unsigned char bdata Flag _at_ 0x25;
sbit	Fdir=Flag^0;			//Fdir=1  positive running  =0 negative running
sbit	Fdir1=Flag^1;			//Fdir=1  positive running  =0 negative running
sbit	Fstop=Flag^4;			//Fstop=1 stop:50us-100us-0(stop)  =0start:0-100us-50us(running)

unsigned char code TAB[]={06,04,05,01,03,02,06,02,03,01,05,04};

void timer1() interrupt 3 using 1
{
ACC=6-pace;						// get offset  address
if(Fdir1==0)ACC=ACC+6;				// judge it's negative running or not 
P1=ACC=TAB[ACC];					// get MOTOR ouput data   =0 then running 
if(--pace==0)pace=6;			// 3 phase 6 pace over then reload
if(--count!=0)return;			// control over or not then exit 
  else {
  		count=12;				// reload count
  		Fdir1=Fdir;				// get direction 
		if(Fstop==0)			// if want to stop then skip
		  {if(TH1>205)return;	// if self cycle = 50us then normal running and exit
		     else TH1=TH1+5;	// else cycle-5us
		  }
  		else 
			{if(TH1<157)		// if self cycle = 100us then stop and exit
				{Fstop=1;TR1=0;TH1=156;return;}
		 	else TH1=TH1-5;		// else cycle+5us
			}
	   }
}

void main(void )
{TMOD=0X20;
P1=0x00;
P2=0x00;
P0=0x00;
P3=0x00;
count=12;
pace=6;
TL1=156;TH1=156;	
Fdir=Fdir1=Fstop=1;
EA=ET1=TR1=1;
Fstop=0;
do{}while(1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -