蓝桥杯温度

#include <stdio.h>
#include <stc15f2k60s2.h>
#include <intrins.h>
#include "onewire.h"
#define u8 unsigned char 
#define u16 unsigned int

u8 dis,flag,c=0;
u8 keynum=0,flagkey=0,flagtemp=0;
u16 temp=0;
int aaa=0,bbb=0;
float a,b;
u8 discode[8]={16,16,16,16,16,16,16,16};
code unsigned char tab[] =
{
0xc0, //0
0xf9, //1
0xa4, //2
0xb0, //3
0x99, //4
0x92, //5
0x82, //6
0xf8, //7
0x80, //8
0x90, //9
0x88, //A10
0x83, //b11
0xc6, //C12
0xa1, //d13
0x86, //E14
0x8e, //F15
	0xff,//Ãð16
		0xff,//Ãð17
		0xff,//Ãð18
		0xff,//Ãð19
0x40, //20
0x79, //1
0x34, //2
0x40, //3
0x19, //4
0x12, //5
0x02, //6
0x78, //7
0x00, //8
0x10, //9
};
void delay(u16 t)
{
	while(t--){;}
}
float rd_temperature(void)
{
    unsigned int temp;
	float temperature;
    unsigned char low,high;
  
  	init_ds18b20();
  	Write_DS18B20(0xCC);
  	Write_DS18B20(0x44); //Æô¶¯Î¶Èת»»
  	Delay_OneWire(200);

  	init_ds18b20();
  	Write_DS18B20(0xCC);
  	Write_DS18B20(0xBE); //¶ÁÈ¡¼Ä´æÆ÷

  	low = Read_DS18B20(); //µÍ×Ö½Ú
  	high = Read_DS18B20(); //¸ß×Ö½Ú  
	temp = (high&0x0f);
	temp <<= 8;
	temp |= low;
	temperature = temp*0.0625;
  
  	return temperature;
}

void display3()
{		b=(a-(int)a)*10000;
	
		discode[5]=(int)a%100/10;
		discode[6]=((int)a%10)+20;
		discode[7]=(int)b/1000;
	
}
	void Delay5ms()		//@12.000MHz
{
	unsigned char i, j;

	i = 59;
	j = 90;
	do
	{
		while (--j);
	} while (--i);
}



void choosep2(u8 a)
{
	switch(a)
		{case 4:
			P2=P2&0x1f|0x8f;
			break;
			case 5:
			P2=P2&0x1f|0xaf;
			break;
			case 6:
			P2=P2&0x1f|0xcf;
			break;
			case 7:
			P2=P2&0x1f|0xef;
			break;
			case 0:
			P2=P2&0x1f|0x1f;
			break;

	}		
}
void display()
{
	choosep2(7);
	P0=0xff;
	choosep2(6);
	P0=0x01<<dis;
	choosep2(7);
	P0=tab[discode[dis++]];
	 //Delay1000ms();

	if(dis==8)
		dis=0;
}
void key()
{
	P30=0;P31=P32=P33=1;
	if(P44==0)
	{Delay5ms();if(P44==0){keynum=7;flagkey=1;}while(P44==0);
	}if(P42==0)
	{Delay5ms();if(P42==0){keynum=11;flagkey=1;}while(P42==0);
	}if(P35==0)
	{Delay5ms();if(P35==0){keynum=15;flagkey=1;}while(P35==0);
	}if(P34==0)
	{Delay5ms();if(P34==0){keynum=19;flagkey=1;}while(P34==0);
	}
	P31=0;P30=P32=P33=1;
	if(P44==0)
	{Delay5ms();if(P44==0){keynum=6;flagkey=1;}while(P44==0);
	}if(P42==0)
	{Delay5ms();if(P42==0){keynum=10;flagkey=1;}while(P42==0);
	}if(P35==0)
	{Delay5ms();if(P35==0){keynum=14;flagkey=1;}while(P35==0);
	}if(P34==0)
	{Delay5ms();if(P34==0){keynum=18;flagkey=1;}while(P34==0);
	}	
	P32=0;P30=P31=P33=1;
	if(P44==0)
	{Delay5ms();if(P44==0){keynum=5;flagkey=1;}while(P44==0);
	}if(P42==0)
	{Delay5ms();if(P42==0){keynum=9;flagkey=1;}while(P42==0);
	}if(P35==0)
	{Delay5ms();if(P35==0){keynum=13;flagkey=1;}while(P35==0);
	}if(P34==0)
	{Delay5ms();if(P34==0){keynum=17;flagkey=1;}while(P34==0);
	}
	P33=0;P30=P32=P31=1;
	if(P44==0)
	{Delay5ms();if(P44==0){keynum=4;flagkey=1;}while(P44==0);
	}if(P42==0)
	{Delay5ms();if(P42==0){keynum=8;flagkey=1;}while(P42==0);
	}if(P35==0)
	{Delay5ms();if(P35==0){keynum=12;flagkey=1;}while(P35==0);
	}if(P34==0)
	{Delay5ms();if(P34==0){keynum=16;flagkey=1;}while(P34==0);
	}
	
}
void Timer0Init(void)		//2??@12.000MHz
{
//	AUXR |= 0x80;		//?????1T??
//	TMOD &= 0xF0;		//???????
 TMOD |= 0x01;  //ÅäÖö¨Ê±Æ÷¹¤×÷ģʽ
   TH0=	(65536-1000)/256;
   TL0=	(65536-1000)%256;
	//TF0 = 0;		//??TF0??
	TR0 = 1;		//???0????
	ET0=1;
	EA=1;
}
void Timer1Init(void)		//2??@12.000MHz
{
 TMOD |= 0x10; 
	TL1 = 0x40;		//??????
	TH1 = 0xA2;		//??????
	TF1 = 0;		//??TF1??
	TR1 = 1;		//???1????	
	ET1=1;
	EA=1;
}

void main()
{Timer0Init()	;

	while(1)
	{	key();
		
		if(flag==1)
		{
				choosep2(4);
				P0=0x01<<c++;

				if(c==8)c=0;
				flag=0;
		}if(flagkey==1&&keynum==14)
		{flagkey=0;
				choosep2(5);
				P0=0x50;
		}
		if(flagkey==1&&keynum==16)
		{
			a=rd_temperature();	
				display3();
		}if(flagkey==1 && keynum==5)
		{flagkey=0;
				choosep2(5);
				P0=0x00;
		}
	}
}
void time1() interrupt 1
{   TH0=	(65536-1000)/256;
   TL0=	(65536-1000)%256;
	
	display();
	aaa++;
	
	if(aaa==1000)
	{flag=1;

	aaa=0;}
}



#ifndef __ONEWIRE_H
#define __ONEWIRE_H

void Delay_OneWire(unsigned int t)  ;
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
#endif


.c补
#include <stc15f2k60s2.h>
sbit DQ=P1^4;
//

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值