|
#include<reg51.h>
sbit SDA=P2^1;
sbit SCL=P2^2;
sbit lcden=P2^7;
sbit lcdrs=P2^6;
unsigned char a,b,c,d,z1,z2,z3,z4,z5,flag,e;
unsigned int number,sensor;
int time,second,key;
void nop()
{ unsigned int TempCyc = 22;
while(TempCyc--) ;
}
unsigned char code table[]="Temperature:";
unsigned char code table1[]="Humidity:";
unsigned char num,adc;
void delay(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y<110;y>0;y--);
}
void write_com(unsigned char com)
{ delay(5);
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(unsigned char date)
{ delay(5);
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
lcden=0;
write_com(0x38);
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void init1()
{SCL=1;
nop();
SDA=1;
nop();
}
void start()
{
SDA=1;
nop();
SCL=1;
nop();
SDA=0;
nop();
SCL=0;
nop();
}
void respons()
{
unsigned char g=0;
SCL=1;
nop();
while((SDA==1)&&(g<255))
g++ ;
SCL=0;
nop();
}
void stop()
{SDA=0;
nop();
SCL=0;
nop();
SCL=1;
nop();
SDA=1;
nop();
}
void write_byte(unsigned char date)
{
unsigned char g,temp;
temp=date;
for(g=0;g<8;g++)
{
temp=temp<<1;
SCL=0;
nop();
SDA=CY;
nop();
SCL=1;
nop();
}
SCL=0;
nop();
SDA=1;
nop();
}
void cpuack()
{SDA=0;
nop();
SCL=1;
nop();
SCL=0;
nop();
}
unsigned char read_byte()
{
unsigned char g,k;
SCL=0;
nop();
SDA=1;
for(g=0;g<8;g++)
{
SCL=1;
nop();
k=(k<<1)|SDA;
SCL=0;
nop();
}
nop();
return k;
}
main()
{
init();
for(;;)
{
write_com(0x80);
for(num=0;num<12;num++)
{
write_data(table[num]) ;
delay(5);
}
write_data(c+48) ;
delay(5);
write_data(d+48) ;
delay(5);
write_data(46) ;
delay(5);
write_data(e+48) ;
delay(5);
write_com(0x80+0x40);
for(adc=0;adc<9;adc++)
{
write_data(table1[adc]);
delay(5);
}
write_data(a+48) ;
delay(5);
write_data(b+48) ;
delay(5);
write_data(37) ;
delay(5);
init1();
start();
write_byte(0x88);
respons();
write_byte(0x24);
respons();
write_byte(0x16);
respons();
stop();
nop();
start();
write_byte(0x89);
respons();
nop();
nop();
if(SCL==0)
{
z2=read_byte();
cpuack() ;
z3=read_byte();
cpuack() ;
read_byte();
cpuack() ;
z4=read_byte();
cpuack();
z5=read_byte();
cpuack() ;
read_byte();
stop();
number=256*z2+z3;
sensor=256*z4+z5;
number=(number/37.5)-450 ;
sensor=sensor/656;
a=sensor/10;
b=sensor%10 ;
c=(number%1000)/100;
d=(number%100)/10;
e=number%10;
}
}
} |
評(píng)分
-
查看全部評(píng)分
|