stc8a_步进电机控制,加减速

#include 
#include 

//晶振选择22.1184Mhz;
//初始化定时器0,定时2ms进入中断;作为步进电机的驱动脉冲来源
//初始化定时器2,作为波特率发生器;

sbit XPUL=P1^0;
sbit XDIR=P1^1;
sbit XENA=P1^2;
sbit XlimitPos=P3^2;//靠近电机,正方向的限位开关
sbit XlimitNeg=P3^3;//远离电机,反方向的限位开关
sbit KEY=P1^7;

//0.5us*2499=1.249ms
//
code unsigned int ACB_C[]={2499,2421,2350,2284,2224,2168,2117,2068,2023,1981,1942,1904,1869,//13
1835,1804,1774,1745,1718,1692,1667,1643,1620,1598,1577,1557,1537,1519,1501,1483,//16
1466,1450,1434,1419,1404,1390,1376,1362,1349,1337,1324,1312,1300,1289,1278,1267,//16
1256,1246,1236,1226,1217,1207,1198,1189,1180,1172,1163,1155,1147,1139,1132,1124,
1117,1109,1102,1095,1088,1082,1075,1069,1062,1056,1050,1044,1038,1032,1026,1021,
1015,1010,1004,999,994,989,984,979,974,969,964,959,955,950,946,941,937,933,928,
924,920,916,912,908,904,900,896,893,889,885,882,878,875,871,868,864,861,857,854,
851,848,844,841,838,835,832,829,826,823,820,817,814,812,809,806,803,801,798,795,
793,790,787,785,782,780,777,775,772,770,768,765,763,760,758,756,754,751,749,747,
745,743,740,738,736,734,732,730,728,726,724,722,720,718,716,714,712,710,708,707,
705,703,701,699,697,696,694,692,690,689,687,685,684,682,680,679,677,675,674,672,
670,669,667,666,664,663,661,660,658,657,655,654,652,651,649,648,646,645,644,642,
641,639,638,637,635,634,633,631,630,629,627,626,625,623,622,621,620,618,617,616,
615,613,612,611,610,609,607,606,605,604,603,602,600,599,598,597,596,595,594,593,
592,590,589,588,587,586,585,584,583,582,581,580,579,578,577,576,575,574,573,572,
571,570,569,568,567,566,565,564,563,562,561,560,559,558,558,557,556,555,554,553,
552,551,550,550,549,548,547,546,545,544,543,543,542,541,540,539,538,538,537,536,
535,534,534,533,532,531,530,530,529,528,527,527,526,525,524,523,523,522,521,520,
520,519,518,518,517,516,515,515,514,513,512,512,511,510,510,509,508,508,507,506,
505,505,504,503,503,502,501,501,500,499,499,498,498,497,496,496,495,494,494,493,
492,492,491,491,490,489,489,488,487,487,486,486,485,484,484,483,483,482,481,481,
480,480,479,478,478,477,477,476,476,475,474,474,473,473,472,472,471,471,470,469,
469,468,468,467,467,466,466,465,465,464,464,463,463,462,461,461,460,460,459,459,
458,458,457,457,456,456,455,455,454,454,453,453,452,452,451,451,450,450,450,449,
449,448,448,447,447,446,446,445,445,444,444,443,443,442,442,442,441,441,440,440,
439,439,438,438,438,437,437,436,436,435,435,434,434,434,433,433,432,432,431,431,
431,430,430,429,429,429,428,428,427,427,426,426,426,425,425,424,424,424,423,423,
422,422,422,421,421,420,420,420,419,419,418,418,418,417,417,417,416,416,415,415,
415,414,414,414,413,413,412,412,412,411,411,411,410,410,409,409,409,408,408,408,
407,407,407,406,406,406,405,405,404,404,404,403,403,403,402,402,402,401,401,401,
400,400,400,399,399,399,398,398,398,397,397,397,396,396,396,395,395,395,394,394,
394,393,393,393,392,392,392,391,391,391,391,390,390,390,389,389,389,388,388,388,
387,387,387,387,386,386,386,385,385,385,384,384,384,383,383,383,383,382,382,382,
381,381,381,381,380,380,380,379,379,379,379,378,378,378,377,377,377,377,376,376,
376,375,375,375,375,374,374,374,373,373,373,373,372,372,372,372,371,371,371,370,
370,370,370,369,369,369,369,368,368,368,368,367,367,367,366,366,366,366,365,365,
365,365,364,364,364,364,363,363,363,363,362,362,362,362,361,361,361,361,360,360,
360,360,359,359,359,359,358,358,358,358,357,357,357,357,356,356,356,356,356,355,
355,355,355,354,354,354,354,353,353,353,353,352,352,352,352,352,351,351,351,351,
350,350,350,350,349,349,349,349,349,348,348,348,348,347,347,347,347,347,346,346,
346,346,346,345,345,345,345,344,344,344,344,344,343,343,343,343,342,342,342,342,
342,341,341,341,341,341,340,340,340,340,340,339,339,339,339,339,338,338,338,338,
337,337,337,337,337,336,336,336,336,336,335,335,335,335,335,334,334,334,334,334,
333,333,333,333,333,333,332,332,332,332,332,331,331,331,331,331,330,330,330,330,
330,329,329,329,329,329,328,328,328,328,328,328,327,327,327,327,327,326,326,326,
326,326,326,325,325,325,325,325,324,324,324,324,324,324,323,323,323,323,323,322,
322,322,322,322,322,321,321,321,321,321,321,320,320,320,320,320,319,319,319,319,
319,319,318,318,318,318,318,318,317,317,317,317,317,317,316,316,316,316,316,316,
315,315,315,315,315,315,314,314,314,314,314,314,313,313,313,313,313,313,312,312,
312,312,312,312,312,311,311,311,311,311,311,310,310,310,310,310,310,309,309,309,
309,309,309,308,308,308,308,308,308,308,307,307,307,307,307,307,306,306,306,306,
306,306,306,305,305,305,305,305,305,305,304,304,304,304,304,304,303,303,303,303,
303,303,303,302,302,302,302,302,302,302,301,301,301,301,301,301,301,300,300,300,
300,300,300,300,299,299,299,299,299,299,299,298,298,298,298,298,298,298,297,297,
297,297,297,297,297,296,296,296,296,296,296,296,296,295,295,295,295,295,295,295,
294,294,294,294,294,294,294,293,293,293,293,293,293,293,293,292,292,292,292,292,
292,292,291,291,291,291,291,291,291,291,290,290,290,290,290,290,290,290,289,289,
289,289,289,289,289,289,288,288,288,288,288,288,288,288,287,287,287,287,287,287,
287,286,286,286,286,286,286,286,286,286,285,285,285,285,285,285,285,285,284,284,
284,284,284,284,284,284,283,283,283,283,283,283,283,283,282,282,282,282,282,282,
282,282,282,281,281,281,281,281,281,281,281,280,280,280,280,280,280,280,280,280,
279,279,279,279,279,279,279,279,278,278,278,278,278,278,278,278,278,277,277,277,
277,277,277,277,277,277,276,276,276,276,276,276,276,276,276,275,275,275,275,275,
275,275,275,275,274,274,274,274,274,274,274,274,274,273,273,273,273,273,273,273,
273,273,272,272,272,272,272,272,272,272,272,272,271,271,271,271,271,271,271,271,
271,270,270,270,270,270,270,270,270,270,270,269,269,269,269,269,269,269,269,269,
269,268,268,268,268,268,268,268,268,268,267,267,267,267,267,267,267,267,267,267,
266,266,266,266,266,266,266,266,266,266,265,265,265,265,265,265,265,265,265,265,
264,264,264,264,264,264,264,264,264,264,264,263,263,263,263,263,263,263,263,263,
263,262,262,262,262,262,262,262,262,262,262,261,261,261,261,261,261,261,261,261,
261,261,260,260,260,260,260,260,260,260,260,260,260,259,259,259,259,259,259,259,
259,259,259,259,258,258,258,258,258,258,258,258,258,258,258,257,257,257,257,257,
257,257,257,257,257,257,256,256,256,256,256,256,256,256,256,256,256,255,255,255,
255,255,255,255,255,255,255,255,254,254,254,254,254,254,254,254,254,254,254,254,
253,253,253,253,253,253,253,253,253,253,253,252,252,252,252,252,252,252,252,252,
252,252,252,251,251,251,251,251,251,251,251,251,251,251,251,250,250,250,250,250,
250,250,250,250,250};
unsigned int  xpul_Global=0;
unsigned int  xpulhalf_Global=0;
unsigned int StepperLineVelMax=10500;//最大700,线段长700个脉冲,速度是上面数组的第700个元素,作为最大值
unsigned char StepperTimNum=0;
//360/1.8=200,8细分,则1600个脉冲走360度,等价于1600个脉冲一个丝杠的导程10mm(丝杠不一样,导程不一样)
//那么0.05mm,多少个脉冲呢?1600/10=x/0.05,x=1600*0.05/10=8个脉冲
unsigned int  StepperKx=160;//8个脉冲,0.05mm   
unsigned char XmoveDirFlag=0;//=0,正方向,=1,反方向;
unsigned char XlimitFlag=0;//限位开关标记
unsigned char StepTimFlag=0;//定时器是否计数的标记
#define XCCWPOS 0  //CCW靠近电机方向
#define XCWNEG  1  //CW离开电机方向
#define XENATrue 1   //电机使能
#define XENAFalse 0  //电机不使能

unsigned int char_ct=0;
//8--0.05
//x--100      x=100*8/0.05=100*8*20
//65535/160=409mm max
 unsigned int Tnum=0;
void Delay2us()		//@22.1184MHz
{
	unsigned char i;

	_nop_();
	_nop_();
	i = 12;
	while (--i);
}

void Delay500ms()		//@22.1184MHz
{
	unsigned char i, j, k;

	i = 57;
	j = 27;
	k = 112;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}



void Timer0Init(void)		//1微秒@22.1184MHz
{
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TH0=(65535-2500)/256;//22.1184Mhz,1T模式的话,=2就是3.208us  500--22.625us
	TL0=(65535-2500)%256;//100--4.2us
	//2500 113us
	//2-0.625us  4-
	TF0 = 0;		//清除TF0标志
	TR0 = 0;		//定时器0开始计时
	ET0 = 1;                                    //使能定时器中断
    

}


void Xposmove(float xmmtemp);
void Xnegmove(float xmmtemp);

void main()
{
	float xmm=1.0f;
	Timer0Init();
	
	 xmm=50;
	 EA = 1;
	//TR0 = 1;		//定时器0开始计时
	Delay500ms();
	//Tnum=ACB_C[0]*10;
//	Delay500ms()	;
//	Delay500ms()	;
	while(1)
	{
		//100--12k?
		Xnegmove(100);//走Xposmove(2),2*8=16,准确,如果	Xposmove(200)  500*8=4000
	  Delay500ms();	  Delay500ms();

//100--12k?
		Xposmove(100);//走Xposmove(2),2*8=16,准确,如果	Xposmove(200)  500*8=4000
	  Delay500ms();	  Delay500ms();

	}

}

void Xposmove(float xmmtemp)
{
	unsigned int i=0;
		unsigned int xpul=0;

//	XmoveDirFlag=XCCWPOS;  100*160=16000
	xpul_Global = (unsigned int)(xmmtemp * StepperKx);//计算需要多少个脉冲
	xpulhalf_Global= (unsigned int)(xpul_Global*0.5);//除以2
	XDIR=XCCWPOS;//方向打开
	XENA=XENATrue;//使能打开
	//xpul_Global=xpul_Global*15;
	//xpulhalf_Global=xpulhalf_Global*15;
	
	xpul=xpul_Global;
	for(i=0;i<xpul;i++)
	{
	//	if(XlimitFlag==1)break;//如果中途碰到限位开关退出循环,在限位开关的中断中XlimitFlag=1
		StepTimFlag=1;
	 //打开定时器0
		TR0=1;
		while(StepTimFlag==1);//走完一步,在定时器中断里面让那个StepTimFlag=0.退出死循环
		//关闭定时器
		TR0=0;
		xpul_Global=xpul_Global-1;
	}
	char_ct=0;
}

void Xnegmove(float xmmtemp)
{
	unsigned int i=0;
		unsigned int xpul=0;

//	XmoveDirFlag=XCCWPOS;  100*160=16000
	xpul_Global = (unsigned int)(xmmtemp * StepperKx);//计算需要多少个脉冲
	xpulhalf_Global= (unsigned int)(xpul_Global*0.5);//除以2
	XDIR=XCWNEG;//方向打开
	XENA=XENATrue;//使能打开
	//xpul_Global=xpul_Global*15;
	//xpulhalf_Global=xpulhalf_Global*15;
	
	xpul=xpul_Global;
	for(i=0;i<xpul;i++)
	{
	//	if(XlimitFlag==1)break;//如果中途碰到限位开关退出循环,在限位开关的中断中XlimitFlag=1
		StepTimFlag=1;
	 //打开定时器0
		TR0=1;
		while(StepTimFlag==1);//走完一步,在定时器中断里面让那个StepTimFlag=0.退出死循环
		//关闭定时器
		TR0=0;
		xpul_Global=xpul_Global-1;
	}
		XENA=XENAFalse;//使能打开

	char_ct=0;
	
}
//100----4us
//2500---4*25==100us
//       1500/100=15
//1.5ms  1500*100=15  


void TM0_Isr() interrupt 1 using 1
{
			 
				 
				P00=!P00;
	     TR0=0;
	     ET0=0;
	
			XPUL = 1;//高电平,产生脉冲
				Delay2us();
				XPUL = 0;	
					
        //判断线段长度是否符合速度条件800
        if  (xpulhalf_Global>2000)//L2=L的一半,如果L2大于v(700)
        {
            if(xpul_Global>2000)//如果L大于700,加速
            {
                char_ct++;//char_ct加1,每次进来中断,加1
                Tnum=ACB_C[char_ct];// Tnum=ACB_C[1];数据大,
                if(char_ct>=600)//若cnt加的数据大于700,那么cart=700,保持700===StepperLineVelMax
                {
                    char_ct=600;
                    Tnum=ACB_C[char_ct];
                }
            }
            else //如果L小于700,定时器开始减速
            {
                char_ct--;
                Tnum=ACB_C[char_ct];
            }
        }
				
				
        else  //如果线段长度小于700,固定的速度运行
        {
            Tnum=ACB_C[0];
        }

				
		
				
			//更新定时器0值,保持低电平这么多时间,下次再进来,重变为高电平。形成脉冲
				TH0=(65535-Tnum*15)/256;//22.1184Mhz,1T模式的话,=2就是3.208us  500--22.625us
				TL0=(65535-Tnum*15)%256;
				ET0=1;
				TR0=1;	
				StepTimFlag = 0;  
}

你可能感兴趣的:(单片机,单片机)