⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thermometor_main.c

📁 温度测量与显示
💻 C
字号:
//http://proteus.5d6d.com
#define	uchar	unsigned char
#define uint	unsigned int
#define ulong 	unsigned long

#include<REG52.h>
sbit	Sign_Port	=P3^0;
sfr		Dat_Port	=0x80;	
sfr		Cs_Port		=0xa0;	
sbit 	TMDAT 		= P3^3;	
uchar	code table[11]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
                          0x7F,0x6F,0x00};
uchar	tmpbuf[5];

void Delay(int useconds)
	 {
		int s;
		for (s=0; s<useconds;s++);
	}

uchar	Reset_Bus(void)
	{
		uchar	presence;
		TMDAT = 0;  			
		Delay(29); 				
		TMDAT = 1; 				
		Delay(3); 			
		presence = TMDAT; 		
		Delay(25); 			
		return(presence); 	
}

void Write_Bit(char bitval)
	{
		TMDAT = 0; 				
		if(bitval==1) TMDAT =1;
		Delay(5); 				
		TMDAT = 1; 		
	}
void Write_Byte(char val)
	 {
		uchar	i;
		uchar	temp;
		for (i=0; i<8; i++) {	
		temp = val>>i; 		
		temp &= 0x01; 		
		Write_Bit(temp); 
	}
	Delay(5);
}


uchar	Read_Bit(void) 
	{
		uchar	i;
		TMDAT = 0;     		 
		TMDAT = 1; 			
		for (i=0; i<3; i++); 
		return(TMDAT); 		
	}


uchar	Read_Byte(void)
 {
	uchar	i;
	uchar	value = 0;
	for (i=0;i<8;i++)				
	 {  				
		if(Read_Bit()) value|=0x01<<i; 	
		Delay(6); 					
	}
	return(value);
}


uint DS18B20_Tmp_Read(void)
 {
	uint 	TEMP;
	uchar	TEMP_LSB,TEMP_MSB;
	Reset_Bus();
	Write_Byte(0xCC); 				
	Write_Byte(0x44); 				
	Delay(5);												
	Reset_Bus();					
	Write_Byte(0xCC); 				
	Write_Byte(0xBE); 				
	TEMP_LSB = Read_Byte(); 
	TEMP_MSB = Read_Byte(); 
	TEMP=TEMP_MSB;
	TEMP=TEMP<<8;
	TEMP=TEMP|TEMP_LSB;
	return	TEMP;
}

void	Display(void)
	{
		uchar	i,j,temp=0xef;		
		for(i=0;i<5;i++)
			{
				Cs_Port=0xff;
				j=tmpbuf[i];
				if(temp==0xFB)
				Dat_Port=(table[j]|0x80);
				else
				Dat_Port=table[j];				
				Cs_Port=temp;
				Delay(5);
				Cs_Port=0xff;
				temp=temp>>1;
				temp=temp|0x80;				
			}
	}

void	main(void)
	{
		uint	tmp;
		uchar	i,tmph,tmpl,sign;
		while(1)
			{
				tmp=DS18B20_Tmp_Read();
				sign=(uchar)((tmp>>8)&0xf0);
				if(sign==0xf0)					
					{
						tmp=(~tmp)+1;
						Sign_Port=0;			
					}
				else	Sign_Port=1;				
				tmpl=(uchar)(tmp&0x0f);			
				tmph=(uchar)((tmp>>4)&0xff);
				tmpl=tmpl*6.25;
				tmpbuf[4]=tmpl%10;
				tmpbuf[3]=tmpl/10;
				tmpbuf[2]=tmph%10;
				tmpbuf[1]=(tmph%100)/10;
				tmpbuf[0]=tmph/100;
				if(tmpbuf[0]==0)		
						{
							tmpbuf[0]=10;
							if(tmpbuf[1]==0)
								tmpbuf[1]=10;
						}				
				for(i=0;i<20;i++)		
					{
						Display();
									
					}
				
			}
	}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -