#include<reg52.h>
#include<stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit DS=P2^2;
sbit rs=P3^5;
sbit lcden=P3^4;
sbit beep=P2^3;
uint temp;
uchar num;
float f_temp;
uchar code table2[]=" temperture ";
//uchar code table[]="welcome to here !";//液晶固定显示内容
//uchar code table1[]=" . ";
void init_yejing();
void write_date(uchar date);
void write_com(uchar com);
void write_temp(uchar add,char date);
void delay(uint x)
{
uint i,j;
for(i=110;i>0;i--)
for(j=x;j>0;j--);
}
void init_yejing()
{
uchar num;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
/* for(num=0;num<15;num++)//写入液晶固定部分显示
{
write_date(table[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<11;num++)
{
write_date(table1[num]);
delay(5);
} */
}
void write_com(uchar com)
{//写液晶命令函数
rs=0;
lcden=0;
P0=com;
delay(3);
lcden=1;
delay(3);
lcden=0;
}
void write_date(uchar date)
{//写液晶数据函数
rs=1;
lcden=0;
P0=date;
delay(3);
lcden=1;
delay(3);
lcden=0;
}
void write_temp(uchar add,char date)
{//1602液晶刷新年月日函数3为年,6为分,9为秒
char shi,ge;
// shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
// write_date(0x30+shi);
write_date(0x30+ge);
}
void di() //蜂鸣器报警声音
{
beep=0;
delay(100);
beep=1;
}
///////功能:串口初始化,波特率9600,方式1///////
void Init_Com(void)
{
TMOD = 0x20;
PCON = 0x00;
SCON = 0x50;
TH1 = 0xFd;
TL1 = 0xFd;
TR1 = 1;
}
void dsreset(void) //send reset and initialization command
{
uint i;
DS=0;
i=103;
while(i>0)i--;
DS=1;
i=4;
while(i>0)i--;
write_com(0x80+2);
for(num=0;num<12;num++)//写入液晶固定部分显示
{
write_date(table2[num]);
delay(5);
}
write_com(0x80+0x40+7);
write_date('.');
}
bit tmpreadbit(void) //read a bit
{
uint i;
bit dat;
DS=0;i++; //i++ for delay
DS=1;i++;i++;
dat=DS;
i=8;while(i>0)i--;
return (dat);
}
uchar tmpread(void) //read a byte date
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tmpreadbit();
dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DAT里
}
return(dat);
}
void tmpwritebyte(uchar dat) //write a byte to ds18b20
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //write 1
{
DS=0;
i++;i++;
DS=1;
i=8;while(i>0)i--;
}
else
{
DS=0; //write 0
i=8;while(i>0)i--;
DS=1;
i++;i++;
}
}
}
void tmpchange(void) //DS18B20 begin change
{
dsreset();
delay(1);
tmpwritebyte(0xcc); // address all drivers on bus
tmpwritebyte(0x44); // initiates a single temperature conversion
}
uint tmp() //get the temperature
{
float tt;
uchar a,b;
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpread();
b=tmpread();
temp=b;
temp<<=8; //two byte compose a int variable
temp=temp|a;
tt=temp*0.0625;
temp=tt*10+0.5;
return temp;
}
void readrom() //read the serial
{
uchar sn1,sn2;
dsreset();
delay(1);
tmpwritebyte(0x33);
sn1=tmpread();
sn2=tmpread();
}
void dis_temp(uint t)
{
uchar i;
i=t/100;
write_temp(5,i);
i=t%100/10;
write_temp(6,i);
i=t%100%10;
write_temp(8,i);
}
void main()
{ uchar a;
init_yejing();
Init_Com();
do
{
tmpchange();
// delay(200);
for(a=10;a>0;a--)
{ dis_temp(tmp());
}
} while(1);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>