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

📄 multistep.c.bak

📁 在ATMEGA128单片机上开发的一个机器人控制程序
💻 BAK
字号:
//************************************************************
//12路PWM波程序
//王超炎2005-05-05
//********************************************************
#include <iom128v.h>
#include <macros.h>
#include "data.h"
unsigned int N1=4;
unsigned int N2=20;
unsigned int N3=20;
unsigned int N4=10;
unsigned int cycle_start1=192;
unsigned int cycle_start2=3;
unsigned int cycle_start3=2;
unsigned int cycle_start4=2;
unsigned int totalstep1=392;
unsigned int totalstep2=11;
unsigned int totalstep3=6;
unsigned int totalstep4=7;

void port_init(void)
{
	DDRA  = 0xFF;//A口设置为输出
	PORTA = 0x00;//A口输出全0
	DDRC  = 0xFF;//...
	PORTC = 0x00;//...
}

void timer1_init(void)
{
	TCCR1A =0x00;
	TCNT1=0x00; 
}


void ClrAll(void)
{
	PORTA = 0x00;
	PORTC = 0x00;//端口清零
}




//*******************************************************************************
//函数名: Motion_init(void) 
//参  数:无
//功  能:初始化动作
//******************************************************************************* 
void Motion_init(void)
{
	int i=0, j=0, t=0, temp=0, temp1, temp2;
    
	for(t=0;t<50;t++) //?50步循环
	{
		for(j=1;j<9;j++) //?内嵌9步循环
		{
    		if(j<5) //当j小于5时
    			{
    				PORTA|=(1<<(j-1)); //把PA口的前4个引脚置1
    				temp2=2250+(j-1)*3750;
    			}
			PORTC|=(1<<(j-1)); //把PC口的8个引脚置1
			temp1=2250+(j-1)*3750;
			do
			{
				temp=TCNT1;	
				if(temp>temp1)PORTC&=~(1<<(j-1));	//把PC口的8个引脚置0
				if(temp>temp2)PORTA&=~(1<<(j-1));   //把PA口的8个引脚置0
			}
			while(temp<j*3750);
		}
		TCNT1=0; 
	}
}  



//*******************************************************************************
//函数名: Motion_excute() 
//参  数:
//功  能:
//******************************************************************************* 
void Motion_excute(unsigned int p)
{
	unsigned int i=0,t=0,j=0;
	unsigned int temp1=0,temp2=0,temp=0;
	if(p==1)
	{
		for(i=0;i<cycle_start1;i++)
			for(t=angle1[i][0];t>0;t--)
			{
				TCNT1=0x00;
				for(j=1;j<9;j++)	
				{
					if(j<5)
					{
						PORTA|=(1<<(j-1));
						temp2=(50*angle1[i][j+8]/3+750)+(j-1)*3750;
					}
					PORTC|=(1<<(j-1));
					temp1=(50*angle1[i][j]/3+750)+(j-1)*3750;
					do
					{
						temp=TCNT1;
						if(temp>temp1)PORTC&=~(1<<(j-1));
						if(temp>temp2)PORTA&=~(1<<(j-1));
					}
					while(temp<j*3750);
				}
				TCNT1=0x00;
			}
			do
			{
				for(i=cycle_start1;i<totalstep1;i++)
					for(t=angle1[i][0];t>0;t--)
					{
						TCNT1=0x00;	
						for(j=1;j<9;j++)
						{
							if(j<5)
							{
								PORTA|=(1<<(j-1));
								temp2=(50*angle1[i][j+8]/3+750)+(j-1)*3750;
							}
							PORTC|=(1<<(j-1));
							temp1=(50*angle1[i][j]/3+750)+(j-1)*3750;
							do
							{
								temp=TCNT1;	
								if(temp>temp1)PORTC&=~(1<<(j-1));
								if(temp>temp2)PORTA&=~(1<<(j-1));
							}
							while(temp<j*3750);
						}
						TCNT1=0x00;	
					}
					N1--;
			}
			while(N1);
			for(i=0;i<4;i++)
				for(t=angle1_2[i][0];t>0;t--)
				{
					TCNT1=0x00;
					for(j=1;j<9;j++)
					{
						if(j<5)
						{
							PORTA|=(1<<(j-1));	
							temp2=(50*angle1_2[i][j+8]/3+750)+(j-1)*3750;
						}
						PORTC|=(1<<(j-1));
						temp1=(50*angle1_2[i][j]/3+750)+(j-1)*3750;
						do
						{
							temp=TCNT1;	
							if(temp>temp1)PORTC&=~(1<<(j-1));
							if(temp>temp2)PORTA&=~(1<<(j-1));
						}
						while(temp<j*3750);
					}
					TCNT1=0x00;
				}
	}
	if(p==2)
	{
		for(i=0;i<cycle_start2;i++)
			for(t=angle2[i][0];t>0;t--)  
			{
				TCNT1=0x00;
				for(j=1;j<9;j++)	
				{
					if(j<5)
					{
						PORTA|=(1<<(j-1));
						temp2=(50*angle2[i][j+8]/3+750)+(j-1)*3750;
					}
					PORTC|=(1<<(j-1));	
					temp1=(50*angle2[i][j]/3+750)+(j-1)*3750;	
					do
					{
						temp=TCNT1;	
						if(temp>temp1)PORTC&=~(1<<(j-1));	
						if(temp>temp2)PORTA&=~(1<<(j-1));}while(temp<j*3750);        
				}
				TCNT1=0x00;		
			}
			do
			{ 
				for(i=cycle_start2;i<totalstep2;i++)
					for(t=angle2[i][0];t>0;t--)
					{
						TCNT1=0x00;	
						for(j=1;j<9;j++)	
						{
							if(j<5)
							{
								PORTA|=(1<<(j-1));	
								temp2=(50*angle2[i][j+8]/3+750)+(j-1)*3750;
							}
							PORTC|=(1<<(j-1));	
							temp1=(50*angle2[i][j]/3+750)+(j-1)*3750;	       
							do
							{
								temp=TCNT1;	
								if(temp>temp1)PORTC&=~(1<<(j-1));	
								if(temp>temp2)PORTA&=~(1<<(j-1));
							}
							while(temp<j*3750);        
						}
						TCNT1=0x00;		
					}
					N2--;	 
			}
			while(N2); 
			for(i=0;i<4;i++)
				for(t=angle2_3[i][0];t>0;t--)    
				{
					TCNT1=0x00;
					for(j=1;j<9;j++)	
					{
						if(j<5)
						{
							PORTA|=(1<<(j-1));	
							temp2=(50*angle2_3[i][j+8]/3+750)+(j-1)*3750;
						}
						PORTC|=(1<<(j-1));	
						temp1=(50*angle2_3[i][j]/3+750)+(j-1)*3750;	
						do
						{
							temp=TCNT1;	
							if(temp>temp1)PORTC&=~(1<<(j-1));	
							if(temp>temp2)PORTA&=~(1<<(j-1));}while(temp<j*3750);
					}
					TCNT1=0x00;		
				}
	} 
	if(p==3)
	{
		for(i=0;i<cycle_start3;i++)
			for(t=angle3[i][0];t>0;t--)    
			{
				TCNT1=0x00;
				for(j=1;j<9;j++)	
				{
					if(j<5)
					{
						PORTA|=(1<<(j-1));	
						temp2=(50*angle3[i][j+8]/3+750)+(j-1)*3750;
					}
					PORTC|=(1<<(j-1));	
					temp1=(50*angle3[i][j]/3+750)+(j-1)*3750;	
					do
					{
						temp=TCNT1;	
						if(temp>temp1)PORTC&=~(1<<(j-1));	
						if(temp>temp2)PORTA&=~(1<<(j-1));}while(temp<j*3750);       
				}
				TCNT1=0x00;		
			}
			do
			{ 
				for(i=cycle_start3;i<totalstep3;i++)
					for(t=angle3[i][0];t>0;t--)
					{
						TCNT1=0x00;	
						for(j=1;j<9;j++)	
						{
							if(j<5)
							{
								PORTA|=(1<<(j-1));	
								temp2=(50*angle3[i][j+8]/3+750)+(j-1)*3750;
							}
							PORTC|=(1<<(j-1));	
							temp1=(50*angle3[i][j]/3+750)+(j-1)*3750;	       
							do
							{
								temp=TCNT1;	
								if(temp>temp1)PORTC&=~(1<<(j-1));	
								if(temp>temp2)PORTA&=~(1<<(j-1));
							}
							while(temp<j*3750);     
						}
						TCNT1=0x00;
					}
					N3--;	 
			}
			while(N3); 
			for(i=0;i<4;i++)
				for(t=angle3_4[i][0];t>0;t--)    
				{
					TCNT1=0x00;
					for(j=1;j<9;j++)	
					{
						if(j<5)
						{
							PORTA|=(1<<(j-1));	
							temp2=(50*angle3_4[i][j+8]/3+750)+(j-1)*3750;
						}
						PORTC|=(1<<(j-1));	
						temp1=(50*angle3_4[i][j]/3+750)+(j-1)*3750;	
						do
						{
							temp=TCNT1;	
							if(temp>temp1)PORTC&=~(1<<(j-1));	
							if(temp>temp2)PORTA&=~(1<<(j-1));}while(temp<j*3750);
					}
					TCNT1=0x00;		
				}
	} 
	if(p==4)
	{
		for(i=0;i<cycle_start4;i++)
			for(t=angle4[i][0];t>0;t--)    
			{
				TCNT1=0x00;
				for(j=1;j<9;j++)	
				{
					if(j<5)
					{
						PORTA|=(1<<(j-1));	
						temp2=(50*angle4[i][j+8]/3+750)+(j-1)*3750;
					}
					PORTC|=(1<<(j-1));	
					temp1=(50*angle4[i][j]/3+750)+(j-1)*3750;	
					do
					{
						temp=TCNT1;	
						if(temp>temp1)PORTC&=~(1<<(j-1));	
						if(temp>temp2)PORTA&=~(1<<(j-1));}while(temp<j*3750);        
				}
				TCNT1=0x00;		
			}
			do
			{ 
				for(i=cycle_start4;i<totalstep4;i++)
					for(t=angle4[i][0];t>0;t--)
					{
						TCNT1=0x00;	
						for(j=1;j<9;j++)	
						{
							if(j<5)
							{
								PORTA|=(1<<(j-1));	
								temp2=(50*angle4[i][j+8]/3+750)+(j-1)*3750;
							}
							PORTC|=(1<<(j-1));	
							temp1=(50*angle4[i][j]/3+750)+(j-1)*3750;	       
							do
							{
								temp=TCNT1;	
								if(temp>temp1)PORTC&=~(1<<(j-1));	
								if(temp>temp2)PORTA&=~(1<<(j-1));
							}
							while(temp<j*3750);        
						}
						TCNT1=0x00;		
					}	 
					N4--;	 
			}
			while(N4); 
	} 

}
  
//*******************************************************************************
//函数名: main() 
//参  数:
//功  能:
//*******************************************************************************

void main(void)
{
	void port_init(void);//I/O端口的初始化 
	void timer1_init(void); //定时器的初始化
	void Motion_init(void); 
	void ClrAll(void);
	void Motion_excute(unsigned int p);
	CLI(); //disable all interrupts
	MCUCR=0x00;
	XDIV  = 0x00; //xtal divider
	XMCRA = 0x00; //external memory
	port_init();
	timer1_init();
	ClrAll();
	TCCR1B = 0x02; //start Timer
	Motion_init();
	TCNT1=0;
	TCNT0=0x00;
	Motion_init(); 
	ClrAll();      
	Motion_excute(1);
	Motion_init();
	Motion_excute(2);
	Motion_init();
	Motion_excute(3);
	Motion_init();
	Motion_excute(4);
 	Motion_init();
 	ClrAll();
}


⌨️ 快捷键说明

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