显示温度 有的时候 会显示 一些乱的数
是不是 延时的问题?
按键 设置上下限 温度时 不能调成负数 和这个让我头皮发麻 完全不知道原因
- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit d1=P2^2;
- sbit d2=P2^1;
- sbit d3=P2^0;
- sbit d4=P2^3;
- sbit key1=P3^0;
- sbit key2=P3^1;
- sbit key3=P3^2;
- sbit beep=P3^3;
- sbit DS=P1^2;
- sbit ACC_7=ACC^7;
- uint count=700,alarm=-300;
- uchar shu;
- uchar shi,fen,ri,yue,nian,xq,miao,ss;
- uint temp; // ??????
- uchar flag;
- uchar code tab[]=
- {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- uchar code tab1[]=//С???????
- {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=120;y>0;y--);
- }
- void dsreset()//????λ
- {
- uint i;
- DS=0;
- i=103;
- while(i>0)i--;
- DS=1;
- i=4;
- while(i>0)i--;
- }
- uchar tmpread() //???????
- {
- uchar j,k,dat;
- uint i;
- for(j=1;j<=8;j++)
- {
- DS=0;i++; //???
- DS=1;i++;i++;
- k=DS;
- i=8;while(i>0)i--;
- dat=(k<<7)|(dat>>1);//?????????????λ????????????????DAT??
- }
- return(dat);
- }
- void tmpwritebyte(uchar dat) //д??????
- {
- uint i;
- uchar j;
- bit testb;
- for(j=1;j<=8;j++)
- {
- testb=dat&0x01;
- dat=dat>>1;
- if(testb) //д 1
- {
- DS=0;
- i++;i++;
- DS=1;
- i=8;while(i>0)i--;
- }
- else
- {
- DS=0; //д 0
- i=8;while(i>0)i--;
- DS=1;
- i++;i++;
- }
- }
- }
- void tmpchange() //DS18B20???任
- {
- dsreset();
- delay(1);
- tmpwritebyte(0xcc); //??????????rom
- tmpwritebyte(0x44); //??????
- }
- uint tmp() //??????
- {
- float tt;
- uchar a,b;
- dsreset();
- delay(1);
- tmpwritebyte(0xcc);
- tmpwritebyte(0xbe);
- a=tmpread(); //a??????8λ
- b=tmpread(); //b??????8λ
- temp=b; //temp?????UINT 16bit
- temp<<=8; //??????????????
- temp=temp|a;
- if(b>127)
- {
- flag=1;
- ss=flag;
- temp=~temp+1;
- }
- tt=temp*0.0625; // temp/16 ????????????tt.7λ????,4λС??
- temp=tt*10+0.5; // ??????????????λС??
- return(temp);
- }
- void displayTemp(uint temp) //?????????
- {
- uchar ge,shi,bai,qian,ser;
- d1=0;
- d1=0;
- d3=0;
- d4=0;
- dsreset();
- ser=temp/10; //???????λ??????????
- SBUF=ser;
- qian=temp/1000;
- bai=temp/100%10; // ??λ????
- shi=temp/10%10; // ?λ????
- ge=temp%10; // ??λ????
- if(flag==1)
- {
- flag=0;
- P0=0xbf;
- d1=1;
- delay(2);
- d1=0;
- }
- if(qian!=0)
- {
- P0=tab[qian];
- d1=1;
- delay(2);
- d1=0;
- }
- else
- {
- P0=0xff;
- d1=1;
- delay(2);
- d1=0;
- }
- if(temp>99)
- {
- P0=tab[bai];
- d2=1;
- delay(2);
- d2=0;
- }
- P0=tab1[shi];
- d3=1;
- delay(2);
- d3=0;
- P0=tab[ge];
- d4=1;
- delay(2);
- d4=0;
- }
- uint keyscan()
- {
- if(key1==0)
- {
- delay(5);
- if(key1==0)
- {
- while(!key1);
- shu++;
- }
- }
- if(key2==0)
- {
- delay(5);
- if(key2==0)
- {
- while(!key2);
- count=count+10;
- if(shu==2)
- {
- alarm+=10;
- }
- }
- }
- if(key3==0)
- {
- delay(5);
- if(key3==0)
- {
- while(!key3);
- count=count-10;
- if(shu==2)
- {
- alarm-=10;
- }
- }
- }
- return(count);
- }
- void main()
- {
- beep=0;
- delay(10);
- while( 1 )
- {
- tmpchange();
- if(shu==0)
- {
- displayTemp(tmp( ));
- }
- keyscan();
- if(shu==1)
- {
- displayTemp(count);
- }
- if(shu==2)
- {
- displayTemp(alarm);
- }
- if(shu==3)
- {
- shu=0;
- }
- if(temp>count||(temp<alarm))
- {
- beep=0;
- }
- else
- beep=1;
- }
- }
复制代码
temp.zip
(103.74 KB, 下载次数: 16)
|