读取DS18B20温度通过LCD1602显示出来并输出控制共20页.docx
- 文档编号:11564197
- 上传时间:2023-06-01
- 格式:DOCX
- 页数:17
- 大小:1.84MB
读取DS18B20温度通过LCD1602显示出来并输出控制共20页.docx
《读取DS18B20温度通过LCD1602显示出来并输出控制共20页.docx》由会员分享,可在线阅读,更多相关《读取DS18B20温度通过LCD1602显示出来并输出控制共20页.docx(17页珍藏版)》请在冰点文库上搜索。
读取DS18B20温度通过LCD1602显示出来并输出控制共20页
电路(diànlù)原理图
程序(chéngxù)
1.main.c
#include
#include"lcd.h"
#include"temp.h"
voidLcdDisplay(int);
sbitK1=P2^0;
sbitK2=P2^1;
sbitK3=P2^2;
sbitK4=P2^3;
externintth=20;
externinttl=-10;
sbitbeep=P1^0;
voidmain()
{
LcdInit();
LcdWriteCom(0xc7);
LcdWriteData('C');
while
(1)
{
if(K1==0)
{
Delay1ms(500);
if(K1==0);
th++;
}
if(K2==0)
{
Delay1ms(500);
if(K2==0);
th--;
}
if(K3==0)
{
Delay1ms(500);
if(K3==0);
tl++;
}
if(K4==0)
{
Delay1ms(500);
if(K4==0);
tl--;
}
LcdDisplay(Ds18b20ReadTemp());
}
}
voidLcdDisplay(inttemp)
{
inti,tt,rr,mm;
unsignedchardatas[]={0,0,0,0},datas1[]={0,0,0},datas2[]={0,0,0};
floattp;
if(temp<0)
{
LcdWriteCom(0xc0);
LcdWriteData('-');
i=1;
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*10+0.5;
mm=-temp;
}
else
{
LcdWriteCom(0xc0);
LcdWriteData('+');
tp=temp;
temp=tp*0.0625*10+0.5;
mm=temp;
}
datas[0]=temp/1000;
datas[1]=temp%1000/100;
datas[2]=temp%100/10;
datas[3]=temp%10;
if(th<0)
{
LcdWriteCom(0x89);
LcdWriteData('-');
tt=-th;
}
else
{
LcdWriteCom(0x89);
LcdWriteData('+');
tt=th;
}
datas1[0]=tt/100;
datas1[1]=tt%100/10;
datas1[2]=tt%10;
LcdWriteCom(0x87);
LcdWriteData('H');
LcdWriteCom(0x88);
LcdWriteData(':
');
LcdWriteCom(0x8a);
LcdWriteData('0'+datas1[0]);
LcdWriteCom(0x8b);
LcdWriteData('0'+datas1[1]);
LcdWriteCom(0x8c);
LcdWriteData('0'+datas1[2]);
if(tl<0)
{
LcdWriteCom(0x90);
LcdWriteData('-');
rr=-tl;
}
else
{
LcdWriteCom(0x90);
LcdWriteData('+');
rr=tl;
}
datas2[0]=rr/100;
datas2[1]=rr%100/10;
datas2[2]=rr%10;
LcdWriteCom(0x8e);
LcdWriteData('L');
LcdWriteCom(0x8f);
LcdWriteData(':
');
LcdWriteCom(0x91);
LcdWriteData('0'+datas2[0]);
LcdWriteCom(0x92);
LcdWriteData('0'+datas2[1]);
LcdWriteCom(0x93);
LcdWriteData('0'+datas2[2]);
if(mm>=(th*10)||mm<=(tl*10)||th<=tl)
beep=0;
else
beep=1;
LcdWriteCom(0x80);
LcdWriteData('T');
LcdWriteCom(0x81);
LcdWriteData('A');
LcdWriteCom(0x82);
LcdWriteData('I');
LcdWriteData('');
LcdWriteCom(0x84);
LcdWriteData('A');
LcdWriteCom(0x85);
LcdWriteData('N');
LcdWriteCom(0xc1);
LcdWriteData('0'+datas[0]);
LcdWriteCom(0xc2);
LcdWriteData('0'+datas[1]);
LcdWriteCom(0xc3);
LcdWriteData('0'+datas[2]);
LcdWriteCom(0xc4);
LcdWriteData('.');
LcdWriteCom(0xc5);
LcdWriteData('0'+datas[3]);
LcdWriteCom(0xc6);
LcdWriteData('"');
}
2.lcd.h
#ifndef__LCD_H_
#define__LCD_H_
/**********************************
当使用(shǐyòng)的是4位数据传输的时候定义,
使用8位取消(qǔxiāo)这个定义
**********************************/
#defineLCD1602_4PINS
/**********************************
包含(bāohán)头文件
**********************************/
#include
//---重定义(dìngyì)关键词---//
#ifndefuchar
#defineucharunsignedchar
#endif
#ifndefuint
#defineuintunsignedint
#endif
/**********************************
PIN口定义(dìngyì)
**********************************/
#defineLCD1602_DATAPINSP0
sbitLCD1602_E=P2^7;
sbitLCD1602_RW=P2^5;
sbitLCD1602_RS=P2^6;
/**********************************
函数(hánshù)声明
**********************************/
/*在51单片机12MHZ时钟(shízhōng)下的延时函数*/
voidLcd1602_Delay1ms(uintc);//误差(wùchā)0us
/*LCD1602写入8位命令(mìnglìng)子函数*/
voidLcdWriteCom(ucharcom);
/*LCD1602写入8位数据(shùjù)子函数*/
voidLcdWriteData(uchardat);
/*LCD1602初始化子程序*/
voidLcdInit();
#endif
3.temp.h
#ifndef__TEMP_H_
#define__TEMP_H_
#include
sbitDSPORT=P3^7;
voidDelay1ms(unsignedint);
unsignedcharDs18b20Init();
voidDs18b20WriteByte(unsignedcharcom);
unsignedcharDs18b20ReadByte();
voidDs18b20ChangTemp();
voidDs18b20ReadTempCom();
intDs18b20ReadTemp();
#endif
4.lcd.c
#include"lcd.h"
voidLcd1602_Delay1ms(uintc)//延时
{
uchara,b;
for(;c>0;c--)
{
for(b=19;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
#ifndefLCD1602_4PINS//当没有定义(dìngyì)这个LCD1602_4PINS时
voidLcdWriteCom(ucharcom)//写入一个(yīɡè)字节命令
{
LCD1602_E=0;
LCD1602_RS=0;
LCD1602_RW=0;
LCD1602_DATAPINS=com;
Lcd1602_Delay1ms
(1);
LCD1602_E=1;
Lcd1602_Delay1ms(5);
LCD1602_E=0;
}
#else
voidLcdWriteCom(ucharcom)
{
LCD1602_E=0;
LCD1602_RS=0;
LCD1602_RW=0;
LCD1602_DATAPINS=com;//由于4位的接线是接到P0口的高四位,所以(suǒyǐ)传送高四位不用改
Lcd1602_Delay1ms
(1);
LCD1602_E=1;
Lcd1602_Delay1ms(5);
LCD1602_E=0;
LCD1602_DATAPINS=com<<4;//发送(fāsònɡ)低四位
Lcd1602_Delay1ms
(1);
LCD1602_E=1;
Lcd1602_Delay1ms(5);
LCD1602_E=0;
}
#endif
#ifndefLCD1602_4PINS
voidLcdWriteData(uchardat)//写入一个字节(zìjié)数据
{
LCD1602_E=0;
LCD1602_RS=1;
LCD1602_RW=0;
LCD1602_DATAPINS=dat;
Lcd1602_Delay1ms
(1);
LCD1602_E=1;
Lcd1602_Delay1ms(5);
LCD1602_E=0;
}
#else
voidLcdWriteData(uchardat)
{
LCD1602_E=0;
LCD1602_RS=1;
LCD1602_RW=0;
LCD1602_DATAPINS=dat;//由于4位的接线是接到P0口的高四位,所以传送(chuánsònɡ)高四位不用改
Lcd1602_Delay1ms
(1);
LCD1602_E=1;//写入时序(shíxù)
Lcd1602_Delay1ms(5);
LCD1602_E=0;
LCD1602_DATAPINS=dat<<4;//写入低四位(sìwèi)
Lcd1602_Delay1ms
(1);
LCD1602_E=1;//写入时序(shíxù)
Lcd1602_Delay1ms(5);
LCD1602_E=0;
}
#endif
#ifndefLCD1602_4PINS
voidLcdInit()//LCD初始化子程序
{
LcdWriteCom(0x38);
LcdWriteCom(0x0c);
LcdWriteCom(0x06);
LcdWriteCom(0x01);
LcdWriteCom(0x80);
}
#else
voidLcdInit()
{
LcdWriteCom(0x32);
LcdWriteCom(0x28);
LcdWriteCom(0x0c);
LcdWriteCom(0x06);
LcdWriteCom(0x01);
LcdWriteCom(0x80);
}
#endif
5.temp.c
#include"temp.h"
voidDelay1ms(unsignedinty)//延时
{
unsignedintx;
for(y;y>0;y--)
for(x=110;x>0;x--);
}
unsignedcharDs18b20Init()//初始化
{
unsignedinti;
DSPORT=0;
i=70;
while(i--);
DSPORT=1;
i=0;
while(DSPORT)
{
i++;
if(i>5000)
return0;//失败(shībài)
}
return1;//成功(chénggōng)
}
voidDs18b20WriteByte(unsignedchardat)//写字节(zìjié)
{
unsignedinti,j;
for(j=0;j<8;j++)
{
DSPORT=0;//每写入一位数据之前(zhīqián)先把总线拉低1us(数据手册上模糊)
i++;
DSPORT=dat&0x01;
i=6;
while(i--);
DSPORT=1;
dat>>=1;
}
}
unsignedcharDs18b20ReadByte()
{
unsignedcharbyte,bi;
unsignedinti,j;
for(j=8;j>0;j--)
{
DSPORT=0;
i++;
DSPORT=1;
i++;
i++;
bi=DSPORT;
byte=(byte>>1)|(bi<<7);
i=4;
while(i--);
}
returnbyte;
}
voidDs18b20ChangTemp()//温度(wēndù)转换
{
Ds18b20Init();
Delay1ms
(1);
Ds18b20WriteByte(0xcc);
Ds18b20WriteByte(0x44);
}
voidDs18b20ReadTempCom()//读取温度(wēndù)命令
{
Ds18b20Init();
Delay1ms
(1);
Ds18b20WriteByte(0xcc);
Ds18b20WriteByte(0xbe);
}
intDs18b20ReadTemp()//读取温度(wēndù)
{
inttemp=0;
unsignedchartmh,tml;
Ds18b20ChangTemp();
Ds18b20ReadTempCom();
tml=Ds18b20ReadByte();
tmh=Ds18b20ReadByte();
temp=tmh;
temp<<=8;
temp|=tml;
returntemp;
}
内容摘要
(1)电路原理图
程序
1.main.c
#include
#include"lcd.h"
#include"temp.h"
voidLcdDisplay(int)
(2)voidmain()
{
LcdInit()
(3)}
LcdDisplay(Ds18b20ReadTemp())
(4)if(temp<0)
{
LcdWriteCom(0xc0)
(5)if(tl<0)
{
LcdWriteCom(0x90)
(6)}
#else
voidLcdInit()
{
LcdWriteCom(0x32)
(7)dat>>=1
(8)temp<<=8
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 读取 DS18B20 温度 通过 LCD1602 显示 出来 输出 控制 20