13届蓝桥杯(单片机)

一.首先这套题目难度还可以,不是那么的简单,我也做了好一会,主要是不会操作蜂鸣器!

下面就直接贴代码了

主函数main.c

#include "dmx.h"
#include "onewire.h"
void main()
{

Init_System();


while(1)
{
	 if(flag_t)
	 {
		 temp=Read_temp();
		 flag_t=0;
	 }

	scan_kbd(); //扫描按键
	key_real(); //按键操作
	Read_time(); //读时间
	SMG_show();  //赋值显示
	led_work();  //led和继电器

	
}


}

二.  然后就是配置文件了 也是程序核心

#include 
#include "dmx.h"
#include "onewire.h"
#include "ds1302.h"

unsigned char mod1=0; //界面切换
 bit mod2=0;  //模式切换
unsigned int temp=0;  //温度
unsigned int para=23;  //温度参数
unsigned int key_num=20;  //按键值
unsigned int count=0;   //计数获取温度
bit flag_L1=0;            //点亮L1标志位
bit flag_L3=0;           //点亮L3标志位
unsigned int count_L1=0;  //L1计数
unsigned int count_L3=0;  //L3计数
 bit flag_temp=0;         //获取温度标志位
 bit flag_t=0;            //时间满足标志位
bit flag_jdq_t=0;           //时间控制继电器标志位
bit flag_jdq_w=0;         //温度控制继电器标志位
 
 unsigned char stat_jdq=0xaf;  
 unsigned count_jdq=0;
unsigned char stat_led=0xff;
unsigned char Read_ds1302[3]={0}; 
unsigned char tp[8]={12,2,10,0,0,11,0,0};
unsigned char Read[8]={10,10,10,10,10,10,10,10};
unsigned char segbuff[8]={10,10,10,10,10,11,0,1};
unsigned char segtab[]={0xc0, //0
0xf9, //1
0xa4, //2
0xb0, //3
0x99, //4
0x92, //5
0x82, //6
0xf8, //7
0x80, //8
0x90, //9
0xff, // 
0xbf, //-
0xc1   //U  12
	};

void time0()   //1ms
{
    TMOD=0x11;
	TH0=(0-1000)/256;
	TL0=(0-1000)%256;
	TR0=1;
	ET0=1;
	EA=1;
	TH1=(0-5000)/256;
	TL1=(0-5000)%256;
	TR1=1;
	ET1=1;

}
void Timer3() interrupt 3  //5ms
{
 
	TH1=(0-5000)/256; 
  	TL1=(0-5000)%256;
     if(flag_L1)
	{ 	
 stat_led=stat_led&0xfe;		
	if(++count_L1==1000)
	{ 
		flag_L1=0;
		stat_led=stat_led|0x01;
	  count_L1=0;
		
	}
  }
		if(flag_L3)
	{
	   if(++count_L3==20)count_L3=0;
		 if(count_L3<=10)
			stat_led&=0xfb;
     else stat_led|=0x04;		 
	
	}
 	else  stat_led|=0x04;
	
	 if( mod2==1)
	 {  
		 if(flag_jdq_t)
		 {
		  stat_jdq=0x1f;
	   if(++count_jdq==1000)
		 {
		   count_jdq=0;
			 flag_jdq_t=0;
			 stat_jdq=0xaf;
		 
		 } 
	   }
	 
	 
	 }
	 else flag_jdq_t=0;
	
	
	


}

void Timer0() interrupt 1
{
   TH0=(0-2000)/256;
	 TL0=(0-2000)%256;
	 segs();
  count++;
	if(count==150)
	{
		flag_t=1;
		count=0;
	}

}

void led_work()
{
    
	
	  
	 if(Read_ds1302[2]==0x00&&Read_ds1302[1]==0x00)
	 {  
		 if(mod2==1)flag_jdq_t=1;
		 else flag_jdq_t=0;
		 
		 flag_L1=1;
		
	 }
	 
	 if(mod1==0) stat_led=stat_led&0xfd;
	 else  stat_led=stat_led|0x02;
	 
   if(mod2==0 )
	 {
	   if( temp/10>para) {stat_jdq=0x1f; flag_jdq_w=1;}
		 else  
		 {stat_jdq=0xaf;
			 flag_jdq_w=0;
	   }
	 }
	 if(flag_jdq_t || flag_jdq_w)
	 {
	    flag_L3=1;
	 
	 }
	 else flag_L3=0;
	 
	
	 
		P2=P2&0x1f|0x80;
  	P0=stat_led;
		
	  P2=P2&0x1f|0xa0;
	  P0=stat_jdq;
 

}


void scan_kbd()
{
	static char kbd_state = 0;
	unsigned char kbd_press;
	char row;
	switch(kbd_state){
		
		case state_0:
	 kbd_io=0x0f;P42=0;P44=0;
	  kbd_press=kbd_io;
  if(kbd_press != kbd_maskrow)
		kbd_state=state_1;
	break;
  
		 case state_1:
			 kbd_press=kbd_io;
		 if(kbd_press!=kbd_maskrow)
		 {
		  	if((kbd_io&0x08)==0)  row=4;            
				if((kbd_io&0x04)==0)  row=5;              
				if((kbd_io&0x02)==0)  row=6;                   
				if((kbd_io&0x01)==0)  row=7;   
		  
			 
			 kbd_io=0xf0;P42=1;P44=1;
			 if(P44==0)key_num=row;
			 if(P42==0)key_num=row+4;
			 if((kbd_io&0x20)==0)key_num=row+8;
			 if((kbd_io&0x10)==0)key_num=row+12; 
			
			 while((!(kbd_io&0x10))&&(row==5)&&(mod1==1))
			 {
			   tp[3]=Read_ds1302[1]>>4;
		     tp[4]=Read_ds1302[1]%16;
	       tp[6]=Read_ds1302[2]>>4;
		     tp[7]=Read_ds1302[2]%16;
				 flag_temp=1;
			 
			 
			 
			 }
			 flag_temp=0;
			 
			  kbd_state=state_2;
		 
		 }
		 else {
           kbd_state=state_0;
		 }
		 break;
		 
		 case state_2:
			kbd_io=0x0f; P42=0; P44=0;
		 kbd_press=kbd_io;
		 if(kbd_press==0x0f)kbd_state=state_0;
			 break;
		 
		 default :
			 break;
	 }

	
}
void key_real()
{
	
	switch(key_num)	
	{
	 
		case 12:
			 if(++mod1==3)mod1=0;
		   break;
		
		case 13:
			   mod2=~mod2;
		      break;
		case 16:
			  if(mod1==2)
				{  
					  para+=10;
					if(para>99)para=0;
			 
				}
		      break;
		case 17:	
         if(mod1==2)
				 {				 
			     para-=10;
					 if(para<10)
						 para=99;
				 }
		     break;
		default :break;
			
	
	}
	
		key_num=20;
	
	
	
}
void Slect_fun(unsigned char n)
{
	
	P2=P2&0x1f;
	
	switch(n)
	{
		case 0:P2|=0x00; break;
		case 4:P2|=0x80; break;
		case 5:P2|=0xa0; break;
		case 6:P2|=0xc0; break;
		case 7:P2|=0xe0; break;
		default : break; 
	
	
	
	}

}
void Init_System()
{ 
	unsigned char i;
	Slect_fun(4);P0=0xff;
	Slect_fun(5);P0=0xaf;
	for(i=0;i<3;i++)
	{
	  Read_temp();
	  
	}
	time0();
	Init_ds1302();
	
}
void segs()
{
	static unsigned char wei=0;
	Slect_fun(7);
	P0=0xff;
	
	Slect_fun(6);
	P0=1<>4;
		segbuff[4]=Read_ds1302[0]%16;
		 
	  segbuff[5]=11;
		 
		segbuff[6]=Read_ds1302[1]>>4;
		segbuff[7]=Read_ds1302[1]%16;
		
	}
	else if(mod1==2)
	{
	
	  segbuff[0]=12;
		segbuff[1]=3;
		segbuff[2]=10;
	
		
		segbuff[3]=10;
		segbuff[4]=10;
		 
	
	  segbuff[5]=10;
		 
		segbuff[6]=para/10;
		segbuff[7]=para%10;
	
	
	
	}
	
	
}
	

下面是各种.h文件了

 

#ifndef _DMX_H
#define _DMX_H

#define state_0 0
#define state_1 1
#define state_2 2
#define kbd_io  P3
#define kbd_maskrow 0x0f
extern unsigned int temp;
extern bit flag_t;
void time0();
void scan_kbd();
void key_real();
void Slect_fun(unsigned char n)	;
void segs();
void Init_ds1302();
void Read_time();
void SMG_show();
void Init_System();
void led_work();
#endif 

 其他基础文件我就不贴了,到处都是,,,,,

你可能感兴趣的:(蓝桥杯,职场和发展)