资讯详情

蓝桥杯单片机第13届省赛程序题参考答案

答案仅供参考,有错误欢迎纠正,非常感谢!

文件多,代码长,谢谢查看!

main.h

main.c

Init.h

Init.c

SMG.h

SMG.c

DSQ.h

DSQ.c

YanShi.h

YanShi.c

JZKey.h

JZKey.c

ds1302.h

ds1302.c

onewire.h

onewire.c

main.h

#ifndef __MAIN_H_ #define __MAIN_H_  #define uchar unsigned char #define uint unsigned int   #include <STC15F2K60S2.H>  #include "Init.h" #include "SMG.h" #include "DSQ.h" #include "YanShi.h" #include "JZKey.h" #include "ds1302.h" #include "onewire.h"  unsigned char JZKey_GetKeynum(); void JieMian_Show(); void LED_Show();  #endif

main.c

#include "main.h"  uchar flag0=0; uchar jie_mian=0; uchar Key_num=0; uchar mo_shi=0; uchar shi_jian_show_flag=0; uchar can_shu=23; float wen_du; uint L1_time=0; uchar L1_time_flag=0; uchar JDQ_flag=0; uint L3_time=0; uchar flag=2; uint wen_du_temp=0; uchar flag1=1;  unsigned char JZKey_GetKeynum(){  unsigned char key_num=0;  if(P30==0){   P30=0;   P34=1;   P35=1;   P42=1;   P44=1;   if(P34==0){    Delay10ms();    if(P34==0){     key_num=19;    }    while(P34==0);   }   else if(P35==0){    Delay10ms();    if(P35==0){     key_num=15;    }    while(P35==0);   }   else if(P42==0){    Delay10ms();    if(P42==0){     key_num=11;    }    while(P42==0);   }   else if(P44==0){    Delay10ms();    if(P44==0){     key_num=7;    }    while(P44==0);   }  }  else if(P31==0){   P31=0;   P34=1;   P35=1;   P42=1;   P44=1;   if(P34==0){    Delay10ms();    if(P34==0){     key_num=18;    }    while(P34==0);   }   else if(P35==0){    Delay10ms();    if(P35==0){     key_num=14;    }    while(P35==0);   }   else if(P42==0){    Delay10ms();    if(P42==0){     key_num=10;    }    while(P42==0);   }   else if(P44==0){    Delay10ms();    if(P44==0){     key_num=6;    }    while(P44==0);   }  }  else if(P32==0){   P32=0;   P34=1;   P35=1;   P42=1;   P44=1;   if(P34==0){            Delay10ms();    if(P34==0){     key_num=17;    }    if(jie_mian==2||jie_mian==0){     while(P34==0);    }   }   else if(P35==0){    Delay10ms();    if(P35==0){     key_num=13;    }       while(P35==0);   }      else if(P42==0){    Delay10ms();    if(P42==0){     key_num=9;    }    while(P42==0);    }   else if(P44==0){    Delay10ms();    if(P44==0){     key_num=5;    }    while(P44==0);   }       }  else if(P33==0){   P33=0;   P34=1;   P35=1;   P42=1;   P44=1;   if(P34==0){    Delay10ms();    if(P34==0){     key_num=16;    }    while(P34==0);   }   else if(P35==0){    Delay10ms();    if(P35==0){     key_num=12;    }    while(P35==0);   }   else if(P42==0){    Delay10ms();    if(P42==0){     key_num=8;    }    while(P42==0);   }   else if(P44==0){    Delay10ms();    if(P44==0){     key_num=4;    }    while(P44==0);   }  }  JZKey_Init();  return key_num; }  void main(){  Init_BZ();  SMG_Init();  DSQ_0_Init();  JZKey_Init();  DS1302_XieDate(23,59,30);  while(1){   EA=0;   DS1302_GetDate();   EA=1;      Key_num=JZKey_GetKeynum();   if(Key_num==12){    jie_mian  ;    jie_mian=jie_mian%3;    shi_jian_show_flag=0;   }   else if(Key_num==13){    mo_shi  ;    mo_shi=mo_shi%2;   }   else if(Key_num==16&&jie_mian==2){    can_shu  ;    if(can_shu>=99){     can_shu=99;    }   }   else if(Key_num==17&&jie_mian==(2)///参数设置界面    can_shu--;    if(can_shu<=10){     can_shu=10;    }   }   else if(Key_num==17&&jie_mian==1){///时间显示界面    shi_jian_show_flag=1;   }   else if(Key_num==0){    shi_jian_show_flag=0;   }   if(flag==2){    wen_du=DS18B20_GetWenDu();    wen_du_temp=wen_du*10;    flag=0;   }  } }  void DSQ_0_ZD() interrupt 1{  flag0  ;  flag  ;    //L1时间技计数  if(fen==0&&miao==0){   L1_time_flag=1;   if(mo_shi==1){//继电器受时间控制    JDQ_flag=1;    P2=(P2&0x1f)|0x00;    P0=0x10;    P2=(P2&0x1f)|0xa0;    P2=(P2&0x1f)|0x00;   }  }  else if(L1_time==0){   JDQ_flag=0;   P2=(P2&0x1f)|0x00;   P0=0x00;   P2=(P2&0x1f)|0a0;
		P2=(P2&0x1f)|0x00;
	}
	if(L1_time_flag){
		L1_time++;
		if(L1_time>=5000){
			L1_time=0;
			L1_time_flag=0;
			if(mo_shi==1){//继电器受时间控制
				JDQ_flag=0;
				P2=(P2&0x1f)|0x00;
				P0=0x00;
				P2=(P2&0x1f)|0xa0;
				P2=(P2&0x1f)|0x00;
			}
		}
	}
	
	//继电器标志位
	if(mo_shi==0){//温度控制模式
		if(wen_du>can_shu){//继电器开
			JDQ_flag=1;
			P2=(P2&0x1f)|0x00;
			P0=0x10;
			P2=(P2&0x1f)|0xa0;
			P2=(P2&0x1f)|0x00;
		}
		else{//继电器关
			JDQ_flag=0;
			P2=(P2&0x1f)|0x00;
			P0=0x00;
			P2=(P2&0x1f)|0xa0;
			P2=(P2&0x1f)|0x00;
		}
	}
	
	//L3时间计数
	if(JDQ_flag){//继电器开启,L3开始闪烁
		L3_time++;
		L3_time=L3_time%200;
	}
	else{
		L3_time=0;
	}
	
	if(flag0>=1){
		flag0=0;
		P2=(P2&0x1f)|0x00;
		JieMian_Show();
	}
	LED_Show();
}

void JieMian_Show(){
	switch(jie_mian){
		case 0:{//温度显示界面
			if(wen_du_temp>=100){
				SMG_Show(25,1,16,16,16,wen_du_temp/100,((wen_du_temp/10)%10)+32,wen_du_temp%10);
			}
			else if(wen_du_temp>=0){
				SMG_Show(25,1,16,16,16,16,(wen_du_temp/10)+32,wen_du_temp%10);
			}
			break;
		}
		case 1:{//时间显示界面
			if(shi_jian_show_flag){//显示分秒
				SMG_Show(25,2,16,fen/10,fen%10,17,miao/10,miao%10);
			}
			else{//显示时分
				SMG_Show(25,2,16,shi/10,shi%10,17,fen/10,fen%10);
			}
			break;
		}
		case 2:{//参数设置界面
			SMG_Show(25,3,16,16,16,16,can_shu/10,can_shu%10);
			break;
		}
	}
}

void LED_Show(){
	uchar L1=0xff;
	uchar L2=0xff;
	uchar L3=0xff;
	if(L1_time>0){//L1时间大于0,时间计数启动,L1点亮
		L1=0xfe;
	}
	else{
		L1=0xff;
	}
	if(mo_shi==0){//温度控制模式,L2点亮
		L2=0xfd;
	}
	else{
		L2=0xff;
	}
	if((L3_time>=0&&L3_time<100)&&JDQ_flag==1){//继电器开启,L3闪烁
		L3=0xfb;
	}
	else{
		L3=0xff;
	}
	P0=L1&L2&L3;
	P2=(P2&0x1f)|0x80;
	P2=(P2&0x1f)|0x00;
}

Init.h

#ifndef __INIT_H_
#define __INIT_H_

#include <STC15F2K60S2.H>

void Init_BZ();

#endif

Init.c

#include "Init.h"

void Init_BZ(){
	P2=(P2&0x1f)|0xa0;
	P0=0x00;
	P2=(P2&0x1f)|0x80;
	P0=0xff;
}

SMG.h

#ifndef __SMG_H_
#define __SMG_H_

#include <STC15F2K60S2.H>

void SMG_Init();
void SMG_Show(unsigned char n1,n2,n3,n4,n5,n6,n7,n8);

#endif

SMG.c

#include "SMG.h"

unsigned char code SMG_ZK[]={                       //??????
//   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,
    0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};    //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1

void SMG_Init(){
	P2=(P2&0x1f)|0xc0;
	P0=0xff;
	P2=(P2&0x1f)|0xe0;
	P0=0xff;
}

void SMG_Show(unsigned char n1,n2,n3,n4,n5,n6,n7,n8){
	static unsigned char i=0;
	i++;
	i=i%8;
	P2=(P2&0x1f)|0xc0;
	switch(i){
		case 1:{
			P0=0x01;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n1];
			break;
		}
		case 2:{
			P0=0x02;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n2];
			break;
		}
		case 3:{
			P0=0x04;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n3];
			break;
		}
		case 4:{
			P0=0x08;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n4];
			break;
		}
		case 5:{
			P0=0x10;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n5];
			break;
		}
		case 6:{
			P0=0x20;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n6];
			break;
		}
		case 7:{
			P0=0x40;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n7];
			break;
		}
		case 0:{
			P0=0x80;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n8];
			break;
		}
	}
	P2=(P2&0x1f)|0x00;
}

DSQ.h

#ifndef __DSQ_H_
#define __DSQ_H_

#include <STC15F2K60S2.H>

void DSQ_0_Init();

#endif

DSQ.c

#include "DSQ.h"

void DSQ_0_Init(){
	AUXR |= 0x80;		//????????T???
	TMOD &= 0xF0;		//????????a??
	TL0 = 0x20;		//????????€?
	TH0 = 0xD1;		//????????€?
	TF0 = 0;		//???TF0???
	TR0 = 1;		//??????€?????
	EA=1;
	ET0=1;
}

YanShi.h

#ifndef __YANSHI_H_
#define __YANSHI_H_

#include <STC15F2K60S2.h>
#include "intrins.h"

void Delay10ms();

#endif

YanShi.c

#include "YanShi.h"

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

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

JZKey.h

#ifndef __JZKEY_H_
#define __JZKEY_H_

#include "YanShi.h"

void JZKey_Init();


#endif

JZKey.c

#include "JZKey.h"

void JZKey_Init(){
	P30=1;
	P31=1;
	P32=1;
	P33=1;
	P34=0;
	P35=0;
	P42=0;
	P44=0;
}

ds1302.h

#ifndef __DS1302_H
#define __DS1302_H

#include <STC15F2K60S2.H>
#include <intrins.h>

extern unsigned char shi,fen,miao;

sbit SCK = P1^7;		
sbit SDA = P2^3;		
sbit RST = P1^3; 

void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );

void DS1302_XieDate(unsigned char h,unsigned char m,unsigned char s);
void DS1302_GetDate();

#endif

ds1302.c

#include "ds1302.h"  									

unsigned char shi,fen,miao;

//写字节
void Write_Ds1302(unsigned  char temp) 
{
	unsigned char i;
	for (i=0;i<8;i++)     	
	{ 
		SCK = 0;
		SDA = temp&0x01;
		temp>>=1; 
		SCK=1;
	}
}   

//向DS1302寄存器写入数据
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )     
{
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1; 	_nop_();  
 	Write_Ds1302(address);	
 	Write_Ds1302(dat);		
 	RST=0; 
}

//从DS1302寄存器读出数据
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
 	unsigned char i,temp=0x00;
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1;	_nop_();
 	Write_Ds1302(address);
 	for (i=0;i<8;i++) 	
 	{		
		SCK=0;
		temp>>=1;	
 		if(SDA)
 		temp|=0x80;	
 		SCK=1;
	} 
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
	SCK=1;	_nop_();
	SDA=0;	_nop_();
	SDA=1;	_nop_();
	return (temp);			
}

void DS1302_XieDate(unsigned char h,unsigned char m,unsigned char s){
	h=(h/10)*16+(h%10);
	m=(m/10)*16+(m%10);
	s=(s/10)*16+(s%10);
	Write_Ds1302_Byte(0x8e,0x00);
	Write_Ds1302_Byte(0x84,h);
	Write_Ds1302_Byte(0x82,m);
	Write_Ds1302_Byte(0x80,s);
	Write_Ds1302_Byte(0x8e,0x80);
}

void DS1302_GetDate(){
	shi=Read_Ds1302_Byte(0x85);
	fen=Read_Ds1302_Byte(0x83);
	miao=Read_Ds1302_Byte(0x81);
	shi=(shi/16)*10+(shi%16);
	fen=(fen/16)*10+(fen%16);
	miao=(miao/16)*10+(miao%16);
}

onewire.h

#ifndef __ONEWIRE_H
#define __ONEWIRE_H

#include <STC15F2K60S2.H>


sbit DQ = P1^4;  

unsigned char rd_temperature(void);  
float DS18B20_GetWenDu();

#endif

onewire.c

#include "onewire.h"

//单总线内部延时函数
void Delay_OneWire(unsigned int t)  
{
	unsigned char i;
	while(t--){
		for(i=0;i<12;i++);
	}
}

//单总线写操作
void Write_DS18B20(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ = 0;
		DQ = dat&0x01;
		Delay_OneWire(5);
		DQ = 1;
		dat >>= 1;
	}
	Delay_OneWire(5);
}

//单总线读操作
unsigned char Read_DS18B20(void)
{
	unsigned char i;
	unsigned char dat;
  
	for(i=0;i<8;i++)
	{
		DQ = 0;
		dat >>= 1;
		DQ = 1;
		if(DQ)
		{
			dat |= 0x80;
		}	    
		Delay_OneWire(5);
	}
	return dat;
}

//DS18B20初始化
bit init_ds18b20(void)
{
  	bit initflag = 0;
  	
  	DQ = 1;
  	Delay_OneWire(12);
  	DQ = 0;
  	Delay_OneWire(80);
  	DQ = 1;
  	Delay_OneWire(10); 
    initflag = DQ;     
  	Delay_OneWire(5);
  
  	return initflag;
}

float DS18B20_GetWenDu(){
	float wen_du,xiao_shu,zheng_shu;
	unsigned char di,gao;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	Delay_OneWire(200);
	
	init_ds18b20();
	EA=0;
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	EA=1;
	EA=0;
	di=Read_DS18B20();
	gao=Read_DS18B20();
	EA=1;
	if(gao>=248){
		di=~di;
		gao=~gao;
		if(di==0xff){
			di=0;
			gao++;
		}
		else{
			di++;
		}
		zheng_shu=((gao<<4)|(di>>4))*1.0;
		xiao_shu=(di&0x0f)*0.0625;
		wen_du=-(zheng_shu+xiao_shu);
	}
	else{
		zheng_shu=((gao<<4)|(di>>4))*1.0;
		xiao_shu=(di&0x0f)*0.0625;
		wen_du=(zheng_shu+xiao_shu);
	}
	return wen_du;
}

标签: 继电器3e47d计数继电器1e计数继电器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台