基于51单片机的太阳追光系统设计

本实例是基于51单片机的太阳追光系统,主要硬件由51单片机最小系统,四路光敏感应电路,ADC0832转换电路、LED指示灯电路、X轴与Y轴步进电机构成。

设计功能

1.四路光敏感应电路:四路光敏电阻分别感应上、下、左、右四个方向的光强度,并通过AD转换芯片将光强数据传递给单片机。
2.5路按键:分别为手自动切换按键、上、下、左、右按键;按键切换为自动模式,系统便根据四个方向的光强对太阳进行追光;手动模式便可通过按键对四个方向进行任意转动。
3.X、Y轴步进电机:两路步进电机通过ULN2803进行驱动;X轴电机控制系统水平方向转动;Y轴电机控制系统上下方向转动 。
4.LED指示灯:分别为自动、手动状态灯;在相应的控制模式下亮起。5.按键功能:

(1)从左到右:按键1为设置键,按键2为加一键、按键3为减一键、按键4为手动自动切换键,按键5远近光灯切换键,按键6为手动关灯键。
(2)单独一个按键为复位按键。

原理图

在这里插入图片描述
Protues仿真:
在这里插入图片描述

void main()	   //函数功能:主函数
{	  
 
	uchar AD_Up,AD_Down,AD_Left,AD_Right;
	uchar i,j,k;
	uchar Table[4];
	Delay_ms(500);
	UART_Init();	
	
	
	lcd_init(); //lcd相应的初始化
	lcd_show("Welcome to","light tracking!");
	Delay_ms(500);
	
	while(1)
	{
	   
	    if(!ENTER)
		{
			Delay_ms(20);
			while(!ENTER);
			k++;	
		}
		
		if(k%2 == 0)
		{
		Delay_ms(20); //延时去抖动
		if(k%2 == 0) 
		{
		LED1 = 1;
    	LED2 = 0;
		AD_Up = 0;	//上
		AD_Down = 0;//下
		AD_Left = 0;//左
		AD_Right = 0;//右	
		
		lcd_show("Currently in ","manual mode");
		
		//判断手动模式下,哪个按键被按下,并作出相应的旋转
		if(!UP)//上转 
		{
			for(i=0;i<10;i++)
			{ 
				for(j=0;j<4;j++)
				{
					P1=Motor_Up[j];
		            lcd_show("You pressed ","the up button!");
					Delay_ms(5);
				}		
			}
			
			P1=0x00;	
		}
		if(!DOWN)//下转
		{
			for(i=0;i<10;i++)
			{ 
				for(j=0;j<4;j++)
				{
					P1=Motor_Down[j];
					lcd_show("You pressed ","the down button!");
					Delay_ms(5);
				}		
			}
			P1=0x00;
		}	
		if(!LEFT)//左转
		{
			for(i=0;i<10;i++)
			{ 
				for(j=0;j<4;j++)
				{
					P1=Motor_Left[j];
					lcd_show("You pressed ","the left button!");
					Delay_ms(5);
				}		
			}
			P1=0x00;
		}
		
		if(!RIGHT)//右转
		{
			for(i=0;i<10;i++)
			{ 
				for(j=0;j<4;j++)
				{
					P1=Motor_Right[j];
					lcd_show("You pressed ","the right button!");
					Delay_ms(5);
				}		
			}
			P1=0x00;
		}
		}
		}
		if(k%2 == 1)  //处于自动模式下
		{
		Delay_ms(20); //延时去抖动
		if(k%2 == 1)
		{
			LED1 = 0;
			LED2 = 1;
			for(i=0;i<8;i++)
		{
			switch(AD_CHANNEL)
			{
				case 0: PCF8591_ISendByte(PCF8591,0x01);
				AD_Up = PCF8591_IRcvByte(PCF8591);  //ADC0 模数转换1
				break;  				
				case 1: PCF8591_ISendByte(PCF8591,0x02);
				AD_Down = PCF8591_IRcvByte(PCF8591);  //ADC1  模数转换2
				break;  
				
				case 2: PCF8591_ISendByte(PCF8591,0x03);
				AD_Left = PCF8591_IRcvByte(PCF8591);  //ADC2 模数转换3
				break;  
				
				case 3: PCF8591_ISendByte(PCF8591,0x00);
				AD_Right = PCF8591_IRcvByte(PCF8591);  //ADC3  模数转换4
				break; 
			}			
			if(++AD_CHANNEL>3)
			{
					AD_CHANNEL=0;
					Table[0] = AD_Up;
					Table[1] = AD_Down;
					Table[2] = AD_Left;
					Table[3] = AD_Right;
			 }			 
			// Send_Hex(Table,4);
		}
		
		lcd_xy(0,0); 
		lcd_display("1:",AD_Up);
		lcd_display("2:",AD_Down);
		
		lcd_xy(0,1);
		lcd_display("3:",AD_Left);
		lcd_display("4:",AD_Right);
		}			
		if(((AD_Up<AD_Down)&&(AD_Down-AD_Up>10)))//上转 
		{
			for(i=0;i<10;i++)
			{ 
				for(j=0;j<4;j++)
				{
					P1=Motor_Up[j];
					Delay_ms(5);
				}		
			}
			P1=0x00;	
			UP_COUNT++;
		}	
		if(((AD_Up>AD_Down)&&(AD_Up-AD_Down>10)))//下转
		{
			for(i=0;i<10;i++)
			{ 
				for(j=0;j<4;j++)
				{
					P1=Motor_Down[j];
					Delay_ms(5);
				}		
			}
			P1=0x00;
			DOWN_COUNT++;
		}
		if(((AD_Left<AD_Right)&&(AD_Right-AD_Left>10)))//左转
		{
			for(i=0;i<10;i++)
			{ 
				for(j=0;j<4;j++)
				{
					P1=Motor_Left[j];
					Delay_ms(5);
				}		
			}
			P1=0x00;
			LEFT_COUNT++;
		}
		if(((AD_Left>AD_Right)&&(AD_Left-AD_Right>10)))//右转
		{
			for(i=0;i<10;i++)
			{ 
				for(j=0;j<4;j++)
				{
					P1=Motor_Right[j];
					Delay_ms(5);
				}		
			}
			P1=0x00;
			RIGHT_COUNT++;
		}
		if(!UP)    //UP按键充当自动模式下的复位按键
		{
		 Delay_ms(20); //延时去抖动
		 if(!UP)
		 {   
		   while(DOWN_COUNT--)
		   {
		     for(i=0;i<10;i++)
			{ 
				for(j=0;j<4;j++)
				{
					P1=Motor_Up[j];
		            lcd_show("Reseting "," ");
					Delay_ms(5);
				}		
			}   
		   }
		   
		   while(UP_COUNT--)
		   {
		     for(i=0;i<10;i++)
			{ 
				for(j=0;j<4;j++)
				{
					P1=Motor_Down[j];
		            lcd_show("Reseting "," ");
					Delay_ms(5);
				}		
			}
		    		   
		   }
		   
		    while(LEFT_COUNT--)
		   {
		     for(i=0;i<10;i++)
			{ 
				for(j=0;j<4;j++)
				{
					P1=Motor_Right[j];
		            lcd_show("Reseting "," ");
					Delay_ms(5);
				}		
			}
		    		   
		   }
		   
		    while(RIGHT_COUNT--)
		   {
		     for(i=0;i<10;i++)
			{ 
				for(j=0;j<4;j++)
				{
					P1=Motor_Left[j];
		           lcd_show("Reseting "," ");
					Delay_ms(5);
				}		
			}
		    		   
		   }
		   LEFT_COUNT = 0;
		   RIGHT_COUNT = 0;
		   UP_COUNT = 0;
		   DOWN_COUNT = 0;
		 }
		}
		}		
		Delay_ms(100);
		
	}
}

关注公众号-电子应用学习馆,首页回复“太阳能追光”获取。

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值