📄 tb1820.c
字号:
/*8M crystal ,temperture detector.The number of controlling
device is DS1820*/
#include<REGX51.h>
#include<absacc.h>
#include<stdio.h>
#include<math.h>
#include<float.h>
#define uchar unsigned char
#define uint unsigned int
char code codenumber[]={0x03,0x9f,0x25,0x0c,0x99,0x48,0x40,0x1f,0x00,0x09};
uchar termp1,termp2,set;
static uchar uptemp,downtemp,j;
int tmpt=0;
void dsreset(void) /*初始化*/
{ unsigned char i;
start:
i=200;
P3_5=0;
while(i>0) i--;
P3_5=1;
i=20;
while(i>0) i--;
while(P3_5==1) goto start;
i=40;
while(i>0)
{ if(P3_5==1) break;
i--;
}
if(i==0) goto start;
else P3_5=1;
i=160;
while(i>0) i--;
}
unsigned char tmpread2(void) /*读一字节*/
{ unsigned char i, k,dat2;
bit testb;
dat2=0;
for(i=0;i<8;i++)
{ P3_5=0;
k=4;
P3_5=1;
while(k>0) k--;
testb=P3_5;
k=18;
while(k>0) k--;
dat2=dat2>>1;
dat2=testb?dat2|0x80:dat2|0x00;
}
return(dat2);
}
void tmpwrite(unsigned char dat) /*写字节数据*/
{
unsigned int i;
unsigned char j;
bit testb;
for(j=0;j<8;j++)
{
testb=dat&0x01;
dat=dat>>1;
P3_5=0;
i=1;
while(i>0) i--;
P3_5=testb;
i=4;
while(i>0) i--;
P3_5=1;
}
}
void tmpchange(void) /*温度转换*/
{
dsreset();
tmpwrite(0xcc);
tmpwrite(0x44);
}
void showtermp()
{
unsigned char ten;
if(set==1)
return;
if(termp2&0x80)
{ termp1=~termp1+1;
termp2=~termp2;
if(termp1==0)
termp2=termp2+1;
termp2=termp2*16;
termp1=termp1&0xf0;
termp1=termp1>>4;
tmpt=termp2+termp1;
ten=tmpt/10;
P0=codenumber[ten];
ten=tmpt%10;
P2=codenumber[ten];
P3_7=0;
}
else{ termp2=termp2*16;
termp1=termp1&0xf0;
termp1=termp1>>4;
tmpt=termp2+termp1;
ten=tmpt/10;
P3_7=1;
P0=codenumber[ten];
ten=tmpt%10;
P2=codenumber[ten];
}
}
void delay()
{ unsigned char i,j;
for(i=0;i<250;i++)
for(j=0;j<254;j++);
}
void control(void) /*温度设置*/
{ uchar i,l;
if(P3_2==0||set==1)
{ if(P3_2==0)
{ i=20;
while(i--);
if(P3_2==0)
{ while(P3_2==0);
set++;
if(set==2)
set=0; } }
if(set==1)
{
if(P3_3==0)
{ for(i=0;i<102;i++);
if(P3_3==0)
{ while(P3_3==0);
uptemp++;
if(uptemp==85)
uptemp=35;
P0=codenumber[uptemp/10];
P2=codenumber[uptemp%10];
}
}
if(P3_4==0)
{ for(i=0;i<100;i++);
if(P3_4==0)
{ while(P3_4==0);
downtemp--;
if(downtemp==3)
downtemp=30;
P3_7=0;
P0=codenumber[downtemp/10];
P2=codenumber[downtemp%10];
delay();
delay();
P0=0xff;
P2=0xff;
delay();
P3_7=1; }
} l++;
if(l==2)
l=0;
}
}
}
void out(void)
{ if(downtemp<tmpt&&tmpt<uptemp)
P1=0x0F2;
else if(tmpt>uptemp||tmpt==uptemp)
P1=0x04;
else P1=0x01;
}
void main(void)
{
unsigned char i;
SCON=0x00;
P3_7=0;
set=0;
uptemp=40;
downtemp=35;
do { control();
P3_5=1;
tmpchange();
dsreset();
tmpwrite(0xcc);
tmpwrite(0xbe);
termp1=tmpread2();
termp2=tmpread2();
P3_5=0;
i=150;
while(i>0) i--;
showtermp();
out();
}while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -