#include <reg52.h>
sbit lcdrs=P3^5;
sbit lcdrw=P3^6;
sbit lcden=P3^4;
sbit ds = P2^2;
sbit TRIG = P1^0;
sbit ECHO =P1^1;
sbit beep=P2^3;
#define uint unsigned int
#define uchar unsigned char
void Timer1_Init();
unsigned int Get_Distance();
void delayms();
void write_com();
void write_data();
void lcd1602init();
int i;
float shuru,value;
bit flag;
uint dValue,tmp,tmp1;
uchar code table[] = "tempure and juli:";
//uchar code lcdline2[]="Table Lam is" ;
//延时
void delayms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//1602
void write_com(uchar com)
{
lcdrs=0;
lcdrw=0;
lcden=0;
P0=com;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void write_data(uchar dat)
{
lcdrs=1;
lcdrw=0;
P0=dat;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void lcd1602init()
{
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
//
void writeString(uchar * str, uchar length)
{
uchar i;
for(i = 0; i < length; i++)
{
write_data(str[i]);
}
}
// ds18b20
void dsInit()
{
unsigned int i;
ds = 0;
i = 100;
while(i>0)
i--;
ds = 1;
i = 4;
while(i>0)
i--;
}
bit readBit()
{
unsigned int i;
bit b;
ds = 0;
i++;
ds = 1;
i++;
i++;
b = ds;
i = 8;
while(i>0)
i--;
return b;
}
unsigned char readByte()
{
unsigned int i;
unsigned char j, dat;
dat = 0;
for(i=0; i<8; i++)
{
j = readBit();
dat = (j << 7) | (dat >> 1);
}
return dat;
}
void writeByte(unsigned char dat)
{
int i,j;
for(j=0;j<8;j++)
{
ds=0;
i++;
ds=dat&0x01;
i=6;
while(i--);
ds=1;
dat>>=1;
}
}
void sendChangeCmd()//温度转化命令
{
dsInit();
delayms(1);
writeByte(0xcc);
writeByte(0x44);
}
void sendReadCmd() //读取温度
{
dsInit();
delayms(1);
writeByte(0xcc);
writeByte(0xbe);
}
int getTmpValue()
{
unsigned int tmpvalue;
float t;
unsigned char low, high;
sendReadCmd();
low = readByte();
high = readByte();
tmpvalue = high;
tmpvalue <<= 8;
tmpvalue |= low;
value = tmpvalue;
t = value * 0.0625;
value = t * 100 + (value > 0 ? 0.5 : -0.5); //大于0加0.5, 小于0减0.5
return value;
}
void display()
{
unsigned char count ;
unsigned char datas[] = {0, 0, 0};
tmp = Get_Distance();
datas[0] = tmp/100 ;
datas[1] = tmp%100/10;
datas[2] = tmp%10;
write_com(0xc0+12);
for(count = 0; count < 3; count++)
{
write_data('0'+datas[count]); //写入能识别的字符 ’0‘+数值
}
if(tmp>200)
{
beep=0;
}
else
{
beep=1;
}
}
void display1()
{
unsigned char count1;
unsigned char datas1[] = {0, 0, 0, 0, 0};
unsigned int tmp1 =getTmpValue();
datas1[0] = tmp1 / 10000;
datas1[1] = tmp1 % 10000 / 1000;
datas1[2] = tmp1 % 1000 / 100;
datas1[3] = tmp1 % 100 / 10;
datas1[4] = tmp % 10;
write_com(0xc0+1);
if(tmp1< 0)
{
writeString("- ", 1);
}
else
{
writeString("+ ", 1);
}
for(count1 = 1; count1 < 5; count1++)
{
write_data('0'+datas1[count1]);
if(count1 == 2)
{
write_data('.');
}
}
if(tmp1>2000)
{
P1=0x00;
}
else
{
P1=0xff;
}
}
void main(void)
{
lcd1602init();
Timer1_Init();
write_com(0x80);
writeString(table,16);
delayms(1000);
while(1)
{
sendChangeCmd();//发送温度转化命令
delayms(750); //温度转换时间需要750ms以上
display1();
delayms(100);
display();
delayms(100);
}
}
/***********开启定时器1并初始化***************/
void Timer1_Init(void)
{
TMOD= 0x10;
TH1=0;
TL1=0;
EA =1;
TR1=0;
}
void Timer1_ISR() interrupt 3
{
flag=1;
}
unsigned int Get_Distance()
{
unsigned int distance;
uint t=100;
TRIG=1;
while(t--);
TRIG=0;
while(!ECHO);
flag=0;
TR1=1;
while(ECHO);
TR1=0;
distance=TH1*256+TL1;
distance=distance*0.017;
TH1=0;
TL1=0;
delayms(100);
return distance;
}
|