蓝桥杯单片机十二届国赛

main.c

#include "Define.h"

bit flag,flag_set;
u8 flag_display;
u8 time_sonic;
u16 length,length_save;
u8 time_key,test;
bit flag_C_F;
u8 set_time[5]={2,3,5,7,9};
u8 compare_time=2;
u8 local_time;
u16 set_length=20,set_length_compare;
u8 G_D_Z=0;
bit flag_sonic;
u16 length_max=0,length_min=100,length_arv=0;
bit flag_light=1,flag_light_pro;
u8 time_adc,ligth;
u8 Sec_pro;
u16 sonic_count;
u8 LED=0XFF;
u8 count=0;


void adc_process()
{
	if(time_adc>=100)
	{
		time_adc=0;
		Read_adc();
		ligth=Read_adc();
//		if(flag_C_F==0)
//		{
			flag_light_pro=flag_light;
			if(ligth<50)
				flag_light=0;
			else
				flag_light=1;
			
			if(flag_light_pro==1&&flag_light==0)
			{
				flag_sonic=1;//打开触发
			}
			
//		}
		
		if(length_save<=10)
		{
			write_adc(51);
		}
		else if(length_save>=80)
		{
			write_adc(255);
		}
		else
		{
			write_adc((u8)2.914f*length_save+21.857f);
		}
		

	}
}
void sonic_process()
{
	if(time_sonic>=100)
	{
		time_sonic=0;
		length=Sonic_receive();
		
		if(flag_C_F==0)
		{
			if(flag_sonic==1)
			{
				
				sonic_count++;
				length_save=length;
				flag_sonic=0;

				if(length_save>length_max)
					length_max=length_save;
				else if(length_save<length_min)
					length_min=length_save;
				length_arv=(length_min+length_max)*10/sonic_count;
			}
		}
		
		if(flag_C_F==1)
		{
			if((Sec%compare_time==0)&&(Sec_pro!=Sec))
			{
				length_save=length;
				sonic_count++;
				if(length_save>length_max)
					length_max=length_save;
				else if(length_save<length_min)
					length_min=length_save;
				length_arv=(length_min+length_max)*10/sonic_count;
								
				if(length_save>=(set_length_compare-5)&&(length_save<=(set_length_compare+5)))
				{
					count++;
				}
				else
				{
					count=0;
				}					

			}
		}
		
		
	}
}




void Key_process()
{
	if(time_key>=100)
	{
		time_key=0;
		Key_Scan3();
		
		if(trg==0x88)//4
		{
			flag=~flag;
			flag_set=0;
			flag_display=0;
			compare_time=set_time[local_time];
			set_length_compare=set_length;
		}
		else if(trg==0x84)//5
		{
			if(flag==1)
			{
				flag_set=~flag_set;
			}
			else if(flag==0)
			{
				if(flag_display==2)
				{
					G_D_Z=0;
				}
				flag_display++;
				
				flag_display%=3;
			}
			
		}
		else if(trg==0x48)//8
		{
			if(flag_display==1)
			{
				if(flag_C_F==1)
					count=0;
				flag_C_F=~flag_C_F;
				if(flag_C_F==1)
					flag_sonic=0;
			}
			else if(flag_display==2)
			{
				G_D_Z++;
				G_D_Z%=3;
			}
		}
		else if(trg==0x44)//9
		{
			if(flag_set==0)
			{
				local_time++;
				local_time%=5;
			}
			else if(flag_set==1)
			{
				set_length+=10;
				if(set_length==90)
					set_length=10;
			}
		}
	}
}


u8 ds1302_time;
void ds1302_process()
{
	if(ds1302_time>=100)
	{
		ds1302_time=0;
		Sec_pro=Sec;
		Read_time();
	}
}


void LED_process()
{
	if(flag_display==0)
		LED=~0X01;
	else if(flag_display==1)
		LED=~0X02;
	else if(flag_display==2)
		LED=~0X04;
	
	if(flag_C_F==0)
		LED&=~0X08;
	else
		LED|=0X08;
	

	
	if(count>=3)
		LED&=~0X10;
	else
		LED|=0X10;
	
	if(flag_light==1)
		LED&=~0X20;//0010 0000
	else
		LED|=0X20;
	Device_Ctri(0x80,LED);
}
void Smg_process()
{
	if(flag==0)//显示界面
	{
		if(flag_display==0)//时间显示
		{
			Smg_show[0]=Smg_code[Hour/10];
			Smg_show[1]=Smg_code[Hour%10];
			Smg_show[2]=0x40;
			Smg_show[3]=Smg_code[Min/10];
			Smg_show[4]=Smg_code[Min%10];
			Smg_show[5]=0x40;
			Smg_show[6]=Smg_code[Sec/10];
			Smg_show[7]=Smg_code[Sec%10];
		}
		else if(flag_display==1)//距离显示
		{
			Smg_show[0]=0x38;
			if(flag_C_F==0)
				Smg_show[1]=Smg_code[12];
			else
				Smg_show[1]=Smg_code[15];
			
			Smg_show[2]=Smg_code[count];
			Smg_show[3]=0x00;
			Smg_show[4]=0x00;
			Smg_show[5]=Smg_code[length_save>99 ? (length_save/100):16];
			Smg_show[6]=Smg_code[length_save>9 ? (length_save/10%10):16];
			Smg_show[7]=Smg_code[length_save%10];
		}
		else if(flag_display==2)//数据显示
		{
			Smg_show[0]=0x76;
			Smg_show[2]=0x00;
			Smg_show[3]=0x00;
			if(G_D_Z==0)//最大
			{
				Smg_show[1]=0x01;
				
				Smg_show[4]=0x00;
				Smg_show[5]=Smg_code[length_max>99 ? (length_max/100):16];
				Smg_show[6]=Smg_code[length_max>9 ? (length_max/10%10):16];
				Smg_show[7]=Smg_code[length_max%10];
				
			}
			else if(G_D_Z==1)//最小
			{
				Smg_show[1]=0x08;
				Smg_show[4]=0x00;
				Smg_show[5]=Smg_code[length_min>99 ? (length_min/100):16];
				Smg_show[6]=Smg_code[length_min>9 ? (length_min/10%10):16];
				Smg_show[7]=Smg_code[length_min%10];
			}
			else if(G_D_Z==2)//均值
			{
				Smg_show[1]=0x40;
				Smg_show[4]=Smg_code[length_arv>999 ? (length_arv/1000):16];
				Smg_show[5]=Smg_code[length_arv>99 ? (length_arv/100%10):16];
				Smg_show[6]=Smg_code[length_arv/10%10]|0x80;
				Smg_show[7]=Smg_code[length_arv%10];
			}

			
		}
	}
	else if(flag==1)//参数设计界面
	{
		Smg_show[0]=0x73;
		Smg_show[2]=0x00;
		Smg_show[3]=0x00;
		Smg_show[4]=0x00;
		Smg_show[5]=0x00;
		
		if(flag_set==0)
		{
			Smg_show[1]=Smg_code[1];
			Smg_show[6]=Smg_code[set_time[local_time]/10];
			Smg_show[7]=Smg_code[set_time[local_time]%10];
		}
		else if(flag_set==1)
		{
			Smg_show[1]=Smg_code[2];
			Smg_show[6]=Smg_code[set_length/10];
			Smg_show[7]=Smg_code[set_length%10];
			
		}
	}
}
void main()
{
	Device_Init();
	Timer2Init();
	Set_time(20,20,01);
	while(1)
	{
		Key_process();
		Smg_process();
		sonic_process();
		ds1302_process();
		adc_process();
	}
}


//-----------------------------------------------
//中断服务程序
void Timer_int() interrupt 12           //中断入口
{
	time_key++;
	time_sonic++;
	ds1302_time++;
	time_adc++;
	LED_process();
	Smg_display();
	
}

Device.c

#include "Device.h"

void Device_Ctri(u8 p2data,u8 p0date)
{
	P0=p0date;
	P2=P2&0X1F|p2data;
	P2&=0X1F;
}

void Device_Init()
{
	Device_Ctri(0x80,0xff);
	Device_Ctri(0xa0,0x00);
}

Timer.c

#include "Timer.h"

void Timer2Init(void)		//1毫秒@12.000MHz
{
	AUXR &= 0xFB;		//定时器时钟12T模式
	T2L = 0x18;		//设置定时初始值
	T2H = 0xFC;		//设置定时初始值
	AUXR |= 0x10;		//定时器2开始计时
	IE2 |= 0x04;                    //开定时器2中断
    EA = 1;
}

Smg.c

#include "Smg.h"

u8 code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};  
u8 Smg_code[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x00};
u8 Smg_show[8];

void Smg_display()
{
	static u8 i=0;
	Device_Ctri(0xc0,0x00);
	Device_Ctri(0xe0,~Smg_show[i]);
	Device_Ctri(0xc0,T_COM[i]);
	i++;
	i%=8;
}

ds1302.c

#include "Key.h"

u8 Key_Scan()
{
	u8 key_io=0xff;
	P3=0XF0;P4=0XFF;
	
	if(P44==0)
		key_io=0x70;
	if(P42==0)
		key_io=0xb0;
	
	P3=0X0F;P4=0X00;
	if(P33==0)
		key_io|=0x07;
	if(P32==0)
		key_io|=0x0b;
	return key_io;
}


u8 trg,con;
void Key_Scan3()
{
	u8 date=Key_Scan()^0xff;
	trg=date&(date^con);
	con=date;
	
}

Sonic.c

#include "Sonic.h"

void Delay12us()		//@12.000MHz
{
	unsigned char i;

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

void Sent_wave()
{
	u8 n=0;
	for(n=0;n<8;n++)
	{
		P10=1;
		Delay12us();
		P10=0;
		Delay12us();	
	}
}

u16 Sonic_receive()
{
	u16 temp=0;
	Sent_wave();
	TH1=0;TL1=0;
	TR1=1;
	while((P11==1)&&(TF1==0));
	TF1=0;
	TR1=0;
	temp=((TH1<<8)|TL1)*0.017;
	return temp;
}

iic.c

/*
  程序说明: IIC总线驱动程序
  软件环境: Keil uVision 4.10 
  硬件环境: CT107单片机综合实训平台 8051,12MHz
  日    期: 2011-8-9
*/

#include "iic.h"


#define DELAY_TIME 5

#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1

//总线引脚定义
sbit SDA = P2^1;  /* 数据线 */
sbit SCL = P2^0;  /* 时钟线 */

void IIC_Delay(unsigned char i)
{
    do{_nop_();}
    while(i--);        
}
//总线启动条件
void IIC_Start(void)
{
    SDA = 1;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 0;
    IIC_Delay(DELAY_TIME);
    SCL = 0;	
}

//总线停止条件
void IIC_Stop(void)
{
    SDA = 0;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 1;
    IIC_Delay(DELAY_TIME);
}

//发送应答
void IIC_SendAck(bit ackbit)
{
    SCL = 0;
    SDA = ackbit;  					// 0:应答,1:非应答
    IIC_Delay(DELAY_TIME);
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SCL = 0; 
    SDA = 1;
    IIC_Delay(DELAY_TIME);
}

//等待应答
bit IIC_WaitAck(void)
{
    bit ackbit;
	
    SCL  = 1;
    IIC_Delay(DELAY_TIME);
    ackbit = SDA;
    SCL = 0;
    IIC_Delay(DELAY_TIME);
    return ackbit;
}

//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
    unsigned char i;

    for(i=0; i<8; i++)
    {
        SCL  = 0;
        IIC_Delay(DELAY_TIME);
        if(byt & 0x80) SDA  = 1;
        else SDA  = 0;
        IIC_Delay(DELAY_TIME);
        SCL = 1;
        byt <<= 1;
        IIC_Delay(DELAY_TIME);
    }
    SCL  = 0;  
}

//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
    unsigned char i, da;
    for(i=0; i<8; i++)
    {   
    	SCL = 1;
	IIC_Delay(DELAY_TIME);
	da <<= 1;
	if(SDA) da |= 1;
	SCL = 0;
	IIC_Delay(DELAY_TIME);
    }
    return da;    
}

u8 Read_adc()
{
	u8 temp=0;
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x41);
	IIC_WaitAck();
	
	IIC_Start();
	IIC_SendByte(0x91);
	IIC_WaitAck();
	temp=IIC_RecByte();
	IIC_SendAck(1);
	IIC_Stop();
	
	return temp;	
}

void write_adc(u8 date)
{
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x40);
	IIC_WaitAck();
	
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(date);
	IIC_WaitAck();
	IIC_Stop();	
}