📄 ds18b20温度测量.c
字号:
//MCU: AT89S51
//晶振:12M
#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,0X9C,0XC6
};//数字0~9及"EP=OC"共阳数码管代码
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]={14,13,0,0,0,12,11,10};
unsigned char timecount;
unsigned char disp_bit_count;
/********************************************************************/
unsigned char GetScratchpad[2];
unsigned char code decimalH[16]={00,06,12,18,25,31,37,43,50,56,62,68,75,81,87,93};
unsigned char code 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--;//延时时间:=(8+delay_time*6)us;
}
//*****初始化DS18B20******/
void Initize_One_Wire_Bus(void)
{
One_Wire_Bus=0;
One_Wire_Delay(80);//总线拉低 488us
One_Wire_Bus=1;
One_Wire_Delay(25);//总线拉高 158us;
}
/***********************************************************************/
/*******************读一个字节ds18b20 *********************************/
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);//延时 62 us
if(temp_bit)
result|=0x01<<i;
}
return(result); //返回ds18b20值
}
/*********向ds18b20写一个字节*********************/
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);// 128 us
One_Wire_Bus=1;
oww_dat>>=1;
}
One_Wire_Delay(10);
}
/******************************************/
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;
/***********************************************************************/
void main(void)
{
TMOD=0x01; //使用定时器0,选择方式1(16位定时器)
TH0=(65536-3000)/256; //定时3MS初值
TL0=(65536-3000)%256;
ET0=1; //开定时器0溢出中断
EA=1; //开总中断
Initize_One_Wire_Bus();
TR0=1; //开定时器0
while(TRUE )
{
if(timecount==10) //每30ms采样一次温度可以修改采样时间
{
timecount=0;
Read_18B20();
dispbuff[4]=((ResultTemperatureH%100)/10); //温度十位
dispbuff[3]=(ResultTemperatureH%10); //温度个位
dispbuff[2]=(ResultTemperatureLH/10); //温度小数位
}
}
}
/*********3MS中断服务程序*************/
void t0(void) interrupt 1
{
TH0=(65536-3000)/256;
TL0=(65536-3000)%256;
timecount++;
if(disp_bit_count==3)
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -