当前位置:文档之家› 51单片机测温程序

51单片机测温程序

#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
uinti,numone,numtwo,temp;
ucharqian,bai,shi,ge,xiaoshu;
sbitdq=P2^2;
sbitdula=P2^6;
sbitwela=P2^7;
uchar code list[]={
0x3f , 0x06 , 0x5b , 0x4f , 0x66 ,
0x6d ,0x7d , 0x07 , 0x7f , 0x6f ,
0x77 , 0x7c , 0x39 , 0x5e , 0x79 , 0x71,0x80 };
unsigned char code listone[] = {0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
void delay(uint z)
{
uintx,y;
for(x=100;x>0;x--)
for(y=z;y>0;y--);
}
voiddelayone(unsigned char i)
{
while(--i);
}
/****************************************** 此延时函数针对的是12Mhz的晶振
delay(0):延时518us 误差:518-2*256=6
delay(1):延时7us (原帖写"5us"是错的)delay(10):延时25us 误差:25-20=5
delay(20):延时45us 误差:45-40=5
delay(100):延时205us 误差:205-200=5
delay(200):延时405us 误差:405-400=5*/ voidshuma(uchar temp)
{
shi=temp/100;
ge=temp%100/10;
xiaoshu=temp%10;
dula=1;
P0=list[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(1);
dula=1;
P0=listone[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(1);
/* dula=1;
P0=list[16];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(1);
*/
dula=1;
P0=list[xiaoshu];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delay(1);
}
voidtempinit()
{
dq=1;
_nop_();
// delayone(1);
dq=0;
for(i=0;i<200;i++);
delayone(250);
dq=1;
i=0;
while((dq==0)&&(i<350))
i++;
delay(1);
// delayone(100);
}
void write(uchar date) {
uint j;
for(i=0;i<8;i++)
{
dq=1;
_nop_();
dq=0;
_nop_();
dq=date&0x01; // for(j=0;j<2;i++);
delayone(20);
dq=1;
date=date>>1;
// for(j=0;j<20;i++);
delayone(1);
}
}
uchar read()
{
ucharmation;
for(i=0;i<8;i++)
{
dq=1;
_nop_();
dq=0;
mation=mation>>1;
dq=1;
// for(i=0;i<2;i++);
delayone(1);
if(dq==1)
mation|=0x80;
else
mation|=0x00;
// for(i=0;i<3;i++);
delayone(10);
}
return(mation);
}
uchargettemp()
{
uchar TH,TL;
floattt;
tempinit();
write(0xcc);
write(0x44);
delay(1);
tempinit();
write(0xcc);
write(0xbe);
TL=read();
TH=read();
temp=TH;
temp<<=8;
temp=temp|(TL);
tt=temp*0.0625;
temp=tt*10+0.5;
return temp;
}
void main()
{
while(1)
{
shuma(gettemp());
}
}。

相关主题