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

📄 main.c

📁 步进电机控制的C程序
💻 C
字号:
#include <REGX52.H>
//#include <SST89X5XXRD2.H>
#include<math.h>
#define uchar unsigned char 
#define uint unsigned int

char code sxf[][4]={" ","1  ","2  ","4  ","8  ","16 ","32 ","64 ","128","256","5  ","10 ","20 ","40 ","80 ","160","   "};

char code workshow[][4]={"OFF","ON "};
char code runflash[][4] = {"ON-","ON_"};
char dirshow[][4]= {"R->","<-F"};
uchar idata recbuf[30];//串口接收缓冲区
uchar outbuf[10];//串口发送缓冲区;
uchar reclen;   
uchar sendlen;  // 发送计数器
uchar *sendbuf; // 发送指针
bit work;		// 运行控制	 0--停止,1--转动
uchar k=0;
bit dir;  // 运转方向
uint steplen;    // 运动步数
uchar uxifen;	// 细分值 

sbit  s0 = P2^0; //细分设置
sbit  s1 = P2^1; //细分设置
sbit  s2 = P2^2; //细分设置
sbit  s3 = P2^3;  //细分设置
#define  XIFEN  P2
#define XFMASK 0xf0
sbit KEYUP = P2^6;
sbit KEYDN = P2^7;

sbit  QA   = P1^0;
sbit  QB   = P2^5;

sbit  FREE = P2^4;  // 脱机控制 
uchar phase[]={0,1,3,2,0,1,3,2}; // 相位控制
uchar zx[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
bit ReadVer;

uint position =0;
uint posmax=60000;

sbit  KEY1 = P2^4;
sbit  KEY2 = P2^5;
sbit  KEY3 = P2^6;
sbit  KEY4 = P2^7;
					   
//函数声明
void sysinit(void);    // 系统初始化
void xifenset(uint i); //细分设置函数
void delay(uint t);   // 延时函数 //
void LcdReset(void);/*初始化*/
void lcd_cls();/*清除显示*/
void ePut_str(char x,char y,char *strpoint); /*在当前显示位置显示LCD 字符串*/
//void lcd_string(char *strpoint) ;
void showx(char x,char y,unsigned int n);
void Delay50ms(uchar n); // 延时n*50ms
void Delayms(uint n);//延时n(ms);
//void show(); //
void senddata(uchar m);//串口发送程序
void SendStr(unsigned char *buf);// 发送字符串

bit bwork;
//%*3011020

void bytewrite (unsigned  int um, unsigned char dat)	 ;
unsigned char byteread(unsigned int um);


void main(void)
{
    uchar i;uchar p;
	sysinit(); //初始化
	//senddata('O');
    LcdReset();
	lcd_cls();
	P0=0xFE;
    EA=1;
	SendStr("V数字共交显微控制 V1.0");
	Delay50ms(4); // 延时2s
	ePut_str(0,0,"Run:OFF XF:     ");
	ePut_str(0,1,"Dir:    ST:     ");
	position = (byteread(10)<<8) | byteread(11);
	delay(1000);	 
	while(1)
   { 
	 if(work) ET0 = 1;
	 else ET0 =0;
	 KEYUP=1;KEYDN=1;
	 if(KEYUP==0) {
	    work=1;
	    dir=1;steplen=10;
		ET0=1;
		}
	 if(KEYDN==0) {
	    work=1;
		dir=0;steplen=10;
		ET0=1;}

	 ePut_str(4,0,workshow[work]);	
	 ePut_str(11,0,sxf[uxifen]);
	 showx (15,0,work);
   	 ePut_str(4,1,dirshow[dir]);
	 showx (11,1,steplen);
	 if(ReadVer){ReadVer=0;SendStr("V数字共交显微控制 V1.0"); }
	 //P0 = zx[uxifen];
   } 
}

void timer0(void) interrupt 1
{
   uchar p=0;
   static int t=0;   

   if((work ==1) && steplen)
   { 
		 p = k&0x03;
		 QA =  phase[p]&0x01;
		 QB =  (phase[p]>>1)&0x01;
 
		 if(work==1 && steplen) steplen--;	

		 if(dir) { k--;if(position) position--;}    	// 正向控制	  
		 else    { k++;if((position<posmax)) position++;}	   // 反向控制	 

   } 
	   /*
	   else if( work==3 )	
	   {
		 p = k&0x03;
		 QA =  phase[p]&0x01;
		 QB =  (phase[p]>>1)&0x01;
	     if(dir) { k--;position--;}    	// 正向控制	  
		 else    { k++;position++;}	   // 反向控制	 
	   }
	   */
	   else 
	   { 
	     QA=0;QB=0; work=0;ET0=0;
		 TI =0;SBUF = '$';while(!TI) ;
		 TI =0;SBUF = position>>8;
	   	 bytewrite(10,(uchar)(position>>8));
	   	 bytewrite(11,(uchar)position);
	   }	           
}

//**************************************************************//
// 细分设置函数 //
//**************************************************************//
void xifenset(uchar i)
{  uchar res;
   uchar k=0;
   i = i&0x0f;
   k |=(i & 0x08)>>3;
   k |=(i & 0x04)>>1;    
   k |=(i & 0x02)<<1;
   k |=(i & 0x01)<<3;

   res = XIFEN;
   res = (res & 0xF0) | ((~k) & 0x0F);
   XIFEN = res;
   uxifen = i+1;      
}


void senddata(uchar m)
{
   TI=0;SBUF=m;
} 				  
//==========串口中断处理函数===========================
// 格式1 "%*xdttttr\r" 其中: x为细分值;d=0正转,d=1反转;tttt为步数;r=0不控制,r=1执行;
// 格式2 "#*s\r"  其中:s=0读状态及参数;s=1正转;s=2反转;s=3停止;
#define DDIV  2
#define DDIR  3
#define DRUN  4
#define DTIME 5

void uart0(void) interrupt 4
{
	uchar res;
	static uchar index=0;
	if(RI)
	{//接收中断处理
	 	RI = 0;
	 	res = SBUF;
		recbuf[index]=res;		 
		if(res!='*' && index==1) {index=0;recbuf[index]=res;}		 
		if(index) index++;
		if(res=='%'||res=='#')       {index=1;recbuf[0]=res; }
		
		if(res==0x0a && index >3)// 收到有效的控制码
		{
		recbuf[index]=0;// 添加字符串结束符
		reclen = index;		
		index =0; 		
		if(recbuf[0]=='%' && reclen==11)		
		{		   
		   xifenset(recbuf[DDIV]&0x0f);	// 设置细分数
		   dir = recbuf[DDIR]&0x01;  // 正反转
		   work = recbuf[DRUN] & 0x03;
		   steplen =(recbuf[DTIME]&0x0f);  steplen<<=4;
		   steplen |=(recbuf[DTIME+1]&0x0f);steplen<<=4;
		   steplen |=(recbuf[DTIME+2]&0x0f);steplen<<=4;
		   steplen |=(recbuf[DTIME+3]&0x0f);
		}
		else if(recbuf[0]=='#' && reclen==5)
		{  //index =0; 
		   switch(recbuf[2]&0x0f)
		   {
		      case 0: work =0;break;
			  case 1: work =1;dir = 0; break;
			  case 2: work =1;dir = 1; break;
			  case 3: work =0;break;
			  case 8: ReadVer=1; break;
			  default: work =0; break;		   
		   }
		}
		}		
		if(index>=20) index =0;	  // 如果收到乱码重置  
	}		 
	else 
	{ // 发送中断处理
	  	TI=0;
		if(sendlen) 
		{
			sendlen--;
			if(sendlen) SBUF = *sendbuf++; 	   	 
		}	  	
	}

}
void SendStr(unsigned char *buf)
{ 
  unsigned char len=0;
  while(sendlen);
  while(buf[len]) len++;
  sendlen = len;
  TI=0;
  sendbuf = buf;
  SBUF = *sendbuf++;  
 
}

//**************************************************************//
//系统初始化//
//**************************************************************//
void sysinit(void)
{
	// == 定时器0 工作在 1ms, Fosc =11.052MHz
    TMOD=0x02;
	TH0=0x00;
	TL0=0x00;

    TMOD &=0x0f;
    TMOD|=0x20;
    SCON=0x50;
    PCON|=0x80;
    TH1=0xfd;
    TL1=0xfd;
    TR1=1;

    P3 = 0xff;

	TR0=1;
	ET0=0;

	ES=1; //允许串口中断
    PS=0; //串口中断优先级别最高
    //EA=1; //开所有中断 
}

//**************************************************************//
// 延时函数 //
//**************************************************************//
 void delay(uint N)
 {
   uint i;
   for(i=0;i<N;i++);
 }

 void Delayms(unsigned int ms)
{
    unsigned int i,j;
    for(i=0;i<ms;i++)
    for(j=0;j<113;j++);
}

void Delay50ms(unsigned char ms)
{
    unsigned int i;
    unsigned char j;
    for(j=0;j<ms;j++)
    for(i=0;i<9135;i++);
}


⌨️ 快捷键说明

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