📄 tb1820gengxin.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
#define toj P3_7
unsigned char xiaoshu(uchar i);
void delay() ;
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,xshu;
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;
}
else{ termp2=termp2*16;
xshu=xiaoshu(termp1&0x0f) ;
termp1=termp1&0xf0;
termp1=termp1>>4;
tmpt=termp2+termp1;
ten=tmpt/10;
}
toj=0;
SBUF=codenumber[ten];
ten=tmpt%10; while(!TI) ; TI=0;
SBUF=codenumber[ten];
while(!TI) ; TI=0;
SBUF=codenumber[xshu];
while(!TI); TI=0;
toj=1;
delay();
}
unsigned char xiaoshu(uchar i)
{ switch(i)
{ case 0 : ;
case 1 : return( 0); break;
case 2 :return (1);break;
case 3 : ;
case 4 :return (2) ; break;
case 5 :return 3 ;break;
case 6 : ;
case 7 : return 4 ;break;
case 8 : return 5 ; break;
case 9 : ;
case 10 : return 6 ;break;
case 11 : ;
case 12 :return 7 ; break;
case 13 : return 8 ;break;
case 14 : ;
case 15 :return 9 ; break;
}
}
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)
{ i=20;
while(i--);
if(P3_2==0)
{ while(P3_2==0);
set=1; }
}
while(set==1)
{
if(P3_3==0)
{ for(i=0;i<8;i++);
if(P3_3==0)
{ while(P3_3==0);
uptemp++;
if(uptemp==85)
uptemp=35;
l=0;
}
}
if(P3_4==0)
{ for(i=0;i<8;i++);
if(P3_4==0)
{ while(P3_4==0);
downtemp--;
if(downtemp==3)
downtemp=30;
l=1; }
}
if(P3_2==0)
{ i=20;
while(i--);
if(P3_2==0)
{ while(P3_2==0);
set=0; }
}
toj=0;
switch(l)
{ case 0 : { SBUF=codenumber[uptemp/10];
while(!TI) ; TI=0;
SBUF=codenumber[uptemp%10];
while(!TI);TI=0;
SBUF=0xff;
while(!TI); TI=0; break; }
case 1 : { SBUF=codenumber[downtemp/10];
while(!TI) ; TI=0;
SBUF=codenumber[downtemp%10];
while(!TI);TI=0;
SBUF=0xff;
while(!TI) ; TI=0; break; }
}
toj=1;
delay();
toj=0;
delay();
toj=1;
}
}
void out(void)
{ if(downtemp<tmpt&&tmpt<uptemp)
{ P1_1=1; P1_0=P1_2=0; }
else if(tmpt>uptemp||tmpt==uptemp)
{ P1_0=1;P1_1=P1_2=0;}
else { P1_2=1;P1_0=P1_1=0; }
}
void main(void)
{
unsigned char i;
SCON=0x00;
ES=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 + -