蓝桥杯省一之路03——第十三届省赛真题

一,代码部分

1.ds1302

#include "ds1302.h"  									

//写字节
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);			
}

2.onewire.c

#include "onewire.h"

//单总线内部延时函数
void Delay_OneWire(unsigned int t)  
{
	while(t--);
}

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

//单总线读操作
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(50);
	}
	return dat;
}

//DS18B20初始化
bit init_ds18b20(void)
{
  	bit initflag = 0;
  	
  	DQ = 1;
  	Delay_OneWire(120);
  	DQ = 0;
  	Delay_OneWire(800);
  	DQ = 1;
  	Delay_OneWire(100); 
    initflag = DQ;     
  	Delay_OneWire(50);
  
  	return initflag;
}

3.ds18b20

#include "onewire.h"
#include "reg52.h"

void Conv_Temperture()
{
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	
}
float Read_Temperture()
{
	int t;
	float temp;
	unsigned char LSB,MSB;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	LSB=Read_DS18B20();
	MSB=Read_DS18B20();
	t=(MSB << 8) | LSB;
  temp=t/16.0;
	return temp;
}

4.smgshow.c 

#include "reg52.h"
void Delay(int t)
{
	while(t--);
}
void HC573(unsigned char channel, unsigned char dat)
{
	P0=dat;
	switch(channel)
	{
		case 4:
			P2=(P2 & 0x1f) | 0x80;//选通Y4C,LED
		  break;
		case 5:
			P2=(P2 & 0x1f) | 0xa0;//选通Y5C,蜂鸣器,继电器
		  break;
		case 6:
			P2=(P2 & 0x1f) | 0xc0;//选通Y6C,数码管位选
		  break;
		case 7:
			P2=(P2 & 0x1f) | 0xe0;//选通Y7C,数码管段码
		  break;
	}
	P2=(P2 & 0x1f) | 0x00;
}
void Display_SMG(unsigned char pos,unsigned char val)
{

	HC573(6,0x01<<pos);
	HC573(7,val);
	Delay(500);
//	HC573(6,0x01<<pos);
	HC573(7,0xff);//消隐
}	
void Display_ALL(unsigned char dat)
{
	HC573(6,0xff);
	HC573(7,dat);
}

5.main.c

#include "reg52.h"
#include "ds1302.h"
#include "smgshow.h"
#include "ds18b20.h"
#include "intrins.h"
sbit C1=P3^5;
sbit C2=P3^4;
sbit R1=P3^2;
sbit R2=P3^3;     
unsigned char Timer[]={0x30,0x59,0x21};//23时25分
unsigned char Write_addr[]={0x80,0x82,0x84};
int hour,min,sec;
int T;
int T_stand=23;
bit Pattern=0;
unsigned char LED_sta=0xff; 
unsigned char Relay_sta=0x00;
bit Flash=0;
void RelayWork();
void Init_18b20()
{
	float First_T;
	Conv_Temperture();
	do
	{
		 First_T=Read_Temperture();
	}while(First_T>50);
	
}
void Read_18b20()
{
	float temp;
	Conv_Temperture();
	temp=Read_Temperture()*10;
	T=temp;
}
void Init_1302()
{
	unsigned char i;
	Write_Ds1302_Byte(0x8e,0x00);
	for(i=0;i<3;i++)
	{
		Write_Ds1302_Byte(Write_addr[i],Timer[i]);
	}
	Write_Ds1302_Byte(0x8e,0x80);
}
void Read_1302()
{
	hour=Read_Ds1302_Byte(0x85);
	min=Read_Ds1302_Byte(0x83);
	sec=Read_Ds1302_Byte(0x81);
}
unsigned char sta=1;
void SMGWork()
{
	
	switch(sta)
	{
		case 1:
			Display_SMG(0,0xc1);
			Display_SMG(1,SMGnotdotduanma[1]);
			Display_SMG(5,SMGnotdotduanma[T/100]);
			Display_SMG(6,SMGdotduanma[T/10%10]);
			Display_SMG(7,SMGnotdotduanma[T%10]);
		break;
		case 2:
			Display_SMG(0,0xc1);
	 	  Display_SMG(1,SMGnotdotduanma[2]);
		  Display_SMG(3,SMGnotdotduanma[hour/16]);
		  Display_SMG(4,SMGnotdotduanma[hour%16]);
		  Display_SMG(5,~0x40);
		  Display_SMG(6,SMGnotdotduanma[min/16]);
		  Display_SMG(7,SMGnotdotduanma[min%16]);
		break;
		case 3:
			Display_SMG(0,0xc1);
	 	  Display_SMG(1,SMGnotdotduanma[3]);
			Display_SMG(6,SMGnotdotduanma[T_stand/10]);
			Display_SMG(7,SMGnotdotduanma[T_stand%10]);
		break;
	}
		RelayWork();
	
}
void Scan_KBD()
{
	//扫描S12
	R2=0;
	R1=C1=C2=1;
	if(C1==0)
	{
		Delay(500);
		if(C1==0)
		{
			if(sta==1)
			{
				sta=2;
			}
			else if(sta==2)
			{
				sta=3;
			  
			}
			else
			{
				sta=1;
			}
			while(C1==0)
			{
				SMGWork();
			}
		}
	}
	//扫描S16
	R2=0;
	R1=C1=C2=1;
	if(C2==0)
	{
		Delay(500);
		if(C2==0)
		{
			if(sta==3 && T_stand<99)
			{
				T_stand=T_stand+1;
			}
		}
		while(C2==0)
			{
				SMGWork();
			}
	}
	//扫描S17
	R1=0;
	R2=C1=C2=1;
	if(C2==0)
	{
		Delay(500);
		if(C2==0)
		{
			if(sta==3 && T_stand>10)
			{
				T_stand=T_stand-1;
			}
			if(sta==2)
			{
				while(C2==0)
				{
					Read_1302();
					Display_SMG(0,0xc1);
	 	      Display_SMG(1,SMGnotdotduanma[2]);
					Display_SMG(3,SMGnotdotduanma[min/16]);
					Display_SMG(4,SMGnotdotduanma[min%16]);
					Display_SMG(5,~0x40);
					Display_SMG(6,SMGnotdotduanma[sec/16]);
					Display_SMG(7,SMGnotdotduanma[sec%16]);
					RelayWork();
				}
			}
		}
		while(C2==0)
			{
				SMGWork();
			
			}
	}
	//扫描S13
	R1=0;
	R2=C1=C2=1;
	if(C1==0)
	{
		Delay(500);
		if(C1==0)
		{
			if(Pattern==0)
			{
				Pattern=1;//时间控制模式
			  LED_sta |= 0x02;
				HC573(4,LED_sta);
				Relay_sta=0x00;
				HC573(5,Relay_sta);
				
			}
			else
			{
				Pattern=0;//温度控制模式	
				LED_sta &= ~0x02;
				HC573(4,LED_sta);
				Relay_sta=0x00;
				HC573(5,Relay_sta);
			}
		}
		
		while(C1==0)
			{
				SMGWork();
			}
	}
}
unsigned char Relay_a=0;
void RelayWork()
{
 
	if(Pattern==0 && T>T_stand*10)
	{
		Relay_sta=0x10;
		HC573(5,Relay_sta);
		
	}
	else if(Pattern==1 && min==0 && sec==0)
	{
		Relay_a=1;
		Relay_sta=0x10;
		HC573(5,Relay_sta);
		LED_sta &= ~0x01;
		HC573(4,LED_sta);
	}
	else if(Relay_a==0)
	{
		    Relay_sta=0x00;
				HC573(5,Relay_sta);
				LED_sta |= 0x01;
				HC573(4,LED_sta);
	}
	
}
	

void Ini_Timer0()
{
	TMOD=0x01;
	TH0=(0-10000)/256;//定时10ms
	TL0=(0-10000)%256;
	TR0=1;
	ET0=1;
	EA=1;
}
int count=0;
unsigned count_LED=0;
void Service_T0() interrupt 1
{
	TH0=(0-10000)/256;//定时10ms
	TL0=(0-10000)%256;
	count_LED+=1;
	if(Relay_a==1)
	{
	count=count+1;
	}
	if(count==500)
	{
		count=0;
		Relay_a=0;
	}
	if(count_LED==10)
	{
		Flash=!Flash;
		count_LED=0;
	}
}
void Inisystem()
{
	LED_sta &=~0x02;
	HC573(4,LED_sta);
	HC573(5,0x00);
	Display_ALL(0xff);
	Init_1302();
	Init_18b20();
	Ini_Timer0();
}
void main()
{
	Inisystem();
	while(1)
	{	
	  Read_1302();
		Read_18b20();
		SMGWork();
		Scan_KBD();
		RelayWork();
		if(Flash==0 && Relay_sta==0x10 )
		{
		LED_sta &= ~0x04;
		HC573(4,LED_sta);
		}
		else
		{
		LED_sta |= 0x04;
		HC573(4,LED_sta);
		}
	
	}
	
}

相比之前的代码加的个初始化ds18b20的函数,解决了以前的数码管显示的温度进行了跳变。

二,总结

      无

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值