📄 ds18b20+-
字号:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit rs=P2^0;
sbit rw=P2^1;
sbit en=P2^2;
sbit DQ=P3^3;
bit DS_OK=1; //温度传感器正常标志
uchar temp[]={0x00,0x00}; //用于存储高字节与低字节
uchar display_digit[]={0,0,0,0};//存储显示温度字符
uchar current=0; //存储整数(高字节低三位与低字节高四位)
uchar temp_display[]={"TEMP: "};
uchar code tab0[]={"welcome to here"};
uchar code tab1[]={"----sky"};
uchar code title[]={"THE TEMPPERATURE:"};
uchar code df_tab[]={0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9};
void delay(uint z)
{
uint x;
while(z--)
for(x=120;x>0;x--);
}
void delay_us()
{_nop_();_nop_();_nop_();_nop_();}
void delay_us1(uint a)
{
while(a--);
}
uchar LCD_check_busy()
{
uchar state;
rs=0;
rw=1;
delay_us();
en=1;
state=P0;
delay_us();
en=0;
delay_us();
return state;
}
void LCD_write_cmd(uchar cmd)
{
while((LCD_check_busy()&0x80)==0x80);
rs=0;
rw=0;
delay_us();
en=1;
P0=cmd;
delay_us();
en=0;
delay_us();
}
void LCD_write_data(uchar dat)
{
while((LCD_check_busy()&0x80)==0x80);
rs=1;
rw=0;
delay_us();
en=1;
P0=dat;
delay_us();
en=0;
delay_us();
}
void LCD_display(uchar *s)
{
uchar i;
for(i=0;i<16;i++)
{
LCD_write_data(s[i]);
}
}
void LCD_init()
{
LCD_write_cmd(0x38);
LCD_write_cmd(0x0c);
LCD_write_cmd(0x06);
LCD_write_cmd(0x01);
}
//LCD*************************************************
uchar DS_check() //检查温度传感器
{
uchar state;
DQ=1;
delay_us1(8); //精确定时
DQ=0;
delay_us1(80);
DQ=1;
delay_us1(8);
state=DQ;
delay(100);
return state;
}
void DS_write_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
if((dat&0x01)==0) //写0
{
DQ=0;
delay_us1(4);
DQ=1;
delay_us1(1);
}
else //写1
{
DQ=0;
delay_us1(1);
DQ=1;
delay_us1(4);
}
dat>>=1;
}
}
uchar DS_read_byte() //读一个字节
{
uchar i,dat=0;
for(i=0;i<8;i++)
{
DQ=0;
dat>>=1;
DQ=1;
if(DQ==1)
dat|=0x80;
else
dat|=0x00;
delay_us1(30);
DQ=1;
}
return dat;
}
void DS_read_temperature()
{
if(DS_check()==1) //检查初始化温度传感器
DS_OK=0;
else
{
DS_check();
DS_write_byte(0xcc); //跳过序列号
DS_write_byte(0x44); //启动温度转换
DS_check(); //重新检查温度传感器
DS_write_byte(0xcc);//
DS_write_byte(0xbe);//启动读取温度
/****先读低8位,再读高8位,不可调换************/
temp[0]=DS_read_byte();
temp[1]=DS_read_byte();
DS_OK=1; //正常工作
}
}
void LCD_display_temperature()
{
uchar flag=0;//负数标识
/***高5位为1(0XF8)则为负数,为负数时取反加一****/
if((temp[1]&0xf8)==0xf8)
{
temp[1]=~temp[1]; //负数取反
temp[0]=~temp[0]+1;
if(temp[0]==0x00)
temp[1]++;
flag=1;//负数标识
}
/****温度小数部分*********/
display_digit[3]=df_tab[temp[0]&0x0f];
/*温度整数部分(高字节低4位低字节高4位,无符号)***/
current=((temp[1]&0x0f)<<4)|((temp[0]&0xf0)>>4);
//分解整数部分
display_digit[0]=current/100;
display_digit[1]=current%100/10;
display_digit[2]=current%10;
/***刷新LCD显示缓冲***/
temp_display[11]=display_digit[3]+'0';
temp_display[10]='.';
temp_display[9]=display_digit[2]+'0';
temp_display[8]=display_digit[1]+'0';
temp_display[7]=display_digit[0]+'0';
/*百位为0时不显示*/
if(display_digit[0]==0)
temp_display[7]=' ';
/*百位为0,且十位为0时不显示 */
if((display_digit[0]==0)&(display_digit[1]==0))
temp_display[8]=' ';
// 负号显示在恰当位置
if(flag)
{
if(temp_display[8]==' ')
temp_display[8]='-';
else
{
if(temp_display[7]==' ')
temp_display[7]='-';
else
temp_display[6]='-';
}
}
LCD_write_cmd(0x80+0x00);
LCD_display(title);
LCD_write_cmd(0x80+0x40);
LCD_display(temp_display);
LCD_write_cmd(0x80+0x4e);
LCD_write_data('C');
LCD_write_cmd(0x80+0x4f);
LCD_write_data(0x00); //LCD最后一位不显示,可以不要
}
void main()
{
uchar i;
LCD_init();
LCD_write_cmd(0x80+0x00);
for(i=0;i<16;i++)
{
LCD_write_data(tab0[i]);
delay(20);
}
LCD_write_cmd(0x80+0x48);
for(i=0;i<8;i++)
{
LCD_write_data(tab1[i]);
delay(20);
}
delay(1000);
for(i=0;i<16;i++)
{
LCD_write_cmd(0x1c);
delay(20);
}
LCD_write_cmd(0x01);
while(1)
{
DS_read_temperature();
if(DS_OK)
LCD_display_temperature();
delay(100);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -