蓝桥杯电子类单片机组国赛——第六届“智能物料传送系统”

为了不吵到大家,我就没有在超载的时候操作蜂鸣器。其他的代码完成率都为100%。
大家感兴趣可以看一下我的代码。

#include "STC15F2K60S2.h"
#include "iic.h"
#include "intrins.h"

#define  u8 unsigned char

#define KEY_STATE_0 0
#define KEY_STATE_1 1
#define KEY_STATE_2 2

#define led_0 0x00
#define led_1 ((0x01)<<(1-1))
#define led_2 ((0x01)<<(2-1))
#define led_3 ((0x01)<<(3-1))
#define led_4 ((0x01)<<(4-1))
#define led_5 ((0x01)<<(5-1))
#define led_6 ((0x01)<<(6-1))
#define led_7 ((0x01)<<(7-1))
#define led_8 ((0x01)<<(8-1))

#define BUZZER 0X40
#define RELAY 0X10
#define NONE 0X00

#define KEY_STATE0 0
#define KEY_STATE1 1
#define KEY_STATE2 2

sbit TX=P1^0;//发送脚
sbit RX=P1^1;//接收脚

u8 code smg_du[]={                       //标准字库
//   0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black  -     H    J    K    L    N    o   P    U     t    G    Q    r   M    y
    0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
    0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};    //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1
u8 code smg_wei[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
u8 code led_display[10]={led_0,led_1,led_2,led_3,led_4,led_5,led_6,led_7,led_8};
u8 code BURE_display[3]={NONE,RELAY,BUZZER};


int distance=0;
bit sonic_flag=0;//超声波检测标志位
bit key_flag=0;
u8 key_val=0;
u8 led_choose=0;
u8 bure_choose=0;
u8 smg_sonic[8];//规定超声波数码管显示数组
u8 adc_val=0;
u8 object_flag=0;
u8 save_object_flag=0;

u8 object_1_time=2;
u8 object_2_time=4;
u8 save_object_1_time;
u8 save_object_2_time;

bit led_blink_flag=0;

unsigned char KEY_SCAN()//按键扫描函数
{
		static u8 key_state=KEY_STATE_0;
		u8 KEY=0;
		u8 key_val=0;
	
		P30=1;P31=1;P32=1;P33=1;
		if(P30==0)   KEY=7;
		if(P31==0)   KEY=6;
		if(P32==0)   KEY=5;
		if(P33==0)   KEY=4;
		if((P30==1)&&(P31==1)&&(P32==1)&&(P33==1))  KEY=0;

		switch(key_state)
	{
		case KEY_STATE_0: 
		if(KEY!=0)
		{
			key_state=KEY_STATE_1;
		}
		break;
		
		case KEY_STATE_1: 
		switch(KEY)
		{
				case 4: key_val=4; break;
				case 5: key_val=5; break;
				case 6: key_val=6; break;
				case 7: key_val=7; break;

		}
			key_state=KEY_STATE_2;
		break;
		
		case KEY_STATE_2:  
		if(KEY==0)
		{
			key_state=KEY_STATE_0;
		}
		
		break;
		
		
		
	}
	
	return key_val;
	

}

void Delay10ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 108;
	j = 145;
	do
	{
		while (--j);
	} while (--i);
}

void Delay12us()		//生成12ms的延时函数
{
	unsigned char i;

	_nop_();
	_nop_();
	_nop_();
	i = 30;
	while (--i);
}


void send_wave(void)//进行超声波检测
{
			 unsigned char i=2;//注意这个参数
			 do
				 {
						TX=1;//打开发送脚
						Delay12us();
					  TX=0;//关闭发送脚
					 	Delay12us();

						
				 }
				 while(i--);
 }
 
 

void Timer_Init(void)
{
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x66;		//设置定时初值
	TH0 = 0xEA;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时	
	ET0 = 1;
	EA=1; 
}



//下面定义一些逻辑变量!
u8 transfer_mode=0;//0:无状态  1:空载  2:非空载  3:过载

u8 smg_display_mode=0;//0:无状态  1:显示界面1  2:显示界面2    3:显示界面3

u8 judge_object_flag=0;//0: 传送带上没货物    1: 传送带上有货物静止且在一开头   2:传送带正在运行货物  3:传送带停止传送货物并且货物静止


u8 smg_0[8];//第0种数码管显示界面
u8 smg_1[8];//第一种数码管显示界面
u8 smg_2[8];//第二种数码管显示界面
u8 smg_3[8];//第三种数码管显示界面

u8 S4_S5_count=0;//重启一次记得清0~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
u8 S6_count=0;

void main(void)
{

	P2=0xa0;P0=0x00;P2=0x00; 
	P2=0x80;P0=~0x00;P2=0x00;		

	save_object_1_time=read_24c02(0);
	save_object_2_time=read_24c02(1);
	Timer_Init(); //1ms 
	

	while(1)
	{
		
		smg_0[0]=smg_du[16];
		smg_0[1]=smg_du[16];
		smg_0[2]=smg_du[16];
		smg_0[3]=smg_du[16];
		smg_0[4]=smg_du[16];
		smg_0[5]=smg_du[16];
		smg_0[6]=smg_du[16];
		smg_0[7]=smg_du[16];
		
		
		adc_val=read_adc(0x03);
		if(adc_val<51)
		{
			transfer_mode=1;//1:空载 
			led_choose=1;
			smg_display_mode=3;//````````````````````````````````````````````````````````````````````````````````````````显示



		}
		
		else if(adc_val<204)
		{
			transfer_mode=2;// 2:非空载 
			if((judge_object_flag==0)||(judge_object_flag==1))
			{
						led_choose=2;
						smg_display_mode=1;
						S4_S5_count=0;//已清0
				
				
						object_1_time=save_object_1_time;
						object_2_time=save_object_2_time;

			}
		}
		
		else
		{
			transfer_mode=3;// 3:过载
			smg_display_mode=0;
			if(led_blink_flag)
			{
			led_choose=3;
			}
			else
			{
				led_choose=0;
			}
			//蜂鸣器报警!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		}
		
		if((transfer_mode==2)&&(S4_S5_count==0)) //非空载且S4和S5不按下
		{
				smg_display_mode=1;
				judge_object_flag=1;//传送带上有货物且在一开始
			
				smg_1[0]=smg_du[1];
				smg_1[1]=smg_du[16];
				smg_1[2]=smg_du[16];
		   	smg_1[3]=smg_sonic[1];
		   	smg_1[4]=smg_sonic[2];
				smg_1[5]=smg_du[16];
				smg_1[6]=smg_du[16];
				smg_1[7]=smg_du[object_flag];
			
		}
		
		if((transfer_mode==2)&&((judge_object_flag==2)||(judge_object_flag==3)))//传送带上有货物()
		{
					smg_2[0]=smg_du[2];
					if(save_object_flag==1)
					{
							smg_2[6]=smg_du[object_1_time/10]; smg_2[7]=smg_du[object_1_time%10];
							if(object_1_time==0)   judge_object_flag=0;  //传送带没货物了
					}
					
					if(save_object_flag==2)
					{
							smg_2[6]=smg_du[object_2_time/10]; smg_2[7]=smg_du[object_2_time%10];
							if(object_2_time==0)   judge_object_flag=0;  //传送带没货物了
					}
					

		}
		
		
		if(judge_object_flag==0)
		{
						bure_choose=0;
		}
		
		if(judge_object_flag==1)
		{
						bure_choose=0;
		}
		
		if(judge_object_flag==2)
		{
				bure_choose=1;
				led_choose=0;
		}
		
		if(judge_object_flag==3)
		{
				bure_choose=0;
				if(led_blink_flag)
				{
				led_choose=4;
				}
				else
				{
				led_choose=0;
				}
				
		}		
		
		if(S6_count==0)
		{
				smg_3[0]=smg_du[16];
				smg_3[1]=smg_du[16];
				smg_3[2]=smg_du[16];
				smg_3[3]=smg_du[16];
				smg_3[4]=smg_du[16];
				smg_3[5]=smg_du[16];
				smg_3[6]=smg_du[16];
				smg_3[7]=smg_du[16];

		}
		
		if(S6_count==1)
		{
							smg_3[0]=smg_du[3];
			
							if(led_blink_flag)
							{
							smg_3[3]=smg_du[save_object_1_time/10]; smg_3[4]=smg_du[save_object_1_time%10];
							}
							else
							{
							smg_3[3]=smg_du[16]; smg_3[4]=smg_du[16];
							}
							
							
							smg_3[6]=smg_du[save_object_2_time/10]; smg_3[7]=smg_du[save_object_2_time%10];
							write_24c02(0,save_object_1_time);
							Delay10ms();
							write_24c02(1,save_object_2_time);
							Delay10ms();

		}
		
		if(S6_count==2)
		{
							smg_3[0]=smg_du[3];
			
							smg_3[3]=smg_du[save_object_1_time/10]; smg_3[4]=smg_du[save_object_1_time%10];

							if(led_blink_flag)
							{
							smg_3[6]=smg_du[save_object_2_time/10]; smg_3[7]=smg_du[save_object_2_time%10];
							}
							else
							{
							smg_3[6]=smg_du[16]; smg_3[7]=smg_du[16];
							}
							write_24c02(0,save_object_1_time);
							Delay10ms();
							write_24c02(1,save_object_2_time);
							Delay10ms();
							
							
		}
		
		if(key_flag)
		{
			key_flag=0;
			key_val=KEY_SCAN();
			switch(key_val)
			{
				
				case 6: 
				if(transfer_mode==1)
				{
							S6_count++;
							if(S6_count==3)
							{
								S6_count=0;
							}
				}
					
				
				break;
				case 7: 
				if(S6_count==1)
				{
					save_object_1_time++;
					if(save_object_1_time>=11)
					{
						save_object_1_time=1;
					}
				}
				
				if(S6_count==2)
				{
					save_object_2_time++;
					if(save_object_2_time>=11)
					{
						save_object_2_time=1;
					}
				}
				
				
				break;
				
				case 4: 
				if((transfer_mode==2)&&(judge_object_flag==1))//传送带上有货物并且非空载
				{
					S4_S5_count=1;
					save_object_flag=object_flag;//保存货物类型
					judge_object_flag=2;  //正在运行货物
					smg_display_mode=2;
				}

				break;
				
				
				case 5: 
				if((transfer_mode==2)&&(S4_S5_count==1))//传送带上正在运行货物并且非空载
				{
						S4_S5_count=1;
						if(judge_object_flag==2)
						{
								judge_object_flag=3;  //货物静止
						}
						
						
						else 
						{	
								judge_object_flag=2;  //货物运动
						}
					
						
				}
				


				break;
				
				

			}
		}
		

		if(sonic_flag)//进行超声波检测
		{
				sonic_flag=0;
				send_wave();
				TR1 = 1;//定时器1中断打开		
				while((RX==1)&&(TF1==0));//接收到信号并且无溢出
				TR1=  0;//定时器1中断关闭
			
							if(TF1==1)//如果数值溢出
							{
									TF1=0;//将数值变为0
							}
							else
							{

									distance=(unsigned int)(((TH1<<8)|TL1)*0.017);
	
							}
				TH1=0;//归0
				TL1=0;//归0
							
				if(distance<=30)
				{
					object_flag=1;
				}
				else 
				{
					object_flag=2;
					if(distance>=99)
					{
						distance=99;
					}
				}
				
				smg_sonic[0]=smg_du[distance/100];
				smg_sonic[1]=smg_du[distance/10%10];
				smg_sonic[2]=smg_du[distance%10];
		}

		
		

	}
}

void timer0() interrupt 1  using 1              //0.5毫秒中断
{
	static int smg_count=0,i=0,sonic_count=0,key_count=0,counter=0,led_count=0;
  smg_count++;sonic_count++;key_count++;led_count++;
	
	if(led_count==1000)
	{
			led_count=0;
			led_blink_flag=~led_blink_flag;
	}	
	
	if(judge_object_flag==2)//正在运行
	{
				counter++;
				if(counter==2000)
			{
					counter=0;
					if(save_object_flag==1)
					{
							  if(object_1_time!=0) object_1_time--;
					}
					
					if(save_object_flag==2)
					{
							  if(object_2_time!=0) object_2_time--;
					}
			}
	}

	if(smg_count==1)
	{
		P0=~led_display[led_choose]; P2=0X80;P2=0X00;
	}
	
	if(smg_count==2)
	{
	  P0=BURE_display[bure_choose]; P2=0XA0;P2=0X00;
	}
	
	if(smg_count==3)
	{		
		
	}
	
	if(smg_count==4)			//2ms
	{
		smg_count=0;
		P2=0xc0;P0=0;P2=0;				//лӰ
		
		switch(smg_display_mode)
		{
			case 0: 		P2=0xe0;P0=~smg_0[i];P2=0;     break;
			case 1: 		P2=0xe0;P0=~smg_1[i];P2=0;     break;
			case 2: 		P2=0xe0;P0=~smg_2[i];P2=0;     break;
			case 3:	   	P2=0xe0;P0=~smg_3[i];P2=0;     break;
		}
		P2=0xc0;P0=smg_wei[i];P2=0;
		i++;
		if(i==8) i=0;
	}
	if(sonic_count==50)//50ms扫描一次按键
	{
		sonic_count=0;
		sonic_flag=1;
	}
	
	if(key_count==10)
	{
			key_count=0;
			key_flag=1;
	}
	
}

代码就是这么多,也就不到600行代码。
这里没有贴“iic.c“和”iic.h“的代码,有需要的同学点击下面的链接:

  • https://blog.csdn.net/qq_44629819/article/details/104524886

祝大家在蓝桥杯比赛中获得优异的成绩!
——南昌大学 电子183刘昊

你可能感兴趣的:(蓝桥杯电子类单片机组国赛——第六届“智能物料传送系统”)