蓝桥杯十三届国赛编程题

目录

main.c

Device.h

Key.h

Ne555.h

iic.h

Sonic.h



main.c

#include "Define.h"
u8 flag,flag_s;
bit flag_f,flag_l;
u8 time_key,test;
u16 time_ne555;
u16 frequence;
u8 time_adc;
u8 shidu,shidu_s=40;
u8 time_sonic;
u16 distance;
u16 distance_s=60,frequence_s=9000;
u8 LED=0XFF,warn=0x00,LED_=0XFF;
u8 time_LED1,time_LED2,time_LED3;
u8 count;
u8 eep;
u16 time_key7;
void LED_gc()
{
	if(distance>distance_s)
	{
		warn=0x10;
		LED_&=~(0x20);
	}
	else
	{
		warn=0x00;
		LED_|=0x20;
	}
	
	if(flag==0)
	{
		time_LED1++;
		if(time_LED1<100)
			LED_&=~(0X01);
		else if(time_LED1<200)
			LED_|=0X01;
		else if(time_LED1>200)
			time_LED1=0;
	}
	else 
		LED_|=0X01;
	
	if(flag==1)
	{
		time_LED2++;
		if(time_LED2<100)
			LED_&=~(0X02);
		else if(time_LED2<200)
			LED_|=0X02;
		else if(time_LED2>200)
			time_LED2=0;
	}
	else
		LED_|=0X02;
	
	if(flag==2)
	{
		time_LED3++;
		if(time_LED3<100)
			LED_&=~(0X04);
		else if(time_LED3<200)
			LED_|=0X04;
		else if(time_LED3>200)
			time_LED3=0;
	}
	else
		LED_|=0X04;
	
	Device_ctri(0xa0,warn);
	
	if(frequence_s<frequence)
		LED_&=~(0x08);
	else
		LED_|=0x08;
	
	if(shidu_s<shidu)
		LED_&=~(0x10);
	else
		LED_|=0x10;
	
	Device_ctri(0x80,LED_);
	
}

void Sonic_gc()
{
	if(time_sonic>=100)
	{
		time_sonic=0;
		distance=Sonic_get();
	}
}

void adc_gc()
{
	if(time_adc>=100)
	{
		time_adc=0;
		shidu=adc_read()*100/255;
		if(distance>distance_s)
		{
			count++;
			eeprom_write(0,count);
		}
	}
}

void NE555_gc()
{
	time_ne555++;
	if(time_ne555>=1000)
	{
		time_ne555=0;
		frequence=TH0<<8|TL0;
		TL0=0X00;
		TH0=0X00;
	}
}

void Key_gc()
{
	if(time_key>=10)
	{
		time_key=0;
		Key_scan();
		
		if(trg&0x08)
		{
			flag++;
			flag%=4;
			
		}
		else if(trg&0x04)
		{
			if(flag==3)
			{
				flag_s++;
				flag_s%=3;
			}
		}
		else if(trg&0x02)//6
		{
			if(flag==2)
				flag_l=~flag_l;
			else if(flag==3)
			{
				if(flag_s==0)
				{
					frequence_s+=500;
					if(frequence_s>12000)
						frequence_s=1000;
				}
				else if(flag_s==1)
				{
					shidu_s+=10;
					if(shidu_s>60)
						shidu_s=10;
				}
				else if(flag_s==2)
				{
					distance_s+=10;
					if(distance_s>120)
						distance_s=10;
				}
			}
		}
		
		if(con&0x01)
			time_key7++;
		else if(con==0x00&&trg==0x00)
		{
			if(time_key7>=100)
			{
				count=0;
			}
			else if(time_key7!=0)
			{
				if(flag==0)
				flag_f=~flag_f;	
			else if(flag==3)
			{
				if(flag_s==0)
				{
					frequence_s-=500;
					if(frequence_s<1000)
						frequence_s=12000;					
				}
				else if(flag_s==1)
				{
					shidu_s-=10;
					if(shidu_s<10)
						shidu_s=60;
				}
				else if(flag_s==2)
				{
					distance_s-=10;
					if(distance_s<10)
						distance_s=120;
				}
				
			}
			}
			time_key7=0;
		}
		
	}

	
}
void Smg_gc()
{
	if(flag==0)
	{
		if(flag_f==0)
		{
			Smg_show[0]=Smg_code[15];			
			Smg_show[1]=Smg_code[16];
			Smg_show[2]=Smg_code[16];
			Smg_show[3]=Smg_code[frequence > 9999 ? frequence/10000 : 16];
			Smg_show[4]=Smg_code[frequence > 999  ? frequence/1000%10 : 16];
			Smg_show[5]=Smg_code[frequence > 99	  ?	frequence/100%10 : 16];
			Smg_show[6]=Smg_code[frequence > 9 	  ? frequence/10%10 : 16];
			Smg_show[7]=Smg_code[frequence%10];
		}
		else if(flag_f==1)
		{
			Smg_show[0]=Smg_code[15];
			Smg_show[1]=Smg_code[16];
			Smg_show[2]=Smg_code[16];
			Smg_show[3]=Smg_code[16];
			Smg_show[4]=Smg_code[16];
			Smg_show[5]=Smg_code[frequence > 9999 ? frequence/10000 : 16];
			Smg_show[6]=Smg_code[frequence/1000%10]|0x80;
			Smg_show[7]=Smg_code[frequence/100%10];
		}
	}
	else if(flag==1)
	{
		Smg_show[0]=0x76;
		Smg_show[1]=Smg_code[16];
		Smg_show[2]=Smg_code[16];
		Smg_show[3]=Smg_code[16];
		Smg_show[4]=Smg_code[16];
		Smg_show[5]=Smg_code[shidu/100];
		Smg_show[6]=Smg_code[shidu/10%10];
		Smg_show[7]=Smg_code[shidu%10];
	}
	else if(flag==2)
	{
		if(flag_l==0)
		{
			Smg_show[0]=0x77;
			Smg_show[1]=Smg_code[16];
			Smg_show[2]=Smg_code[16];
			Smg_show[3]=Smg_code[16];
			Smg_show[4]=Smg_code[16];
			Smg_show[5]=Smg_code[distance > 99 ? distance/100 :16];
			Smg_show[6]=Smg_code[distance > 9  ? distance/10%10 :16];
			Smg_show[7]=Smg_code[distance%10];
		}
		else if(flag_l==1)
		{
			Smg_show[0]=0x77;
			Smg_show[1]=Smg_code[16];
			Smg_show[2]=Smg_code[16];
			Smg_show[3]=Smg_code[16];
			Smg_show[4]=Smg_code[16];
			Smg_show[5]=Smg_code[distance > 99 ? distance/100 :0]|0x80;
			Smg_show[6]=Smg_code[distance/10%10];
			Smg_show[7]=Smg_code[distance%10];
		}
	}
	
	else if(flag==3)
	{
		if(flag_s==0)//平率
		{
			Smg_show[0]=0x73;
			Smg_show[1]=Smg_code[1];
			Smg_show[2]=Smg_code[16];
			Smg_show[3]=Smg_code[16];
			Smg_show[4]=Smg_code[16];
			Smg_show[5]=Smg_code[frequence_s > 9999 ? frequence_s/10000 : 16];
			Smg_show[6]=Smg_code[frequence_s/1000%10]|0x80;
			Smg_show[7]=Smg_code[frequence_s/100%10];
		}
		else if(flag_s==1)//湿度
		{
			Smg_show[0]=0x73;
			Smg_show[1]=Smg_code[2];
			Smg_show[2]=Smg_code[16];
			Smg_show[3]=Smg_code[16];
			Smg_show[4]=Smg_code[16];
			Smg_show[5]=Smg_code[16];
			Smg_show[6]=Smg_code[shidu_s/10];
			Smg_show[7]=Smg_code[shidu_s%10];
		}
		else if(flag_s==2)//距离
		{
			Smg_show[0]=0x73;
			Smg_show[1]=Smg_code[3];
			Smg_show[2]=Smg_code[16];
			Smg_show[3]=Smg_code[16];
			Smg_show[4]=Smg_code[16];
			Smg_show[5]=Smg_code[16];
			Smg_show[6]=Smg_code[distance_s > 99 ? distance_s/100 :0]|0x80;
			Smg_show[7]=Smg_code[distance_s > 9  ? distance_s/10%10 :16];
		}
	}
}

void main()
{
	Device_init();
	NE555_init();
	Timer2_Init();
	if(eeprom_read(1)==0)
		eep=eeprom_read(0);
	else
	{
		eeprom_write(0,0);
		eeprom_write(1,0);
	}

	
	while(1)
	{
		Key_gc();
		adc_gc();
		Smg_gc();
		Sonic_gc();
	}
}

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

Device.h

#include "Device.h"

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

void Device_init()
{
	Device_ctri(0x80,0xff);
	Device_ctri(0xa0,0x00);
}

Smg.h

#include "Smg.h"

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

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

Timer.h

#include "Timer.h"

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

Key.h

#include "Key.h"

u8 trg,con;
void Key_scan()
{
	u8 date=P3^0XFF;
	trg=date&(date^con);
	con=date;
	
}

Ne555.h

#include "NE555.h"

void NE555_init()
{
	TMOD|=0X05;
	TL0=0X00;
	TH0=0X00;
	TR0=1;
}

iic.h

/*	#   I2C代码片段说明
	1. 	本文件夹中提供的驱动代码供参赛选手完成程序设计参考。
	2. 	参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题
		中对单片机时钟频率的要求,进行代码调试和修改。
*/
#include "iic.h"
#define DELAY_TIME	5
sbit scl=P2^0;
sbit sda=P2^1;
//

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

	i = 117;
	j = 184;
	do
	{
		while (--j);
	} while (--i);
}

static void I2C_Delay(unsigned char n)
{
    do
    {
        _nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();		
    }
    while(n--);      	
}

//
void I2CStart(void)
{
    sda = 1;
    scl = 1;
	I2C_Delay(DELAY_TIME);
    sda = 0;
	I2C_Delay(DELAY_TIME);
    scl = 0;    
}

//
void I2CStop(void)
{
    sda = 0;
    scl = 1;
	I2C_Delay(DELAY_TIME);
    sda = 1;
	I2C_Delay(DELAY_TIME);
}

//
void I2CSendByte(unsigned char byt)
{
    unsigned char i;
	
    for(i=0; i<8; i++){
        scl = 0;
		I2C_Delay(DELAY_TIME);
        if(byt & 0x80){
            sda = 1;
        }
        else{
            sda = 0;
        }
		I2C_Delay(DELAY_TIME);
        scl = 1;
        byt <<= 1;
		I2C_Delay(DELAY_TIME);
    }
	
    scl = 0;  
}

//
unsigned char I2CReceiveByte(void)
{
	unsigned char da;
	unsigned char i;
	for(i=0;i<8;i++){   
		scl = 1;
		I2C_Delay(DELAY_TIME);
		da <<= 1;
		if(sda) 
			da |= 0x01;
		scl = 0;
		I2C_Delay(DELAY_TIME);
	}
	return da;    
}

//
unsigned char I2CWaitAck(void)
{
	unsigned char ackbit;
	
    scl = 1;
	I2C_Delay(DELAY_TIME);
    ackbit = sda; 
    scl = 0;
	I2C_Delay(DELAY_TIME);
	
	return ackbit;
}

//
void I2CSendAck(unsigned char ackbit)
{
    scl = 0;
    sda = ackbit; 
	I2C_Delay(DELAY_TIME);
    scl = 1;
	I2C_Delay(DELAY_TIME);
    scl = 0; 
	sda = 1;
	I2C_Delay(DELAY_TIME);
}

u8 adc_read()
{
	u8 temp;
	I2CStart();
	I2CSendByte(0x90);
	I2CWaitAck();
	I2CSendByte(0x43);
	I2CWaitAck();
	
	I2CStart();
	I2CSendByte(0x91);
	I2CWaitAck();
	temp=I2CReceiveByte();
	I2CSendAck(1);
	I2CStop();
	
	return temp;
}


void eeprom_write(u8 add,u8 date)
{
	I2CStart();
	I2CSendByte(0xa0);
	I2CWaitAck();
	I2CSendByte(add);
	I2CWaitAck();
	I2CSendByte(date);
	I2CWaitAck();
	I2CStop();	
	Delay10ms();
}

u8 eeprom_read(u8 add)
{
	u8 temp;
	
	I2CStart();
	I2CSendByte(0xa0);
	I2CWaitAck();
	I2CSendByte(add);
	I2CWaitAck();
	
	I2CStart();
	I2CSendByte(0xa1);
	I2CWaitAck();
	temp=I2CReceiveByte();
	I2CSendAck(1);
	I2CStop();
	return temp;
}

Sonic.h

#include "Sonic.h"

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

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

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


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

进一步学习嵌入式,私我!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值