蓝桥杯单片机第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

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)|0xa0;
		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;
}

练习版

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"

void LED_Show();

#endif

main.c

#include "main.h"

uchar key_num;
uchar key_num_old;
uchar JieMian=0;
uchar MoShi=0;
int WenDu=0;
uchar shi;
uchar fen;
uchar miao;
uchar WenDu_CanShu=23;//温度参数
uchar L1_flag=0;//L1亮灭标志位
uint L1_time=0;
uchar JDQ=0;//继电器标志位
uchar L3_time_flag=0;
uchar L3_time=0;
uchar time=0;

void main(){
	Init_BZ();
	SMG_Init();
	DSQ_2_Init();
	JZKey_Init();
	DS18B20_GetWenDu();
	Ds1302_XieData(23,59,50);
	while(1){
		key_num_old=key_num;
		key_num=JZKey_GetKeynum();
		if(key_num_old!=key_num){
			if(key_num==12){
				JieMian++;
				JieMian=JieMian%3;
			}
			else if(key_num==13){
				MoShi++;
				MoShi=MoShi%2;
				if(MoShi){
					L3_time_flag=0;
				}
			}
			else if(key_num==16&&JieMian==2){
				WenDu_CanShu++;
				if(WenDu_CanShu>=99){
					WenDu_CanShu=99;
				}
			}
			else if(key_num==17&&JieMian==2){
				WenDu_CanShu--;
				if(WenDu_CanShu<=10){
					WenDu_CanShu=10;
				}
			}
		}
		
		if(time>=10){
			time=0;
			WenDu=DS18B20_GetWenDu()*10;
			
			shi=Ds1302_GetShi();
			fen=Ds1302_GetFen();
			miao=Ds1302_GetMiao();
			if(fen==0&&miao==0){
				L1_flag=1;
				L1_time=0;
				if(MoShi){
					P0=0x10;
					P2=(P2&0x1f)|0xa0;
					P2=(P2&0x1f)|0x00;
					L3_time_flag=1;
				}
			}
			
			if(MoShi==0){
				if(WenDu>WenDu_CanShu*10){
					P0=0x10;
					L3_time_flag=1;
				}
				else{
					P0=0x00;
					L3_time_flag=0;
				}
				P2=(P2&0x1f)|0xa0;
				P2=(P2&0x1f)|0x00;
			}
		}
	}
}

void DSQ_2_ZD() interrupt 12{
	time++;
	
	if(L1_flag){
		L1_time++;
		if(L1_time>=5000){
			L1_time=0;
			L1_flag=0;
			if(MoShi){
				P0=0x00;
				P2=(P2&0x1f)|0xa0;
				P2=(P2&0x1f)|0x00;
				L3_time_flag=0;
				L3_time=0;
			}
		}
	}
	
	if(L3_time_flag){
		L3_time++;
		if(L3_time>=200){
			L3_time=0;
		}
	}
	
	if(JieMian==0){
		if(WenDu>=100){
			SMG_Show(25,1,16,16,16,WenDu/100,((WenDu/10)%10)+32,WenDu%10);
		}
		else if(WenDu>=0){
			SMG_Show(25,1,16,16,16,16,(WenDu/10)+32,WenDu%10);
		}
		else if(WenDu<0){
			SMG_Show(25,1,16,16,16,17,(((-WenDu)/10)%10)+32,(-WenDu)%10);
		}
	}
	else if(JieMian==1){
		if(key_num==17){
			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);
		}
	}
	else if(JieMian==2){
		SMG_Show(25,3,16,16,16,16,WenDu_CanShu/10,WenDu_CanShu%10);
	}
	
	LED_Show();
}

void LED_Show(){
	uchar L1=0xff;
	uchar L2=0xff;
	uchar L3=0xff;
	if(L1_flag){
		L1=0xfe;
	}
	if(MoShi==0){
		L2=0xfd;
	}
	if(L3_time_flag&&L3_time<=100){
		L3=0xfb;
	}
	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 t_display[]={                       //????
//   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=~t_display[n1];
			break;
		}
		case 2:{
			P0=0x02;
			P2=(P2&0x1f)|0xe0;
			P0=~t_display[n2];
			break;
		}
		case 3:{
			P0=0x04;
			P2=(P2&0x1f)|0xe0;
			P0=~t_display[n3];
			break;
		}
		case 4:{
			P0=0x08;
			P2=(P2&0x1f)|0xe0;
			P0=~t_display[n4];
			break;
		}
		case 5:{
			P0=0x10;
			P2=(P2&0x1f)|0xe0;
			P0=~t_display[n5];
			break;
		}
		case 6:{
			P0=0x20;
			P2=(P2&0x1f)|0xe0;
			P0=~t_display[n6];
			break;
		}
		case 7:{
			P0=0x40;
			P2=(P2&0x1f)|0xe0;
			P0=~t_display[n7];
			break;
		}
		case 0:{
			P0=0x80;
			P2=(P2&0x1f)|0xe0;
			P0=~t_display[n8];
			break;
		}
	}
	P2=(P2&0x1f)|0x00;
}

DSQ.h

#ifndef __DSQ_H_
#define __DSQ_H_

#include <STC15F2K60S2.H>

void DSQ_2_Init();

#endif

DSQ.c

#include "DSQ.h"

void DSQ_2_Init(){
	AUXR |= 0x04;		//定时器时钟1T模式
	T2L = 0x20;		//设置定时初值
	T2H = 0xD1;		//设置定时初值
	AUXR |= 0x10;		//定时器2开始计时
	AUXR=AUXR&0xf7;
	IE2=IE2|0x04;
	EA=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();
unsigned char JZKey_GetKeynum();

#endif

JZKey.c

#include "JZKey.h"

void JZKey_Init(){
	P32=1;
	P33=1;
	P34=0;
	P35=0;
}

unsigned char JZKey_GetKeynum(){
	unsigned char key_num=0;
	if(P32==0){
		P32=0;
		P34=1;
		P35=1;
		if(P34==0){
			Delay10ms();
			if(P34==0){
				key_num=17;
			}
			//while(P34==0);
		}
		else if(P35==0){
			Delay10ms();
			if(P35==0){
				key_num=13;
			}
			//while(P35==0);
		}
	}
	else if(P33==0){
		P33=0;
		P34=1;
		P35=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);
		}
	}
	JZKey_Init();
	return key_num;
}

ds1302.h

#ifndef __DS1302_H
#define __DS1302_H

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

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_XieData(unsigned char shi,fen,miao);
unsigned char Ds1302_GetShi();
unsigned char Ds1302_GetFen();
unsigned char Ds1302_GetMiao();

#endif

ds1302.c

/*
  程序说明: DS1302驱动程序
  软件环境: Keil uVision 4.10 
  硬件环境: CT107单片机综合实训平台 8051,12MHz
  日    期: 2011-8-9
*/

#include "ds1302.h"

sbit SCK=P1^7;		
sbit SDA=P2^3;		
sbit RST = P1^3;   // DS1302复位												

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

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

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_XieData(unsigned char shi,fen,miao){
	shi=(shi/10)*16+(shi%10);
	fen=(fen/10)*16+(fen%10);
	miao=(miao/10)*16+(miao%10);
	Write_Ds1302_Byte(0x8e,0x00);
	Write_Ds1302_Byte(0x84,shi);
	Write_Ds1302_Byte(0x82,fen);
	Write_Ds1302_Byte(0x80,miao);
	Write_Ds1302_Byte(0x8e,0x80);
}

unsigned char Ds1302_GetShi(){
	unsigned char date;
	date=Read_Ds1302_Byte(0x85);
	date=(date/16)*10+(date%16);
	return date;
}

unsigned char Ds1302_GetFen(){
	unsigned char date;
	date=Read_Ds1302_Byte(0x83);
	date=(date/16)*10+(date%16);
	return date;
}

unsigned char Ds1302_GetMiao(){
	unsigned char date;
	date=Read_Ds1302_Byte(0x81);
	date=(date/16)*10+(date%16);
	return date;
}

onewire.h

#ifndef __ONEWIRE_H
#define __ONEWIRE_H

#include <STC15F2K60S2.H>

float DS18B20_GetWenDu();

#endif

onewire.c

/*
  程序说明: 单总线驱动程序
  软件环境: Keil uVision 4.10 
  硬件环境: CT107单片机综合实训平台(外部晶振12MHz) STC89C52RC单片机
  日    期: 2011-8-9
*/
#include "onewire.h"

sbit DQ = P1^4;  //单总线接口

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

//通过单总线向DS18B20写一个字节
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);
}

//从DS18B20读取一个字节
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,zs,xs;
	unsigned char di,gao;
	init_ds18b20();
	//EA=0;
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	//EA=1;
	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>=240){
		di=~di;
		gao=~gao;
		if(di==0xff){
			di=0;
			gao++;
		}
		else{
			di++;
		}
		zs=((gao<<4)|(di>>4))*1.0;
		xs=(di&0x0f)*0.0625;
		wen_du=-(zs+xs);
	}
	else{
		zs=((gao<<4)|(di>>4))*1.0;
		xs=(di&0x0f)*0.0625;
		wen_du=(zs+xs);
	}
	return wen_du;
}
  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

因心,三人水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值