51单片机温度检测程序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
#include //包含nop等
#define uchar unsigned char
#define uint unsigned int
sbit DS=P2^2; //define interface
uint temp; // variable of temperature
uchar flag1; // sign of the result positive or negative

void delay(uint count) //delay
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
/**************1602LCD**********************/
sbit LCM_RS=P3^5; //LCD1602命令端口
sbit LCM_RW=P3^6; //LCD1602命令端口
sbit LCM_EN=P3^4; //LCD1602命令端口
#define DataPort P0 //LCD1602数据端口
/*******************************/
void WaitForEnable(void)
{
DataPort=0xff;
LCM_RS=0;LCM_RW=1;_nop_();
LCM_EN=1;_nop_();_nop_();
while(DataPort&0x80);
LCM_EN=0;
}
/*******************************/
/////1602有关函数
void WriteCommandLCM(uchar CMD,uchar Attribc)
{
if(Attribc)WaitForEnable();
LCM_RS=0;LCM_RW=0;_nop_();
DataPort=CMD;_nop_();
LCM_EN=1;_nop_();_nop_();LCM_EN=0;
}
/*******************************/
void WriteDataLCM(uchar dataW)
{
WaitForEnable();
LCM_RS=1;LCM_RW=0;_nop_();
DataPort=dataW;_nop_();
LCM_EN=1;_nop_();_nop_();LCM_EN=0;
}
/***********************************/
void InitLcd()
{
WriteCommandLCM(0x38,1);
WriteCommandLCM(0x08,1);
WriteCommandLCM(0x01,1);
WriteCommandLCM(0x06,1);
WriteCommandLCM(0x0c,1);
}
/***********************************/
void DisplayOneChar(uchar X,uchar Y,uchar DData)
{
Y&=1;
X&=15;
if(Y)X|=0x40;
X|=0x80;
WriteCommandLCM(X,0);
WriteDataLCM(DData);
}



///////功能:串口初始化,波特率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--;
}

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 delay10ms() //delay
{
uchar a,b;
for(a=10; a>0; a--)
for(b=60; b>0; b--);
}

void display(uint temp) //显示程序
{
uchar A1,A2,A2t,A3;

A1=temp/100+'0'; //十位
A2t=temp%100;
A2=A2t/10+'0'; //个位
A3=A2t%10+'0'; //十分位
DisplayOneChar(0,0,A1);
DisplayOneChar(1,0,A2);
DisplayOneChar(2,0,'.');
DisplayOneChar(3,0,A3);
DisplayOneChar(4,0,'D');
DisplayOneChar(5,0,'e');
DisplayOneChar(6,0,'g');
DisplayOneChar(7,0,'r');
DisplayOneChar(8,0,'e');
DisplayOneChar(9,0,'e');


SBUF=A1;
while(TI==0);
TI=0;
SBUF=A2;
while(TI==0);
TI=0;
SBUF='.';
while(TI==0);
TI=0;
SBUF=A3;
while(TI==0);
TI=0;
SBUF='\r';
while(TI==0);
TI=0;
SBUF='\n';
while(TI==0);
TI=0;
}


void main()
{
unsigned int temp_x_10;
Init_Com();
InitLcd();
while(1)
{
tmpchange();
temp_x_10=tmp();
display(temp_x_10);
}

}

相关文档
最新文档