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

📄 robot01.c

📁 自己做的机器人01
💻 C
字号:
#include "avr/io.h"
#include "avr/interrupt.h"

#define uint unsigned int
#define uchar unsigned char

static uint count=0;
static uint countd=0;
static uint _20delay=0;
static uint shuacurrent[6]={1500,1500,1500,1500,1500,1500};
static uint shuanext[6]={1500,1500,1500,1500,1500,1500};
static uint shua[]={1500,1500,1500,1500,1500,1500,1300,1200,1500,1500,1500,1500,1200,1500,1500,1500,1500,1500,\
					1300,1200,1500,1500,1500,1500,1200,1500,1500,1500,1500,1500,1100,1500,1500,1500,1500,1500}; 
static uint count20shua=19760;

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

SIGNAL(SIG_OVERFLOW1)
{
	PORTB &= 0X00;                      //测定每加一个数在8M 256分频下为32US
	if(_20delay==0)
	{
		PORTB|=(1<<countd); 
	    TCNT1H=(65536-shuacurrent[countd])/256;
		TCNT1L=(65536-shuacurrent[countd])%256;  
		count20shua-=shuacurrent[countd];
		countd++;
		if(countd==6)
		{
			_20delay=1;
			countd=0;
		}
	 }
	 else
 	{
    	TCNT1H=(65536-count20shua)/256;
		TCNT1L=(65536-count20shua)%256;
		count20shua=19760;
		_20delay=0;
	}
}
void TIMER1_INIT()
{
	cli();
	TCCR1B = 0x00;             //停 
	TCNT1H = (65536-19760)/256;  //设置最初值
	TCNT1L = (65536-19760)%256; 
	TCCR1B = 0x02;             //8分频 启动 
	TIMSK = 0x04; 
	TIFR &= 0x00;              //清除中断标志
	sei();
}

int main()
{
	uint i;
	uint times=0;
	uint _delay;
	uint temp;
	DDRB=0XFF;                         //初始化端口
	PORTB=0X00;
	TIMER1_INIT();
	sei();
	_ms_delay(500);
	while(1)
	{
		i=0;
		while(i<6)
		{
			shuanext[i]=shua[count];
			temp=(shuanext[i]-shuacurrent[i])/11;
			if(temp>times)times=temp;
			count++;
			i++;
		}	
		_ms_delay(300);
		i=6;
		_delay=1000/times;
		while(times)
		{
			while(i)
			{
				if(shuacurrent[i-1]>shuanext[i-1])shuacurrent[i-1]-=11;
				else shuacurrent[i-1]+=11;
				i--;
			}
			_ms_delay(_delay);
			times--;
		}
		if(count==35)count=0;

	}
}

⌨️ 快捷键说明

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