完整版 电路图和程序代码 下载地址
https://pan.baidu.com/s/1qRy5cm2qqGUJuPsZEJi0CA?pwd=8888
部分代码展示
#include <reg51.h> /* 导入库文件 */
#define uchar unsigned char /* 宏定义 */
#define uint unsigned int /* 宏定义 */
sbit ds = P2 ^ 7;
/* 温度采集 */
sbit bemp = P3 ^ 7;
/* 报警蜂鸣器 */
sbit lcden = P2 ^ 2;
/* lcd使能 */
sbit lcdrs = P2 ^ 0;
/* lcd复位 */
sbit lcdrw = P2 ^ 1;
/* lcd数据写入 */
sbit sh_zeng = P1 ^ 7;
/*上限增加按键 */
sbit sh_jian = P1 ^ 6;
/*上限减少按键 */
sbit xia_zeng = P1 ^ 5;
/*下限增加按键 */
sbit xia_jian = P1 ^ 4;
/*下限减少按键 */
sbit dianji = P3 ^ 0;
/* 散热电机 */
sbit heat = P3 ^ 1;
/* 加热模块 */
sbit zhengchang = P1 ^ 2;
/* 正常温度led */
uchar code table1[] = "temp:";
/* 一些是液晶显示初始化 */
uchar code table2[] = "up:";
uchar code table3[] = "down:";
uchar code table4[] = "0123456789.-";
uchar bai, shi, ge, flag, num, shi_sh, shi_xia, ge_sh, ge_xia;
/* 变量定义 */
int temp;
/* 初始化温度上下限 */
int warn_sh = 27;
int warn_xia = 18;
void delayms( uint z )
{
uint x, y;
for ( x = z; z > 0; z-- )
for ( y = 110; y > 0; y-- )
;
}
void dsreset() /* DS18B20初始化 */
{
uint i;
ds = 0;
i = 103;
while ( i > 0 )
i--;
ds = 1;
i = 1;
while ( i > 0 )
i--;
}
bit tempreadbit( void ) /* 读一位数据函数 */
{
uint i;
bit dat;
ds = 0; i++; /* 延时 */
ds = 1; i++; i++;
dat = ds;
i = 8; while ( i > 0 )
i--;
return(dat);
}
uchar tempread( void )
{
uchar i, j, dat;
dat = 0;
for ( i = 1; i <= 8; i++ )
{
j = tempreadbit();
dat = (j << 7) | (dat >> 1); /* 将位转换为字节 */
}
return(dat);
}
void tempwrite( uchar dat ) /* 向DS18B20写一个字节数据函数 */
{
uint i;
uchar j;
bit testb;
for ( j = 1; j <= 8; j++ )
{
testb = dat & 0x01;
dat = dat >> 1;
if ( testb )
{
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 tempchange() /* DS18B20开始获取温度并转换 */
{
dsreset();
delayms( 1 );
tempwrite( 0xcc ); /* 写跳过读ROM指令 */
tempwrite( 0x44 ); /* 写温度转换指令 */
}
void get_temp() /* 读取寄存器中存储的温度数据 */
{
uchar a, b;
dsreset();
delayms( 1 );
tempwrite( 0xcc );
tempwrite( 0xbe );
a = tempread();
b = tempread();
temp = b;
temp <<= 8;
temp = temp | a;
if ( !(b & 0xf8) )
temp = temp * 0.625; /* 温度在寄存器中为12为,分辨率为0.0625,加0.5是为了四舍五入 */
else{
temp = (~temp + 1) * 0.625;
flag = 1;
}
}
void write_com( uchar com )
{
lcdrs = 0;
P0 = com;
delayms( 5 );
lcden = 1;
delayms( 5 );
lcden = 0;
}
void write_date( uchar date )
{
lcdrs = 1;
P0 = date;
delayms( 5 );
lcden = 1;
delayms( 5 );
lcden = 0;
}
void init_1602()
{
}