📄 ds18b20的应用.c
字号:
#include<reg51.h>
#include<intrins.h>
#include<absacc.h>
#define uchar unsigned char
#define uint unsigned int
sbit DQ = P2^0; //定义DS18B20端口DQ
sbit RS = P3^5;
sbit RW = P3^6;
sbit E = P3^7;
uchar temp_data_l,temp_data_h;
uchar code LCDData[10]= {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
uchar code ditab[16] = {0x30,0x31,0x31,0x32,0x33,0x33,0x34,0x34,0x35,0x36,0x36,0x37,0x38,0x38,0x39,0x39};
uchar code table2[16] = {0x74,0x65,0x6d,0x70,0x65,0x72,0x61,0x74,0x75,0x72,0x65,0x20,0x69,0x53,0x20,0x3a};
uchar display[7] = {0x00,0x00,0x00,0x2e,0x00,0xdf,0x43};
//*********************************
// 延时程序
//*********************************
void delay(uint N)
{
uint i;
for(i=0;i<N;i++);
}
//**********************************
// 初始化ds18B20程序
//**********************************
bit resetpulse(void)
{
DQ=0;
//delay600us(); //延时500us
delay(40); //延时500us
DQ=1;
//delay60us(); // 延时60us
delay(4); // 延时60us
return(DQ); //读取P1.0的状态
}
/*************************************************************
**功能:ds18b20的初始化 **
**参数:无 **
*************************************************************/
void ds18b20_init(void)
{
while(1)
{
if(!resetpulse()) //收到ds18b20的应答信号
{
DQ=1;
//delay600us(); //延时240us
delay(40); //延时240us
break;
}
else
resetpulse(); //否则再发复位信号
}
}
//************************************
// 读一位程序
//************************************
uchar read_bit(void)
{
// uchar i;
DQ=0;
_nop_();
_nop_();
//_nop_();
//_nop_();
DQ=1;
delay(2);
//for(i=0;i<5;i++);
return(DQ);
}
//*************************************
// 读一个字节程序
//*************************************
uchar read_byte(void)
{
uchar i,m,receive_data;
m=1;
receive_data=0;
for(i=0;i<8;i++)
{
if(read_bit())
{
receive_data=receive_data+(m<<i);
}
delay(7);
}
return(receive_data);
}
//**************************************
// 写一位程序
//**************************************
void write_bit(uchar bitval)
{
DQ=0;
if(bitval==1)
DQ=1;
delay(5);
DQ=1;
}
//***************************************
// 向DS18B20写一个字节命令程序
//***************************************
void write_byte(uchar val)
{
uchar i,temp;
for(i=0;i<8;i++)
{
temp=val>>i;
temp=temp&0x01;
write_bit(temp);
delay(5);
}
}
//*****************************************
// 启动温度转换及读出温度值
//*****************************************
void read_T(void)
{
ds18b20_init();
write_byte(0xCC); // 跳过读序号列号的操作
write_byte(0x44); // 启动温度转换
delay(500);
ds18b20_init();
write_byte(0xCC); //跳过读序号列号的操作
write_byte(0xBE); //读取温度寄存器
temp_data_l= read_byte(); //温度低8位
temp_data_h = read_byte(); //温度高8位
}
/*************************************************************
**功能:判断LCD忙 **
**参数:无 **
*************************************************************/
void check_busy(void)
{
while(1)
{
P1=0xff;
E=0;
_nop_();
RS=0;
_nop_();
_nop_();
RW=1;
_nop_();
_nop_();
E=1;
_nop_();
_nop_();
_nop_();
_nop_();
if((P1&0x80)==0)
{
break;
}
E=0;
}
}
//******************************************
// 将数据码写入LCD数据寄存器
//******************************************
void write_command(uchar tempdata)
{
E=0;
_nop_();
_nop_();
RS=0;
_nop_();
_nop_();
RW=0;
P1=tempdata;
_nop_();
_nop_();
E=1;
_nop_();
_nop_();
E=0;
_nop_();
check_busy();
}
//*******************************************
// 写LCD1602使能程序
//*******************************************
void write_data(uchar tempdata)
{
E=0;
_nop_();
_nop_();
RS=1;
_nop_();
_nop_();
RW=0;
P1=tempdata;
_nop_();
_nop_();
E=1;
_nop_();
_nop_();
E=0;
_nop_();
check_busy();
}
//*******************************************
// 温度处理及显示
//*******************************************
void convert_T()//显示温度
{
uchar temp;
if((temp_data_h&0xf0)==0xf0)
{
temp_data_l=~temp_data_l;
if(temp_data_l==0xff)
{
temp_data_l=temp_data_l+0x01;
temp_data_h=~temp_data_h;
temp_data_h=temp_data_h+0x01;
}
else
{
temp_data_l=temp_data_l+0x01;
temp_data_h=~temp_data_h;
}
display[4]=ditab[temp_data_l&0x0f]; //查表得小数位的值
temp=((temp_data_l&0xf0)>>4)|((temp_data_h&0x0f)<<4);
display[0]=0x2d;
display[1]=LCDData[(temp%100)/10];
display[2]=LCDData[(temp%100)%10];
}
else
{
display[4]=ditab[temp_data_l&0x0f]; //查表得小数位的值
temp=((temp_data_l&0xf0)>>4)|((temp_data_h&0x0f)<<4);
display[0]=LCDData[temp/100];
display[1]=LCDData[(temp%100)/10];
display[2]=LCDData[(temp%100)%10];
}
}
//********************************************
// 初始化LCD1602
//********************************************
void init()
{
write_command(0x01);
write_command(0x38);
write_command(0x0C);
write_command(0x06);
}
//********************************************
// 显示子程序
//********************************************
void display_T(void)
{
uchar i;
write_command(0x80);
for(i=0;i<16;i++)
{
write_data(table2[i]);
}
write_command(0xc0);
for(i=0;i<7;i++)
{
write_data(display[i]);
}
}
//********************************************
// 主函数
//********************************************
void main(void)
{
init();
while(1)
{
read_T();
convert_T();
display_T();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -