基于AT89C51单片机和DS18B20温度传感器、LCD1602液晶显示的高精度数字温度计程序,用C语言编程

2024-11-08 19:18:20
推荐回答(2个)
回答(1):

/***********ds18b20子程序*************************/

/***********ds18b20延迟子函数(晶振12MHz )*******/
#include
sbit DQ=P1^2;
#define uchar unsigned char

void delay_18B20(unsigned int i)
{
while(i--);
}

/**********ds18b20初始化函数**********************/

void Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ复位
delay_18B20(8); //稍做延时
DQ = 0; //单片机将DQ拉低
delay_18B20(80); //精确延时 大于 480us
DQ = 1; //拉高总线
delay_18B20(4);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay_18B20(20);
}

/***********ds18b20读一个字节**************/

unsigned char ReadOneChar(void)
{
uchar i=0;
uchar dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
delay_18B20(4);
}
return(dat);
}

/*************ds18b20写一个字节****************/

void WriteOneChar(uchar dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay_18B20(5);
DQ = 1;
dat>>=1;
}
}

/**************读取ds18b20当前温度************/

ReadTemp(void)
{ float val;
uchar temp_value,value;
unsigned char a=0;
unsigned char b=0;
unsigned char t=0;

Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换

delay_18B20(100); // this message is wery important

Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度

delay_18B20(100);

a=ReadOneChar(); //读取温度值低位
b=ReadOneChar(); //读取温度值高位
temp_value=b<<4;
temp_value+=(a&0xf0)>>4;
value=a&0x0f;
val=temp_value+value;
return(val);

}
以上是DS18B20的驱动程序。然后在主程序中直接调用函数就可以了。
以上是在主程序中的调用,你看关于温度的那个就可以。
#include "reg51.h"
#include "18b20.h"
#define uchar unsigned char
#define uint unsigned int
uchar code table[]="Welcome To" ;//初始化日期和星期
uchar code table1[]="Our System!";//初始化时间

uchar code table2[]="Temperature is:";//初始化时间

//以下三个是定义LCD的引脚
sbit lcden=P2^2;
sbit lcdwrite=P2^1;
sbit lcdrs=P2^0;

char wendu;

//延时程序
void delay(uint z)
{ uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}

//lcd的写指令
void write_com(uchar com)
{
lcdrs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}

//lcd的写数据
void write_data(uchar da)
{ lcdrs=1;
lcden=0;
P0=da;
delay(5);
lcden=1;
delay(5);
lcden=0;
}

//初始化
void init()
{
uchar num;

lcdwrite=0;
lcden=0;
write_com(0x38); //16*2显示,5*7点阵,8位数据
write_com(0x0c);//显示开,关光标
write_com(0x06); //移动光标
write_com(0x01);//清除LCD的显示内容

write_com(0x80);
for (num=0;num<10;num++)
{
write_data(table[num]);
delay(5);
}

write_com(0x80+0x40);

for (num=0;num<11;num++)
{
write_data(table1[num]);
delay(5);
}

}

void write_wendu(uchar add, char da)
{
uchar shi,ge;
shi=da/10;
ge=da%10;
write_com(0x80+0x40+add);
write_data(0x30+shi);
write_data(0x30+ge);
}

//主函数
void main ()
{

uchar num;

init();
delay(2000);

delay(2000);
delay(2000);

write_com(0x01);//清除LCD的显示内容

while(1)
{

write_com(0x80);
for (num=0;num<15;num++)
{
write_data(table2[num]);
delay(5);
}

wendu = ReadTemp( );
write_wendu(2,wendu);
}
}

回答(2):

#include
#include
#include

#define uchar unsigned char
#define uint unsigned int
sbit dula = P2^6;
sbit wela = P2^7;

sbit RS = P3^5;
sbit LCDEN = P3^4;

void delayUs()
{
_nop_();
}

void delayMs(uint a)
{
uint i, j;
for(i = a; i > 0; i--)
for(j = 100; j > 0; j--);
}

void writeComm(uchar comm)
{
RS = 0;
P0 = comm;
LCDEN = 1;
delayUs();
LCDEN = 0;
delayMs(1);
}

//写数据:RS=1, RW=0;
void writeData(uchar dat)
{
RS = 1;
P0 = dat;
LCDEN = 1;
delayUs();
LCDEN = 0;
delayMs(1);
}

void init()
{

dula = wela = 0;
writeComm(0x38);
writeComm(0x0c);
writeComm(0x06);
writeComm(0x01);
}

void writeString(uchar * str, uchar length)
{
uchar i;
for(i = 0; i < length; i++)
{
writeData(str[i]);
}
}

/**//*****************************DS18B20*******************************/
sbit ds = P2^2;
void dsInit()
{

unsigned int i;
ds = 0;
i = 100;
while(i>0) i--;
ds = 1;
i = 4;
while(i>0) i--;
}

void dsWait()
{
unsigned int i;
while(ds);
while(~ds);
i = 4;
while(i > 0) i--;
}

bit readBit()
{
unsigned int i;
bit b;
ds = 0;
i++;
ds = 1;
i++; i++;
b = ds;
i = 8;
while(i>0) i--;
return b;
}

unsigned char readByte()
{
unsigned int i;
unsigned char j, dat;
dat = 0;
for(i=0; i<8; i++)
{
j = readBit();

dat = (j << 7) | (dat >> 1);
}
return dat;
}

void writeByte(unsigned char dat)
{
unsigned int i;
unsigned char j;
bit b;
for(j = 0; j < 8; j++)
{
b = dat & 0x01;
dat >>= 1;

if(b)
{
ds = 0; i++; i++;
ds = 1;
i = 8; while(i>0) i--;
}
else
{
ds = 0;
i = 8; while(i>0) i--;
ds = 1;
i++; i++;
}
}
}

void sendChangeCmd()
{
dsInit();
dsWait();
delayMs(1);
writeByte(0xcc);
writeByte(0x44);
}

void sendReadCmd()
{
dsInit();
dsWait();
delayMs(1);
writeByte(0xcc);
writeByte(0xbe);
}

int getTmpValue()
{
unsigned int tmpvalue;
int value;
float t;
unsigned char low, high;
sendReadCmd();

low = readByte();
high = readByte();

tmpvalue = high;
tmpvalue <<= 8;
tmpvalue |= low;
value = tmpvalue;

\
t = value * 0.0625;
\
value = t * 100 + (value > 0 ? 0.5 : -0.5); //大于0加0.5, 小于0减0.5
return value;
}

void display(int v)
{
unsigned char count;
unsigned char datas[] = {0, 0, 0, 0, 0};
unsigned int tmp = abs(v);
datas[0] = tmp / 10000;
datas[1] = tmp % 10000 / 1000;
datas[2] = tmp % 1000 / 100;
datas[3] = tmp % 100 / 10;
datas[4] = tmp % 10;
writeComm(0xc0+3);
if(v < 0)
{
writeString("- ", 2);
}
else
{
writeString("+ ", 2);
}
if(datas[0] != 0)
{
writeData('0'+datas[0]);
}
for(count = 1; count != 5; count++)
{
writeData('0'+datas[count]);
if(count == 2)
{
writeData('.');
}
}
}
/**//*****************************DS18B20*******************************/

void main()
{
uchar table[] = " xianzaiwendu:";
sendChangeCmd();
init();
writeComm(0x80);
writeString(table, 16);
while(1)
{
delayMs(1000); //温度转换时间需要750ms以上
writeComm(0xc0);
display(getTmpValue());
sendChangeCmd();
}
}