作业帮 > 综合 > 作业

DS18B20能直接测出环境温度吗?它的输出信号需要先经过AD转换后再送入单片机么?

来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/06 19:28:50
DS18B20能直接测出环境温度吗?它的输出信号需要先经过AD转换后再送入单片机么?
因为我测人体温度(用的红外)要先知道环境温度
DS18B20能直接测出环境温度吗?它的输出信号需要先经过AD转换后再送入单片机么?
不用经过AD,单片机只需通过一个I/O口就可以把DS18B20的数据读出来,主要是要编程.
把程序写给你吧.程序里面的数组数据是我自己接的数码管显示数字数据,你应用时注意改就好.DS与单片机的接口也能自己决定,不必和我一样.
//安装目录下的EXE文件打开后可在电脑上显示当前温度值
#include
#define uchar unsigned char
#define uint unsigned int
sbit DS=P1^0; //define interface
uint temp; // variable of temperature
uchar flag1; // sign of the result positive or negative
//sbit dula=P2^6;
//sbit wela=P2^7;
//unsigned char code table2[]={0x3f,0x30,0x5b,0x4f,0x66,0x6d,0x7d,
// 0x07,0x7f,0x67/*,0x77,0x7c,0x39,0x5e,0x79,0x71*/};
//unsigned char code table1[]={0xbf,0xb0,0xdb,0xcf,0xe6,0xed,0xfd,
// 0x87,0xff,0xe7};
//unsigned char code table2[]={0x40,0x5e,0x24,0x21,0x13,0x09,0x08,
// 0x63,0x00,0x03/*,0x77,0x7c,0x39,0x5e,0x79,0x71*/};
//unsigned char code table1[]={0xc0,0xde,0xa4,0xa1,0x93,0x89,0x88,
// 0xe7,0x8f,0x87};
unsigned char code table5[]={0x77,0x22,0x6d,0x5d,0x1e,0x5b,0x7b,
0x15,0x7f,0x1f};
unsigned char code table6[]={0xf7,0xa2,0xed,0xdd,0x9e,0xdb,0xfb,
0x95,0xff,0x9f};
void delay(uint count) //delay
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
///////功能:串口初始化,波特率9600,方式1///////
void Init_Com(void)
{
TMOD = 0x20;
PCON = 0x00;
SCON = 0x50;
TH1 = 0xFd;
TL1 = 0xFd;
TR1 = 1;
}
void dsreset(void) //send reset and initialization command 18B20复位,初始化函数
{
uint i;
DS=0;
i=103;
while(i>0)i--;
DS=1;
i=4;
while(i>0)i--;
}
bit tmpreadbit(void) //read a bit 读1位数据函数
{
uint i;
bit dat;
DS=0;i++; //i++ for delay
DS=1;i++;i++;
dat=DS;
i=8;while(i>0)i--;
return (dat);
}
uchar tmpread(void) //read a byte date 读1字节函数
{
uchar i,j,dat;
dat=0;
for(i=1;i1;
if(testb) //write 1
{
DS=0;
i++;i++;
DS=1;
i=8;while(i>0)i--;
}
else
{
DS=0; //write 0
i=8;while(i>0)i--;
DS=1;
i++;i++;
}
}
}
void tmpchange(void) //DS18B20 begin change 开始获取数据并转换
{
dsreset();
delay(1);
tmpwritebyte(0xcc); // address all drivers on bus 写跳过读ROM指令
tmpwritebyte(0x44); // initiates a single temperature conversion 写温度转换指令
}
uint tmp() //get the temperature 读取寄存器中存储的温度数据
{
float tt;
uchar a,b;
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpread(); //读低8位
b=tmpread(); //读高8位
temp=b;
temp0;b--);
}
void display(uint temp)//显示程序
{
uchar A1,A2,A2t,A3,ser;
ser=temp/10;
SBUF=ser;
A1=temp/100;
A2t=temp%100;
A2=A2t/10;
A3=A2t%10;
//dula=0;
d4=0;
P0=table5[A1];
delay(1);
d4=1;//显示百位
//dula=1;
// dula=0;
/*wela=0;
P0=0x7e;
wela=1;
wela=0;*/

//dula=0;
d3=0;
P0=table6[A2];
delay(1);
d3=1;//显示十位
//dula=1;
//dula=0;
/*wela=0;
P0=0x7d;
wela=1;
wela=0; */

d2=0;
P0=table5[A3];
delay(1);
d2=1;//显示个位
//dula=1;
//dula=0;
/*P0=0x7b;
wela=1;
wela=0; */

}
void main()
{
uchar a;
Init_Com();
do
{
tmpchange();
// delay(200);
for(a=1;a>0;a--)
{ display(tmp());
}
} while(1);
}
//安装目录下的EXE文件打开后可在电脑上显示当前温度值
#include
#define uchar unsigned char
#define uint unsigned int
sbit DS=P1^0; //define interface
uint temp; // variable of temperature
uchar flag1; // sign of the result positive or negative
//sbit dula=P2^6;
//sbit wela=P2^7;
//unsigned char code table2[]={0x3f,0x30,0x5b,0x4f,0x66,0x6d,0x7d,
// 0x07,0x7f,0x67/*,0x77,0x7c,0x39,0x5e,0x79,0x71*/};
//unsigned char code table1[]={0xbf,0xb0,0xdb,0xcf,0xe6,0xed,0xfd,
// 0x87,0xff,0xe7};
//unsigned char code table2[]={0x40,0x5e,0x24,0x21,0x13,0x09,0x08,
// 0x63,0x00,0x03/*,0x77,0x7c,0x39,0x5e,0x79,0x71*/};
//unsigned char code table1[]={0xc0,0xde,0xa4,0xa1,0x93,0x89,0x88,
// 0xe7,0x8f,0x87};
unsigned char code table5[]={0x77,0x22,0x6d,0x5d,0x1e,0x5b,0x7b,
0x15,0x7f,0x1f};
unsigned char code table6[]={0xf7,0xa2,0xed,0xdd,0x9e,0xdb,0xfb,
0x95,0xff,0x9f};
void delay(uint count) //delay
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
///////功能:串口初始化,波特率9600,方式1///////
void Init_Com(void)
{
TMOD = 0x20;
PCON = 0x00;
SCON = 0x50;
TH1 = 0xFd;
TL1 = 0xFd;
TR1 = 1;
}
void dsreset(void) //send reset and initialization command 18B20复位,初始化函数
{
uint i;
DS=0;
i=103;
while(i>0)i--;
DS=1;
i=4;
while(i>0)i--;
}
bit tmpreadbit(void) //read a bit 读1位数据函数
{
uint i;
bit dat;
DS=0;i++; //i++ for delay
DS=1;i++;i++;
dat=DS;
i=8;while(i>0)i--;
return (dat);
}
uchar tmpread(void) //read a byte date 读1字节函数
{
uchar i,j,dat;
dat=0;
for(i=1;i1;
if(testb) //write 1
{
DS=0;
i++;i++;
DS=1;
i=8;while(i>0)i--;
}
else
{
DS=0; //write 0
i=8;while(i>0)i--;
DS=1;
i++;i++;
}
}
}
void tmpchange(void) //DS18B20 begin change 开始获取数据并转换
{
dsreset();
delay(1);
tmpwritebyte(0xcc); // address all drivers on bus 写跳过读ROM指令
tmpwritebyte(0x44); // initiates a single temperature conversion 写温度转换指令
}
uint tmp() //get the temperature 读取寄存器中存储的温度数据
{
float tt;
uchar a,b;
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpread(); //读低8位
b=tmpread(); //读高8位
temp=b;
temp0;b--);
}
void display(uint temp)//显示程序
{
uchar A1,A2,A2t,A3,ser;
ser=temp/10;
SBUF=ser;
A1=temp/100;
A2t=temp%100;
A2=A2t/10;
A3=A2t%10;
//dula=0;
d4=0;
P0=table5[A1];
delay(1);
d4=1;//显示百位
//dula=1;
// dula=0;
/*wela=0;
P0=0x7e;
wela=1;
wela=0;*/

//dula=0;
d3=0;
P0=table6[A2];
delay(1);
d3=1;//显示十位
//dula=1;
//dula=0;
/*wela=0;
P0=0x7d;
wela=1;
wela=0; */

d2=0;
P0=table5[A3];
delay(1);
d2=1;//显示个位
//dula=1;
//dula=0;
/*P0=0x7b;
wela=1;
wela=0; */

}
void main()
{
uchar a;
Init_Com();
do
{
tmpchange();
// delay(200);
for(a=1;a>0;a--)
{ display(tmp());
}
} while(1);
}
再问: 非非常感谢你的程序,如果用LCD1602显示的话该怎么改啊。。
再答: 下面的程序是简单地显示数字和字母的LCD显示程序。你在主函数那里改下你需要显示的东西就行了。最近有些忙,就没时间帮你详细到每个步骤了。剩下的你就自己弄吧。 #include #define uchar unsigned char #define uint unsigned int sbit rs=P3^5; sbit lcden=P3^4; sbit dula=P2^6; sbit wela=P2^7; sbit rw=P3^6; uchar table1[]="CC-DZ MCU"; uchar table2[]="1234567654321"; void delay(uint x) { uint a,b; for(a=x;a>0;a--) for(b=10;b>0;b--); } void delay1(uint x) { uint a,b; for(a=x;a>0;a--) for(b=100;b>0;b--); } void write_com(uchar com) { P0=com; rs=0; rw = 0; lcden=0; delay(10); lcden=1; delay(10); lcden=0; } void write_date(uchar date) { P0=date; rs=1; rw = 0; lcden=0; delay(10); lcden=1; delay(10); lcden=0; } void init() { rw=0; dula=0; wela=0; write_com(0x38); delay(20); write_com(0x0f); delay(20); write_com(0x06); delay(20); write_com(0x01); delay(20); } void main() { uchar a; init(); write_com(0x80+17); delay(20); for(a=0;a