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

📄 ds18b20温度测量.txt

📁 这是本人花了200元买的51开发板上的所有程序资料
💻 TXT
字号:

#include "AT89X51.h"
#include "intrins.h"
//common part 
#define  HIGH     1
#define  LOW      0
#define  TRUE     1
#define  ZERO     0 
#define  MSB      0x80
//ds18b20 part
#define  SkipRom              0xcc
#define  ConvertTemperature   0x44
#define  ReadScratchpad       0xbe

/*******************************************************************/
sbit One_Wire_Bus=P2^7;

unsigned char code numcode[]={0xc0,0xf9,0xa4,0xb0,0x99,
                              0x92,0x82,0xf8,0x80,0x90,
							  0x86,0x8c,0xb7,0xff
							  };//数字0~9及"EP="共阳数码管代码 
unsigned char code dot_numcode[]={0X40,0X79,0X24,0X30,0X19,
                                  0X12,0X02,0X78,0X00,0X10
								 };//带数点的0~9共阳数码管代码
unsigned char code bitcode[]={0xfe,0xfd,0xfb,0xf7,
                              0xef,0xdf,0xbf,0x7f}; //数码管位选代码
unsigned char dispbuff[8]={0,0,0,12,11,10,13,13};
unsigned int timecount;
unsigned char disp_bit_count;

/********************************************************************/
data unsigned char GetScratchpad[2];
code unsigned char decimalH[16]={00,06,12,18,25,31,37,43,50,56,62,68,75,81,87,93};
code unsigned char decimalL[16]={00,25,50,75,00,25,50,75,00,25,50,75,00,25,50,75};
unsigned char ResultTemperatureH;
unsigned char ResultTemperatureLH,ResultTemperatureLL;
 unsigned char ResultSignal;

/********************************************************************/
void One_Wire_Delay(unsigned char delay_time)
{ 
 while(delay_time)delay_time--;//Delay time us :=(8+delay_time*6)us;
}
void Initize_One_Wire_Bus(void)
{ 
 One_Wire_Bus=0;
 One_Wire_Delay(80);//Bus master pulling low 488us 
 One_Wire_Bus=1;
 One_Wire_Delay(25);//Resister pull up 158us;
}//Intialization the 1-wire devices;
/***********************************************************************/
/******************* ds 18b20 **********************************/

/***********************************************************************/
unsigned char One_Wire_Read_Byte(void)
{ 
 bit temp_bit;
 unsigned char i,result=0;
 for(i=0;i<8;i++)
 { 
  One_Wire_Bus=0;
  One_Wire_Bus=1; 
  temp_bit=One_Wire_Bus;
  One_Wire_Delay(9);//delay 62 us
  if(temp_bit)
   result|=0x01<<i;
 }
 return(result);
  //return the result of the 1-wire device;
}//Read a byte from the 1-wire bus;
/***********************************************************************/
void One_Wire_Write_Byte(unsigned char oww_dat)
{  
 unsigned char i;
 for(i=0;i<8;i++)
 { 
  One_Wire_Bus=0;
  if(oww_dat&0x01)One_Wire_Bus=1; 
  One_Wire_Delay(20);//delay 128 us
  One_Wire_Bus=1;
  oww_dat>>=1;
 }
 One_Wire_Delay(10);
}//Write a byte to the 1-wire bus;
/***********************************************************************/
void Read_18B20(void)
{ 
 unsigned char tempH,tempL;
 Initize_One_Wire_Bus();
 One_Wire_Write_Byte(SkipRom);
 _nop_();
  //There is just one DS1820 on the bus;
 One_Wire_Write_Byte(ConvertTemperature);
 One_Wire_Delay(5);
  //Start to convert temperature;
 Initize_One_Wire_Bus();
 One_Wire_Write_Byte(SkipRom);
 _nop_();
 One_Wire_Write_Byte(ReadScratchpad);
 GetScratchpad[0]=One_Wire_Read_Byte();
  //Master samples the LSB temperature from the scratchpad;
 GetScratchpad[1]=One_Wire_Read_Byte();
  //Master samples the MSB temperature from the scratchpad;
 One_Wire_Delay(120);
 tempH=(GetScratchpad[1]<<4)|(GetScratchpad[0]>>4);  
 tempL=(GetScratchpad[0]&0x0f);
 Initize_One_Wire_Bus();
  //Issue a reset to terminate left parts;
 if(tempH&0x80)
 { 
  tempH=~tempH;
  tempL=~tempL+1;
  ResultSignal=1;
  //Negative temperature;
 }
 ResultTemperatureH=tempH;
 ResultTemperatureLL=decimalL[tempL];
 ResultTemperatureLH=decimalH[tempL];
  //Result of temperature; 
}//Read the byte0 and byte1 from scratchpad;
/************************************************************************/


  //The signal of temperature;
/********************************************************************/
void main(void)
{ 
 
  TMOD=0x02;  //使用定时器0,选择方式2(常数自动重装的8位定时器)
  TH0=0x06;   //保存数值,用于自动重装
  TL0=0x06;  //定时250uS初值
  TR0=1;    //开定时器0
  ET0=1;    //开定时器0溢出中断
  EA=1;     //开总中断
 Initize_One_Wire_Bus(); 
 while(TRUE )    
 {

 
 // Read_18B20();
 // i=ResultTemperatureH/10;
 // j=ResultTemperatureH-(i*10);
;
 // LCD_write_string(0,LINE1,"  DS1B20 TEST  ");
 // LCD_write_string(0,LINE2,"BY ZL T IS:   .");
 // LCD_write_char(0x0c,LINE2,i|0x30);
//  LCD_write_char(0x0d,LINE2,j|0x30);
 // LCD_write_char(0x0f,LINE2,(ResultTemperatureLH/10)|0x30);
 }
}

/*********250uS中断服务程序*************/ 
void t0(void) interrupt 1 using 0
{
 if(timecount==40)
   {  
      timecount=0;
	  if(disp_bit_count==1)
        P0=dot_numcode[dispbuff[disp_bit_count]];
      else
	    P0=numcode[dispbuff[disp_bit_count]];
 	  P1=bitcode[disp_bit_count];
	  disp_bit_count++;
	  if(disp_bit_count==8)
	     disp_bit_count=0;
   }
 // if(timecount==8)    //每2ms采样一次温度
    {
     Read_18B20();
     dispbuff[2]=(ResultTemperatureH%100)/10;  //温度十位
	 dispbuff[1]=ResultTemperatureH%10;     //温度个位
     dispbuff[0]=ResultTemperatureLH/10;  //温度小数位
	}
}

⌨️ 快捷键说明

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