1602液晶显示温度求解,温度可以变化,但是第一位是乱码
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uint temp,k;
uchar Tempdata[5];
sbit DS=P3^6;
sbit lcden=P3^4;
sbit rs=P3^2;
sbit rw=P3^3;
uchar code table[]={0x30,0x31,0x32,
0x33,0x34,0x35,0x36,0x37,0x38,0x39}; //液晶内部字符编码表0-9
void delay(uint count)
{
uint i,j;
for(i=count;i>0;i--)
for(j=103;j>0;j--);
}
void dsreset() //复位18B20
{
DS=0;
delay(1);
DS=1;
delay(1);
}
bit tmpreadbit() //从18B20读一位
{
uint i;
bit dat;
DS=0;i++;
DS=1;i++;i++;
dat=DS;
i=8;while(i>0)i--;
return dat;
}
uchar tmpreadbyte() //从18B20读一个字节
{
uchar j,i,dat;
dat=0;
for(i=0;i<8;i++)
{
j=tmpreadbit();
dat=(j<<7)|(dat>>1);
}
return dat;
}
void tmpwritebyte(uchar dat) //向18B20写一个字节
{
bit testb;
uint i,j;
for(j=0;j<8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //写1
{
DS=0;
i++;i++;
DS=1;
i=8;while(i>0)i--;
}
else //写0
{
DS=0;
i=8;while(i>0)i--;
DS=1;
i++;i++;
}
}
}
void tmpchange() //开始温度转换并将温度存到寄存器内
{
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0x44);
}
uint tmp() //对温度进行处理
{
float tt;
uchar a,b;
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpreadbyte();
b=tmpreadbyte();
temp=b;
temp<<=8;
temp=temp|a;
tt=temp*0.625;
temp=tt*10+0.5;
return temp;
}
void write_com(uchar com) //对lcd写命令
{
rs=0;
rw=0;
P1=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar dat) //对lcd写数据
{
rs=1;
rw=0;
P1=dat;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void lcd_init() //lcd初始化
{
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void change(uint a) //将温度存进数组
{
uchar A1,A2,A3;
A1=a/100;
A2=a%100/10;
A3=a%10;
Tempdata[0]=table[A1];
Tempdata[1]=table[A2];
Tempdata[3]=table[A3];
}
void main()
{
lcd_init();
Tempdata[4]='C';
Tempdata[2]='.';
write_com(0x80+0x03);
while(1)
{
tmpchange();
change(tmp());
for(k=0;k<5;k++)
{
write_data(Tempdata[k]);
delay(10);
}
write_com(0x80+0x03);
}
}
#define uint unsigned int
#define uchar unsigned char
uint temp,k;
uchar Tempdata[5];
sbit DS=P3^6;
sbit lcden=P3^4;
sbit rs=P3^2;
sbit rw=P3^3;
uchar code table[]={0x30,0x31,0x32,
0x33,0x34,0x35,0x36,0x37,0x38,0x39}; //液晶内部字符编码表0-9
void delay(uint count)
{
uint i,j;
for(i=count;i>0;i--)
for(j=103;j>0;j--);
}
void dsreset() //复位18B20
{
DS=0;
delay(1);
DS=1;
delay(1);
}
bit tmpreadbit() //从18B20读一位
{
uint i;
bit dat;
DS=0;i++;
DS=1;i++;i++;
dat=DS;
i=8;while(i>0)i--;
return dat;
}
uchar tmpreadbyte() //从18B20读一个字节
{
uchar j,i,dat;
dat=0;
for(i=0;i<8;i++)
{
j=tmpreadbit();
dat=(j<<7)|(dat>>1);
}
return dat;
}
void tmpwritebyte(uchar dat) //向18B20写一个字节
{
bit testb;
uint i,j;
for(j=0;j<8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //写1
{
DS=0;
i++;i++;
DS=1;
i=8;while(i>0)i--;
}
else //写0
{
DS=0;
i=8;while(i>0)i--;
DS=1;
i++;i++;
}
}
}
void tmpchange() //开始温度转换并将温度存到寄存器内
{
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0x44);
}
uint tmp() //对温度进行处理
{
float tt;
uchar a,b;
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpreadbyte();
b=tmpreadbyte();
temp=b;
temp<<=8;
temp=temp|a;
tt=temp*0.625;
temp=tt*10+0.5;
return temp;
}
void write_com(uchar com) //对lcd写命令
{
rs=0;
rw=0;
P1=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar dat) //对lcd写数据
{
rs=1;
rw=0;
P1=dat;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void lcd_init() //lcd初始化
{
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void change(uint a) //将温度存进数组
{
uchar A1,A2,A3;
A1=a/100;
A2=a%100/10;
A3=a%10;
Tempdata[0]=table[A1];
Tempdata[1]=table[A2];
Tempdata[3]=table[A3];
}
void main()
{
lcd_init();
Tempdata[4]='C';
Tempdata[2]='.';
write_com(0x80+0x03);
while(1)
{
tmpchange();
change(tmp());
for(k=0;k<5;k++)
{
write_data(Tempdata[k]);
delay(10);
}
write_com(0x80+0x03);
}
}
