蓝桥杯比赛

#include <STC15F2K60S2.H>
#include <timer0.h>
#include <nixie.h>
#include <key.h>
#include <onewire.h>
#include <I2C.h>
#include <sound.h>
#include <ds1302.h>

unsigned int PL,A=15;        //ÓÃÓÚ½çÃæÇл»µÄ±äÁ¿
unsigned int AD;             //ÓÃÓÚÊýģת»»µÄÏÔʾ±äÁ¿
unsigned int sound;          //ÓÃÓÚ³¬Éù²¨»ñÈ¡¾àÀëµÄ±äÁ¿£¬ÓÃÓÚÆô¶¯·äÃùÆ÷±¨¾¯£¬½çÃæÇл»¼°¸´Î»£¬³¬Éù²¨ÏÔʾ
unsigned int PLnow,PLlast;   //ÓÃÓÚ³¬Éù²¨½çÃæ¼°ÉèÖÃʱÖÓ½çÃæÇл»ºóµÄ½çÃ渴λ
unsigned char sound_flag1;   //³¬Éù²¨ÊýÂë¹ÜÏÔʾÑÓ³Ù±ê־λ
unsigned int Counter;        //ÓÃÓÚ·äÃùÆ÷µÄPWMÇý¶¯¼ÆÊý
unsigned char BUZZ;          //ÓÃÓÚ·äÃùÆ÷µÄÆô¶¯¼°¹Ø±Õ
unsigned char longkey_flag;  //°´¼ü³¤°´¿ªÆô¼Æʱ±ê־λ
unsigned char longkey1_flag; //°´¼ü³¤°´Ö´Ðа´¼ü³¤°´¹¦Äܱê־λ
unsigned int mode=0;         //ÉèÖÃʱ¼äģʽÇл»
unsigned char th;            //³¬Éù²¨¾àÀëãÐÖµ
int T;                       //ÓÃÓÚ¸ÐεÄÏÔʾ±äÁ¿

bit temper_flag = 0,AD_flag = 0,sound_flag = 0,time_flag = 0;  //·ÀÖ¹³åÍ»£¬¹¦Äܺ¯ÊýÖ´Ðбê־λ

void show_T()                //ÊýÂë¹ÜζÈÏÔʾº¯Êý
{
			if(T<0)				         //Èç¹ûζÈСÓÚ0
		{
			buf[5]=10;             //ÏÔʾ¸ººÅ
			T=-T;			             //½«Î¶ȱäΪÕýÊý
		}
		buf[0]=11;
		buf[1]=3;
		buf[6]=T/10;
		buf[7]=T%10; 
}

void show_AD()               //ÊýÂë¹ÜADÏÔʾº¯Êý
{ 
	buf[0]=11;
	buf[1]=4;
	buf[2]=buf[3]=buf[4]=12;		
	buf[5] = AD>=100?AD/100:12;
	buf[6] = AD>=10?AD/10%10:12;
	buf[7] = AD%10;	
}

void show_sound()             //ÊýÂë¹Ü³¬Éù²¨¾àÀëÏÔʾº¯Êý
{
	buf[0]=11;
	buf[1]=5;
	buf[2]=buf[3]=buf[4]=12;
	buf[5] = sound>=100?sound/100:12;
	buf[6] = sound>=10?sound/10%10:12;
	buf[7] = sound%10;	
}

void show_date()              //ÊýÂë¹ÜÈÕÆÚÏÔʾº¯Êý
{
	buf[0]=2;
	buf[1]=0;
	buf[2]=DS1302_Time[0]/10;
	buf[3]=DS1302_Time[0]%10;
	buf[4]=DS1302_Time[1]/10;
	buf[5]=DS1302_Time[1]%10;
	buf[6]=DS1302_Time[2]/10;
	buf[7]=DS1302_Time[2]%10;
}

void show_time()              //ÊýÂë¹Üʱ·ÖÃëÏÔʾº¯Êý
{
	buf[0]=DS1302_Time[3]/10;
	buf[1]=DS1302_Time[3]%10;
	buf[2]=10;
	buf[3]=DS1302_Time[4]/10;
	buf[4]=DS1302_Time[4]%10;
	buf[5]=10;
	buf[6]=DS1302_Time[5]/10;
	buf[7]=DS1302_Time[5]%10;
}


void show_th()                 //ÊýÂë¹Ü³¬Éù²¨ãÐÖµÏÔʾº¯Êý
{
	buf[0]=11;
	buf[1]=6;
	buf[2]=buf[3]=buf[4]=12;
	buf[5] = th>=100?th/100:12;
	buf[6] = th>=10?th/10%10:12;
	buf[7] = th%10;	
}

void main()
{
	P2 = ((P2&0x1f)|0xA0);    //¹Ø±Õ·äÃùÆ÷
  P0 = 0x00;  
  P2 &= 0x1f;
	
	P2 = ((P2&0x1f)|0x80);    //¹Ø±ÕËùÓÐLED
  P0 = 0xff; 
  P2 &= 0x1f;
	
	Timer0Init();             //³õʼ»¯¶¨Ê±Æ÷0
	init_pcf8591();           //³õʼ»¯AD
	DS1302_SetTime();         //½«Èí¼þÊý×éÖеÄʱ¼äĬÈÏÖµ´æÈëDS1302оƬ
	th=AT24C02_ReadByte(1);   //´ÓAT24C02ÖжÁÈ¡³¬Éù²¨¾àÀëãÐÖµ
	
	while(1)
	{
		//°´¼ü¹¦ÄÜÖ´ÐУ¬ËÉÊÖ¼ì²â
		if(temp==0){if(A!=1)A=1;else A=15;close_nixie();PL=50;temp=0xff;}   //ËÉÊÖÏÔʾÈÕÆÚ
		if(temp==1){if(A!=2)A=2;else A=15;close_nixie();PL=150;temp=0xff;}  //ËÉÊÖÏÔʾʱ·ÖÃë
		if(temp==2){if(A!=3)A=3;else A=15;close_nixie();PL=250;temp=0xff;}  //ËÉÊÖÏÔʾζÈ
		if(temp==3){if(A!=4)A=4;else A=15;close_nixie();PL=350;temp=0xff;}  //ËÉÊÖÏÔʾAD
		if(temp==4){if(A!=5)A=5;else A=15;close_nixie();PL=450;temp=0xff;}  //ËÉÊÖÏÔʾ³¬Éù²¨¾àÀë
		if(temp==5){if(A!=6)A=6;else A=15;close_nixie();PL=550;temp=0xff;}  //ËÉÊÖÏÔʾãÐÖµµ÷½Ú
		if(temp==6){th++;if(th>=70)th=70;longkey_flag=0;longkey1_flag=0;AT24C02_WriteByte(1,th);temp=0xff;}  //Ôö¼Ó³¬Éù²¨ãÐÖµ
		if(temp==7){th--;if(th<=1)th=1;longkey_flag=0;AT24C02_WriteByte(1,th);longkey1_flag=0;temp=0xff;}    //¼õÉÙ³¬Éù²¨ãÐÖµ 
		
		if(temp==8)     //Çл»Ä£Ê½£¬mode=0£º³¬Éù²¨¡¢Ê±ÖÓÖ´ÐÐģʽ£»mode=1£ºÉèÖÃʱ¼äģʽ
		{
			if(PL!=700)    //°´¼ü°´Ï£¬´¢´æ½çÃæÇл»Ç°½çÃæ¶ÔÓ¦µÄÊýÖµ£¬²¢ÔÚ´¢´æºóÇл»½çÃæ
			{
				PLlast=PL;
			  PL=700;
			}
			else
			{
				PL=PLlast;   //°´¼üÔٴΰ´Ï£¬»Øµ½Çл»Ç°µÄ½çÃæ
			}
			
			if(mode==0){mode=1;TimeSetSelect=0;}    //Çл»Ä£Ê½1
			else{mode=0;DS1302_SetTime();}          //Çл»Ä£Ê½0
			temp=0xff;
		}
		
		switch(mode)                              //ģʽִÐÐ
		{
			case 0:                                 //ģʽ0
			{
				if(sound_flag==1)                     //³¬Éù²¨¹¦Äܺ¯ÊýÖ´ÐÐ
		    {
			    sound_flag = 0;                     //³õʼ»¯³¬Éù²¨Ö´Ðбê־λ
   		    sound=getUltraDistance();           //»ñÈ¡¾àÀë
			
			    if(sound<=th)                       //½ü¾àÀë·äÃùÆ÷¾¯¸æ
			    {
				    if(BUZZ==1){P2=((P2&0x1f)|0xA0);P0=0x40;P2&=0x1f;}      //¿ªÆô·äÃùÆ÷£¬ÓÉpwmÇý¶¯
				    else{P2=((P2&0x1f)|0xA0);P0=0x00;P2&=0x1f;}	            //¹Ø±Õ·äÃùÆ÷£¬ÓÉpwmÇý¶¯
			    }
			    else
			    {
					  P2 = ((P2&0x1f)|0xA0);            //¹Ø±Õ·äÃùÆ÷£¬Óɳ¬Éù²¨¾àÀëÇý¶¯
            P0 = 0x00;  
            P2 &= 0x1f;
			    }
					
			    if(sound<198)                       //³¬Éù²¨ÊµÊ±ÏÔʾ²â¾àʵÏÖ£¬µ±¾àÀëСÓÚ198ʱ
			    {
				    if(PL<460||PL>500&&PL<700)        //Èç¹û²»ÔÚ³¬Éù²¨ÏÔʾ½çÃæ
				    {PLlast=PL;}                      //Ôò½«´ËʱËùÔÚ½çÃæµÄÊýÖµ¼Ç¼
				    PL=480;                           //Çл»µ½³¬Éù²¨½çÃæ
				    PLnow=PL;                         //¸øÅжÏÔÚСÓÚ198Ö´ÐйýÒÔÉÏÓï¾äµÄ±äÁ¿¸³Öµ
			    }
			    else
			    {
				    if(PLnow==480)                    //ÔÚ¾àÀëÔٴλص½200£¬ÅжÏÊÇ·ñÖ´ÐйýÉÏÃæ½çÃæת»¯Óï¾ä
				    {
				     	PL=PLlast;                      //Ö´Ðйýת»¯£¬½«×ª»¯Öµ¸´Î»£¬·µ»Ø±»×ª»¯Ç°µÄ½çÃæ
				  	  PLnow=0;                        //½«ÅжÏÔÚСÓÚ198Ö´ÐйýÒÔÉÏÓï¾äµÄ±äÁ¿³õʼ»¯
				    }
			    }
        }
		
					if(time_flag==1&&temper_flag==0&&sound_flag==0)    //·ÀÖ¹³åÍ»£¬ÔÚÖ´ÐÐζȣ¬³¬Éù²¨ºó²ÅÖ´ÐÐ
				 {
						time_flag = 0;
						DS1302_ReadTime();                  //ʱ¼ä¶ÁÈ¡¹¦Äܺ¯Êý£¬½«ÊýÖµ´æÈëÆäÎļþϵÄÈ«¾ÖÊý×éÖÐ
					}
			
					if(temper_flag==1)                    //¶ÁÈ¡Ö´ÐÐζÈÅÐ¶Ï  
				 {
						 temper_flag = 0;
						 T = rd_temperature();              //¶ÁÈ¡ÎÂ¶È         
				 }
				 break;
		  }
			
	    case 1:                                 //ģʽ1
			{
				TimeSet();                            //ÉèÖÃʱ¼ä¹¦Äܺ¯Êý
				break;
			}
		}
  }	
}

void timer0_zhongduan(void)		interrupt 1     //16λ×Ô¶¯ÖØ×°£¬1Tģʽ£¬Ã¿2ms×Ô¶¯ÖØ×°
{
		static unsigned int c1,c2,c3,c4,c5,c6,c7; //Ö´Ðмä¸ôʱ¼ä±êÖ¾
		
	  //·äÃùÆ÷PWMÇý¶¯
		Counter++;       //¼ÆÊýÖµ×ÔÔö¼Ó
		Counter%=100;	   //¼ÆÊýÖµ±ä»¯·¶Î§ÏÞÖÆÔÚ0~99
		if(Counter<20)	 //¼ÆÊýֵСÓڱȽÏÖµ
		{
			BUZZ=1;		     //Êä³ö1
		}
		else				     //¼ÆÊýÖµ´óÓڱȽÏÖµ
		{
			BUZZ=0;	       //Êä³ö0
		}
		
		display();	     //ÊýÂë¹ÜÏÔʾº¯Êý
		keyloop();       //¼üÅÌÇý¶¯º¯Êý
		keyfun();        //¼üÅ̹¦Äܺ¯Êý£¨¿ÉÍØÕ¹ÐÂÓ÷¨£©
		
		c1++;
		if(c1>=25)       //ÿ25*2ms=50msÖ´ÐÐÒ»´Î
		{
				if(AD_flag==1&&time_flag==0&&temper_flag==0&&sound_flag==0)    //·ÀÖ¹³åÍ»£¬ÔÚʱ¼ä¡¢Î¶ȡ¢³¬Éù²¨¹¦Äܺ¯ÊýÖ´Ðкó²ÅÖ´ÐÐ
				{
					AD_flag = 0;
					AD = adc_pcf8591();                //¶ÁÈ¡Ä£ÊýÖµ
				} 
				c1=0;
		}
	
		c2++;
		if(c2>=10)       //ÿ10*2ms=20msÖ´ÐÐÒ»´Î
		{ 
			//½çÃæÇл»º¯Êý£¬Ã¿Ò»¸ö½çÃ涼¶ÔÓ¦100¸öPLµÄÖµ
			//0-100ÏÔʾζȣ»100-200ÏÔʾAD£»200-300ÏÔʾ³¬Éù²¨¾àÀ룻300-400ÏÔʾÈÕÆÚ£»400-500ÏÔʾʱ·ÖÃ룻500-600ÏÔʾ³¬Éù²¨¾àÀëãÐÖµ£»700ÏÔʾʱÖÓÉèÖã»
			if(PL<100){show_date();if(A==15)PL++;}               //½«Î¶ÈÊýÖµ´æÈëÊýÂë¹ÜÏÔʾÊý×é         
			if(PL==100)close_nixie();
			if(PL<200&&PL>=100){show_time();if(A==15)PL++;}     //½«ADÊýÖµ´æÈëÊýÂë¹ÜÏÔʾÊý×é     
			if(PL==200)close_nixie();
			if(PL<300&&PL>=200){show_T();if(A==15)PL++;}     //½«³¬Éù²¨¾àÀë´æÈëÊýÂë¹ÜÏÔʾÊý×飬ÀûÓó¬Éù²¨ÊýÂë¹ÜÏÔʾÑÓ³Ù±ê־루sound_flag1£©½µµÍ³¬Éù²¨ÊýÖµÔÚÊýÂë¹ÜÉϸıäµÄƵÂÊ     
			if(PL==300)close_nixie();
			if(PL<400&&PL>=300){show_AD();if(A==15)PL++;}   //½«ÈÕÆÚÊýÖµ´æÈëÊýÂë¹ÜÏÔʾÊý×é   
			if(PL==400)close_nixie();
			if(PL<500&&PL>=400){if(sound_flag1==1){show_sound();sound_flag1=0;}if(A==15)PL++;}   //½«Ê±·ÖÃëÊýÖµ´æÈëÊýÂë¹ÜÏÔʾÊý×é
			if(PL==500)close_nixie();
			if(PL<600&&PL>=500){show_th();if(A==15)PL++;}     //½«³¬Éù²¨ãÐÖµÊýÖµ´æÈëÊýÂë¹ÜÏÔʾÊý×é
			if(PL==600){close_nixie();PL=0;}                  //½«PLÖµ¸´Î»£¬ÊµÏÖÑ­»·ÏÔʾ
			c2=0;
		}
		
		c3++;
		if(c3>=50)      //ÿ50*2ms=100msÖ´ÐÐÒ»´Î
		{
			temper_flag = 1;         //ζȳÌÐòÖ´Ðбê־λ     
			sound_flag1 = 1;         //³¬Éù²¨ÊýÂë¹ÜÏÔʾÑÓ³Ù±ê־λ 
			c3=0; 
		}
		
		c4++;
		if(c4>=30)      //ÿ30*2ms=60msÖ´ÐÐÒ»´Î
		{ 
			time_flag = 1;           //ʱ¼ä³ÌÐòÖ´Ðбê־λ 
			AD_flag = 1;             //AD³ÌÐòÖ´Ðбê־λ 
			sound_flag = 1;          //³¬Éù²¨³ÌÐòÖ´Ðбê־λ
			c4=0;
		}
		
		c5++;
		if(c5>=250)     //ÿ250*2ms=500msÖ´ÐÐÒ»´Î
		{
			TimeSetFlashFlag=(!TimeSetFlashFlag);      //ÉèÖÃʱ¼äÉÁ˸״̬Çл»±ê־λ
			c5=0;
		}	
		
		if(longkey_flag)           //°´¼ü°´ÏÂÆô¶¯¼Æʱ±ê־루³¤°´¹¦ÄÜ£©
		{
			c6++;
			if(c6>=1000)  //ÿ1000*2ms=2000ms=2sÖ´ÐÐÒ»´Î         
			{
					longkey1_flag=1;     //³¤°´¹¦ÄÜÖ´Ðбê־λ
					c6=0;
			}
		}
		
	  c7++;
	  if(c7>=100)     //ÿ250*2ms=500msÖ´ÐÐÒ»´Î
	  {
			switch(keyvalue)          //°´¼üÊýÖµ¶ÔÓ¦¹¦ÄÜÖ´ÐÐÅжϣ¬°´Ï¼´¼ì²â
			{
				case 6:                 //°´ÏµÚ6¼ü²»ËÉÊÖ
				{  
           longkey_flag=1; 			//°´ÏµÚ6¼ü¼´Æô¶¯°´¼ü³¤°´¼Æʱ
					 if(longkey1_flag)    //Åж¨°´ÏÂʱ¼ä³¬¹ý2s
					 {
						 th++;              //µÚ6¼üÖ´Ðг¤°´¹¦ÄÜ£¬ãÐÖµ×ÔÔö£¬Ã¿500msÖ´ÐÐÒ»´Î£¬Ö´ÐÐËÙ¶ÈÓÉc7¼ÆÊý¾ö¶¨
						 if(th>=70)th=70;   //ãÐÖµ×î¸ß²»³¬¹ý70
					 }
					break;
				}
				case 7:                 //°´ÏµÚ7¼ü²»ËÉÊÖ
				{ 
					 longkey_flag=1;      //°´ÏµÚ6¼ü¼´Æô¶¯°´¼ü³¤°´¼Æʱ
					 if(longkey1_flag)    //Åж¨°´ÏÂʱ¼ä³¬¹ý2s
					 { 
						 th--;              //µÚ7¼üÖ´Ðг¤°´¹¦ÄÜ£¬ãÐÖµ×Ô¼õ£¬Ã¿500msÖ´ÐÐÒ»´Î£¬Ö´ÐÐËÙ¶ÈÓÉc7¼ÆÊý¾ö¶¨
						 if(th<=1)th=1;     //ãÐÖµ×îµÍ²»ÉÙÓÚ1
					 }
					break;
				}	
			}		
      c7=0;
	  }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值