#include
//#include
#define uchar unsigned char
#define uint unsigned int
uchar num=0;
uchar table[]="temperature :";
uchar table1[]=".";
sbit scl=P3^7;
sbit rs=P2^0;
sbit en=P2^2;
sbit rw=P2^1;
void delay(unsigned int us)
{
while(us--);
}
void delay1(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--) ;
}
void rest(void) //复位
{
uchar x=0;
scl = 1;
delay(8); //稍做延时
scl = 0;
delay(80); //精确延时 大于 480us
scl = 1; //拉高总线
delay(14);
x=scl;
delay(20);
}
void write(unsigned char dat) //写1字节
{
uchar i=0;
for (i=8; i>0; i--)
{
scl=0;
scl=dat&0x01;
delay(5);
scl= 1;
dat>>=1;
}
delay(4);
}
uchar read(void) //读1字节
{
uchar i=0;
uchar dat=0;
for (i=8;i>0;i--)
{
scl=0;
dat>>=1;
scl=1;
if(scl)
dat|=0x80;
delay(4);
}
return(dat);
}
void write1(uchar comm,uchar date)
{
rs=comm;
P0=date;
delay1(5);
en=1;
delay1(5);
en=0;
}
void write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write1(0,0x0c);
write1(0,add);
write1(1,0x30+shi);
write1(1,0x30+ge);
}
init()
{
rw=0;
en=0;
write1(0,0x38);// /76mn/8为数据口
write1(0,0x0f);//显示光标,闪烁
write1(0,0x06);//
write1(0,0x01);//显示清0
write1(0,0x80);
// write()
for(num=0;num<13;num++)
{
write1(1,table[num]);
}
write1(0,0x80+0x40+6);
write1(1,table1[0]);
}
void display()
{
uchar m,b,y;
float n;
ES=0;
rest();
write(0x0cc);
write(0x44);
rest();
write(0xcc);
write(0xbe);
m=read();
b=read();
y=m&0x0f;
m>>=4;
b<<=4;
n=y;
n=y*6.25;
y=n;
b=b|m;
write_sfm(0x80+0x40+4,b);
write_sfm(0x80+0x40+7,n);
delay1(100);
//t=t*0.0625;
}
void main()
{
init();
while(1)
{
display();
// while(1);
//delay(1000) ;
}
}
我是用1602液晶显示的,精确到小数点后两位的。
我有的qq379663387
自己到论坛搜吧
多的是