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

📄 2008chinarobot.c

📁 双足竞步机器人
💻 C
字号:
#include "avr/io.h"
#include "avr/interrupt.h"
#include "math.h"

#define uint unsigned int
#define uchar unsigned char

static uint countd=0;
static uint sound=0;
static int count20shua=19760;//zx(3,660)zx(4,2220);
static int shuacurrent[15]={1500,1500,660,2220,1500,1500,1440,1500,1500,1500,1500,1500,1500,1400,1500};//{1360,1340,1420,1280,1460,1320}; 
static int shuanext[16]={1500,1500,660,2220,1500,1500,1440,1500,1500,1500,1500,1500,1500,1400,1500,1000};//{1360,1340,1420,1280,1460,1320};
static int shuaadd[15]={};
static int shuacurrentl[15]={};


uchar tmp;
uchar Tx_Pos = 0;
uchar Tx_Len = 0;

uchar Rx_Pos = 0;
uchar Rx_Len = 0;
uchar SendBuf[16];       //发送数据缓冲区
uchar RecvBuf[16];		//接收数据缓冲区

//==================================================
SIGNAL(SIG_UART_TRANS)  //发送结束中断 
{
	if(--Tx_Len>0)
	{
		while(!(UCSRA&(1 << UDRE)));   //判断上次发送有没有完成
		UDR = SendBuf[++Tx_Pos];	
	}
}
//====================================================
void SendToUart(uchar size)            //发送缓冲区的数据
{
	UDR = SendBuf[0];
	Tx_Pos = 0;
	Tx_Len = size;
}
//====================================================
void RecvFromUart(uchar size,uchar bwait)
{
	Rx_Pos = 0;
	Rx_Len = size;
	if(bwait)
		while(Rx_Len);
}
//====================================================
unsigned char USART_Receive( void )
{
	while ( !(UCSRA & (1<<RXC)));       // 等待接收数据
	return UDR;                         //从缓冲器中获取并返回数据
}
//====================================================
void USART_Transmit( unsigned char data )
{
	/* 等待发送缓冲器为空 */
	while (!( UCSRA & (1<<UDRE)));
	/* 将数据放入缓冲器,发送数据 */
	UDR = data;
}
//=====================================================

void _ms_delay(uint j)//延时,每次延时为1ms
{
	uint m;
	for(;j>0;j--)
	{
		for(m = 0;m <= 210;m ++)
			{;}
	}
}

void dongzhuo()
{
	int temp1,i,temp=0;

	for(i=14;i>=0;i--)
	{
		shuaadd[i]=shuanext[i]-shuacurrent[i];
		if(fabs(shuaadd[i])>temp)temp=fabs(shuaadd[i]);
	//	temp=temp*10/9;
		}

	for(i=14;i>=0;i--)
	{
		shuaadd[i]=(shuaadd[i]*10)/temp;
		shuacurrentl[i]=shuacurrent[i]*10;
		}

	for(;temp!=0;temp--)
	{
		for(i=14;i>=0;i--)
		{
			shuacurrentl[i]=shuacurrentl[i]+shuaadd[i];
			shuacurrent[i]=(shuacurrentl[i]/10);
			}

		for(temp1=shuanext[15];temp1!=0;temp1--);//延时
		
		}

	for(i=14;i>=0;i--)
	{
		shuacurrent[i]=shuanext[i];
		}
	
}

unsigned char uart_r( void )
{
	uint x,y,z;
	uchar k;
	z=0;
	k=255;
	for(y=0;y<2000;y++)
	{
		for(x=0;x<1600;x++)
		{ 	
			if ( (UCSRA & (1<<RXC)) ) { k=UDR;z=1;break;}	
		
		}
		if(z==1) break;
	}
	return k;
}

SIGNAL(SIG_OVERFLOW1)
{
		switch (sound)
			{
				case 0: {
							PORTB&=0X00;                      //测定每加一个数在8M ,8分频下为32US
							if(countd<7){
											PORTB|=(1<<countd);				
    										TCNT1H=(65536-shuacurrent[countd])/256;
											TCNT1L=(65536-shuacurrent[countd])%256;
											countd++;
    										count20shua+=shuacurrent[countd];
  											}
 								else 
 										{
    										TCNT1H=(count20shua+65536-19760)/256;
											TCNT1L=(count20shua+65536-19760)%256;
											count20shua=0;
											sound=1;
											countd=0;
  											}
							};break;

			   	  case 1:{
				  				PORTA&=0X00;
								if(	countd<8)
									{
										PORTA|=(1<<countd);
    									TCNT1H=(65536-shuacurrent[countd+7])/256;
										TCNT1L=(65536-shuacurrent[countd+7])%256;
										countd++;  
    									count20shua+=shuacurrent[countd+7];
  										}

								else 
 									{
  								 	 	TCNT1H=(count20shua+65536-19760)/256;
										TCNT1L=(count20shua+65536-19760)%256;
										count20shua=0;
										sound=0;
										countd=0;
  										}

							};break;
							}
}

void IO_INT()                //初始化端口
{
	DDRB=0XFF;
	PORTB=0X00;
	DDRA=0XFF;
	PORTA=0X00;
}

void TIMER1_INIT()
{
	cli();
	TCCR1B = 0x00;               //停 
	TCNT1H = (65536-1500)/256;  //设置最初值
	TCNT1L = (65536-1500)%256; 
	TCCR1B = 0x02;               //8分频 启动 
	sei();
}

void USART_INIT()
{
	int baud = 51;
	UBRRH = (unsigned char)(baud>>8); //设置波特率 
	UBRRL = (unsigned char)baud;
	UCSRB = (1<<RXEN)|(1<<TXEN);      //初始化串口寄存器的控制位(1<<RXCIE)|(1<<TXCIE)|
	UCSRC = (1<<URSEL)|(3<<UCSZ0);    //设置帧格式8位1停止位         									
    sei();							  //使能全局中断
}

int main()
{
	OSCCAL=0xAA;                         //系统时钟校准,不同的芯片和不同的频率   
	uchar data1,data2,data3;
	uint datadj,datadz;
	cli();
	IO_INT();
	TIMER1_INIT();
	USART_INIT();
	MCUCR = 0x00;
	GICR  = 0x00;
	TIMSK = 0x04;
	sei();

	while(1)
		{
	 		while(1)
				{
					tmp=USART_Receive();
					if(tmp==0xff)break;				
	   			 }

			 cli();
			 while(1)
				{
					data1=uart_r();		
					if(data1==0xff)break;
					USART_Transmit(data1);
					data2=uart_r();
					USART_Transmit(data2);
					data3=uart_r();
					USART_Transmit(data3);
					datadj=data1-1;
					datadz=data2;
					datadz=((datadz<<8)+data3);
					shuanext[datadj]=datadz;
				}

		      sei();
			  dongzhuo();
   	 	 }
}

⌨️ 快捷键说明

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