蓝桥杯十二届省赛

 这是我备赛时写的代码,还挺好,功能全部实现。有意者私

main.c

#include "Define.h"

u8 flag=1;
u8 time_key,test;
bit flag_V_G;
u8 time_adc;
u16 U16_V,U16_V_pro;
u16 frequence_pro;
bit flag_LED;

u16 time_key7;
void Key_pross()
{
	if(time_key>=10)
	{
		time_key=0;
		Key_scan();
		
		if(trg&0x08)//4
		{
			flag++;
			flag%=3;
		}
		else if(trg&0x04)//5
			flag_V_G=~flag_V_G;
		else if(trg&0x02)//6
			U16_V_pro=U16_V;	
	
		if(con&0x01)
			time_key7++;
		else if(con==0x00&&trg==0x00)
		{
			if(time_key7>=100)
			{
				flag_LED=~flag_LED;
				time_key7=0;
			}
			else if(time_key7!=0)
			{
				time_key7=0;
				frequence_pro=frequence;
			}
		}
		
	}
}

u8 LED=0XFF;
void LED_prosss()
{
	if(flag_LED==0)
	{
		if(U16_V>U16_V_pro)
			LED&=~0x01;
		else
			LED|=0X01;
		if(frequence>frequence_pro)
			LED&=~0x02;
		else
			LED|=0X02;
		
		if(flag==0)
			LED&=~0x04;
		else
			LED|=0X04;
		
		if(flag==1)
			LED&=~0x08;
		else
			LED|=0X08;
		
		if(flag==2)
			LED&=~0x10;
		else
			LED|=0X10;
		
		Device_Ctri(0x80,LED);
	}
	else
	Device_Ctri(0x80,0xff);

	
	
}
void ADC_pross()
{
	if(time_adc>=100)
	{
		time_adc=0;
		U16_V=ADC_read(0x43)*100/51;
		
	}
}
void Smg_pross()
{
	
	if(flag==0)//频率显示
	{
		Smg_show[0]=Smg_code[15];
		Smg_show[1]=0x00;
		Smg_show[2]=0x00;
		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/10%10];
		Smg_show[7]=Smg_code[frequence%10];
	}
	 if(flag==1)//周期显示
	{
		
		Smg_show[0]=0x37;
		Smg_show[1]=0x00;
		Smg_show[2]=0x00;
		Smg_show[3]=Smg_code[zhouqi>9999 ? zhouqi/10000:16];
		Smg_show[4]=Smg_code[zhouqi>999 ? zhouqi/1000%10:16];
		Smg_show[5]=Smg_code[zhouqi>99 ? zhouqi/100%10:16];
		Smg_show[6]=Smg_code[zhouqi/10%10];
		Smg_show[7]=Smg_code[zhouqi%10];
	}
	else if(flag==2)//电压-光敏
	{
		Smg_show[0]=0x3E;
		Smg_show[1]=0x40;
		if(flag_V_G==0)
			Smg_show[2]=Smg_code[1];
		else
			Smg_show[2]=Smg_code[3];
		Smg_show[3]=0x00;
		Smg_show[4]=0x00;
		Smg_show[5]=Smg_code[U16_V/100]|0x80;
		Smg_show[6]=Smg_code[U16_V/10%10];
		Smg_show[7]=Smg_code[U16_V%10];
	}
}
void main()
{
	Device_Init();	
	Timer2Init();
	NE555_Init();
	
	while(1)
	{
		ADC_pross();
		Key_pross();
		Smg_pross();		
	}
	
	
}

//-----------------------------------------------
//中断服务1程序里面放不耗时间的函数,LED灯也放这
//中断服务程序
void Timer_2int() interrupt 12           //中断入口
{
	time_key++;
	time_adc++;
	
	NE555_process();
	LED_prosss();
    Smg_display();
}

Smg.c

#include "Smg.h"

   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,

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

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;
}


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;
}

Key.c

#include "Key.h"

//独立按键

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







Ne555.c

#include "NE555.h"

void NE555_Init()
{
	TMOD|=0X05;
	TL0=0x00;
	TH0=0x00;
	TR0=1;
}

u16 time_NE555;
u16 frequence;
u16 zhouqi;

void NE555_process()
{
	time_NE555++;
	if(time_NE555>=1000)
	{
		time_NE555=0;
		frequence=((TH0<<8)|TL0);
		TL0=0x00;
		TH0=0x00;
		zhouqi=(u16)(1000000/frequence);
	}
}




/*
//NE555频率测量
u16 cnt_ne555;
u16 freq_ne555;
void vNE555_Process()
{
	cnt_ne555++;
	if(cnt_ne555>=100)		//精确的100ms
	{
		cnt_ne555=0;
		freq_ne555 = ((TH0<<8)|TL0)*10;
		TL0 = 0;
		TH0 = 0;
	}
}
*/

iic.c

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

#define DELAY_TIME	5
#include "iic.h"
sbit scl=P2^0;
sbit sda=P2^1;
void Delay5ms()		//@12.000MHz
{
	unsigned char i, j;

	i = 59;
	j = 90;
	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);
}
//ADC,DAC的功能		他的读是读出上一次的数,所以要空读一次,才能得到想要的值
//电压采集	add=0x43
//光敏采集	add=0x41
u8 ADC_read(u8 add)
{
	u8 temp=0;
	
	I2CStart();
	I2CSendByte(0x90);
	I2CWaitAck();
	I2CSendByte(add);
	I2CWaitAck();
	
	I2CStart();
	I2CSendByte(0x91);
	I2CWaitAck();
	temp=I2CReceiveByte();
	I2CSendAck(1);
	I2CStop();
	
	return temp;
}