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

📄 motorcontrolsight.c

📁 单片机控制步进电机
💻 C
字号:
#include<avr/io.h>
#include<avr/delay.h>
#include<avr/signal.h>
#include<avr/interrupt.h>

#define uchar unsigned char
#define uint unsigned int

//常量定义
#define BAUDRATE        9600	//波特率
#define rotor1        32768	//左右无限转动
#define rotor2		9//上下转动的角度(电机转动6.3157度)
//#define round		32768//round
/* 
#define	0XA00000081E1EAF07 1
#define	0XA00000101E1EAF1F 2
#define	0XA00000041E1EAF0B 3
#define	0XA00000021E1EAF0D 4
*/
unsigned char FLAG;
unsigned char STOP_FLAG;
unsigned char RUNNING_FLAG;
volatile unsigned char PC_COMMAND;		//PC发出的当前命令
/*	delay program	*/
void	mdelay(uint16_t delaytime)	
{
	uint16_t	i;
	for(;delaytime>0;delaytime--)
		{
			for(i=0;i<400;i++)
			{;}
		}
}
void	ldelay(uint16_t delaytime1)
{
	for(;delaytime1>0;delaytime1--)
	{;}
}
/*
void put_c(unsigned char c) //发送采用查询方式
{
	while( !(UCSRA & (1<<UDRE)) );
	UDR=c;
}
*/

/*上下与左右与前版本反一反*/

/*	control the motor turn left(up)	*/
void rotleft(void)
{
	uint16_t	r;
//	uint16_t	s;//电机每次左转20度
//	uint16_t	t;//电机每次左转间隔1秒
	RUNNING_FLAG = 1;
	FLAG = 2;
	for(r=0;r<rotor1;r++)
	{
		if (bit_is_set(PIND,3))
		{
			if (STOP_FLAG==0)
			{
			//for(s=0;s<30;s++)
			//{
			//DDRB=0xff;
			mdelay(250);		//电机向上转动的速度
			PORTB=0x0e;
			mdelay(250);
			PORTB=0x0c;
			mdelay(250);
			PORTB=0x0d;
			mdelay(250);
			PORTB=0x09;
			mdelay(250);
			PORTB=0x0b;
			mdelay(250);
			PORTB=0x03;
			mdelay(250);
			PORTB=0x07;
			mdelay(250);
			PORTB=0x06;
			}
			//PORTB=0x00;
			//for (t=0;t<60;t++)
			//{
				//ldelay(30000);
				//}
			//}
			else if (STOP_FLAG==1)
			{
			PORTB=0x00;
			RUNNING_FLAG =0;
			STOP_FLAG = 0;
			return;
			}
		}
		else
		{
		PORTB=0x00;
		RUNNING_FLAG = 0;
		STOP_FLAG = 0;
		return;
		}
	}
}
	/*	control the motor turn right(down)	*/
void rotright(void)
{
	uint16_t	l;
//	uint16_t	m;//电机每次右转20度
//	uint16_t	n;//电机每次右转间隔1秒
	RUNNING_FLAG = 1;
	FLAG = 2;
	for(l=0;l<rotor1;l++)
	{
		if (bit_is_set(PIND,4))
		{
			if (STOP_FLAG==0)
			{
			//for(m=0;m<30;m++)
			//{
			//DDRB=0xff;
			mdelay(250);		//电机向下转动的速度
			PORTB=0x06;
			mdelay(250);
			PORTB=0x07;
			mdelay(250);
			PORTB=0x03;
			mdelay(250);
			PORTB=0x0b;
			mdelay(250);
			PORTB=0x09;
			mdelay(250);
			PORTB=0x0d;
			mdelay(250);
			PORTB=0x0c;
			mdelay(250);
			PORTB=0x0e;
			//}
			//PORTB=0x00;
			//for (n=0;n<60;n++)
			//{
				//ldelay(30000);
				//}
			}
			else if (STOP_FLAG==1)
			{
			PORTB=0x00;
			RUNNING_FLAG = 0;
			STOP_FLAG = 0;
			return;
			}
		}
		else
		{
		PORTB=0x00;
		RUNNING_FLAG = 0;
		STOP_FLAG = 0;
		return;
		}
	}
}
/*	control the motor turn up(left)	*/
void rotup(void)
{
	uint16_t	u;
	for(u=0;u<rotor2;u++)
		{	
		mdelay(1000);		//电机左转动的速度
		PORTB=0xe0;
		mdelay(1000);
		PORTB=0xc0;
		mdelay(1000);
		PORTB=0xd0;
		mdelay(1000);
		PORTB=0x90;
		mdelay(1000);
		PORTB=0xb0;
		mdelay(1000);
		PORTB=0x30;
		mdelay(1000);
		PORTB=0x70;
		mdelay(1000);
		PORTB=0x60;
		}
		FLAG = 2;
}
/*	control the motor turn down(right)	*/
void rotdown(void)
{
	uint16_t	d;
	for(d=0;d<rotor2;d++)
	{
	mdelay(1000);		//电机右转动的速度
	PORTB=0x60;
	mdelay(1000);
	PORTB=0x70;
	mdelay(1000);
	PORTB=0x30;
	mdelay(1000);
	PORTB=0xb0;
	mdelay(1000);
	PORTB=0x90;
	mdelay(1000);
	PORTB=0xd0;
	mdelay(1000);
	PORTB=0xc0;
	mdelay(1000);
	PORTB=0xe0;
	}
	FLAG = 2;
}
/*	io set	*/
void io_init(void)
{
	cli();
	PORTB	=	0x00;		//PORTB	=	0xf0; for 1 motor before
	DDRB	=	0xff;		//set PORTB as out put;	1 is output		0 is input
	/*PORTD3,4,5,6 set as the feed back information from tow motors */
	PORTD	=	PORTD|0x78;	//0xf0;set the PORTD3,4,5,6 at high;
	DDRD	=	DDRD&0x87;	//0xff;set the PORTD3,4,5,6 input;
	
}
/*	interrupt program receive the usart0 input signal and bring a interrupt*/
SIGNAL(SIG_USART0_RX)
{
	PC_COMMAND=UDR;		//the received signal put in the register UDR
	switch(PC_COMMAND)
	{
		case  0x01:	//rotor up //0x30 ASCII '0'
		//	put_c(0xff);
			if (RUNNING_FLAG==0)
			{
			FLAG = 0;
			break;
			}
			else
			{
			FLAG =2;
			break;
			}
		case  0x02:	//rotor down
		//	put_c(0x00);
			if (RUNNING_FLAG==0)
			{
			FLAG = 1;
			break;
			}
			else
			{
			FLAG =2;
			break;
			}
		case  0x03:	//rotor left
		//	put_c(0xf0);
			if (RUNNING_FLAG==0)
			{
			FLAG = 3;
			break;
			}
			else
			{
			FLAG = 2;
			break;
			}
		case  0x04:	//rotor right
		//	put_c(0x0f);
			if (RUNNING_FLAG==0)
			{
			FLAG = 4;
			break;
			}
			else
			{
			FLAG = 2;
			break;
			}
		case 0x05://stop_run
			if (RUNNING_FLAG==1)
			{
			STOP_FLAG = 1;
			break;
			}
			else
			{
			STOP_FLAG = 0;
			break;
			}
		default:
		//	put_c(0xe3);
			break;
	}
}
void inituart(void)
{

    UCSRC =  0x06;
    //异步,8位数据,无奇偶校验,一个停止位,无倍速

    UBRRL= (F_CPU/BAUDRATE/16-1)%256;
    UBRRH= (F_CPU/BAUDRATE/16-1)/256;
   
    UCSRA = 0x00;
	//receive and transmit enable
    UCSRB = (1<<RXCIE)|(1<<RXEN);//|(1<<TXEN);
}

int main(void)
{
	uint16_t	ii;
	inituart();
	io_init();
	RUNNING_FLAG = 0;
	STOP_FLAG = 0;
	FLAG = 2;
	for(ii=0;ii<5000;ii++)//屏蔽串口30秒,避过开机自检
	{
		ldelay(10000);
		}
 	sei();
	while(1) 
	{
	
	if((FLAG==0)&&(bit_is_set(PIND,5) ))
	{
		//DDRB	=	0xff;
		rotup();
	}
	else if ((FLAG==1)&&(bit_is_set(PIND,6)) )
	{
		//DDRB	=	0xff;
		rotdown();
	}
	else if ((FLAG==3)&&(bit_is_set(PIND,3)) )
	{
		//DDRB	=	0xff;
		rotleft();
	}
	else if ((FLAG==4)&&(bit_is_set(PIND,4)) )
	{
		//DDRB	=	0xff;
		rotright();
	}
	PORTB=0x00;
	}
}

⌨️ 快捷键说明

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