本来应该是应该显示几个数而且可调节,但是一直在抖动,这是程序
- #define uchar unsigned char
- #define uint unsigned int
- sbit DQ=P1^7;
- sbit key1=P1^3;
- sbit key2=P1^4;
- sbit dianji=P3^1;
- float ff;
- uint y3;
- uchar shi,ge,xiaoshu,sheding=20,gaonum,dinum;
- uchar code dispcode[]={ //∂Œ¬Î
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71};
- uchar code tablel[]={ //¥ˉ–° ˝μ„μƒ∂Œ¬Î
- 0xbf,0x86,0xdb,0xcf,
- 0xe6,0xed,0xfd,
- 0x87,0xff,0xef};
- uchar dispbitcode[]={ //Œa—°
- 0xfe,0xfd,0xfb,0xf7,
- 0xef,0xdf,0xbf,0x7f};
- uchar dispbuf[8]={0,0,0,0,0,0,0,0};
- void Delay(uint num)// —” ±∫Ø ˝
- {
- while(--num);
- }
- void digitalshow(uchar a4,uchar a3,uchar a2,uchar a1,uchar a0)
- {
- dispbuf[0]=a0;
- dispbuf[1]=a1;
- dispbuf[2]=a2;
- dispbuf[3]=a3;
- dispbuf[4]=a4;
-
- P2=0xff;
- P0=dispcode[dispbuf[0]];
- P2=dispbitcode[5];
- Delay(1);
-
- P2=0xff;
- P0=dispcode[dispbuf[1]];
- P2=dispbitcode[4];
- Delay(1);
- P2=0xff;
- P0=dispcode[dispbuf[2]];
- P2=dispbitcode[2];
- Delay(1);
- P2=0xff;
- P0=tablel[dispbuf[3]];
- P2=dispbitcode[1];
- Delay(1);
- P2=0xff;
- P0=dispcode[dispbuf[4]];
- P2=dispbitcode[0];
- Delay(1);
- }
- void dmsec(uint count)
- {
- uint i; // 1ms—” ±
- while(count--)
- {
- for(i=0;i<125;i++){}
- }
- }
- void tmreset(void)
- {
- DQ=0;
- Delay(90);
- DQ=1;
- Delay(4);
- }
- void tmpre(void)
- {
- while(DQ);
- while(~DQ);
- Delay(4);
- }
- bit tmrbit(void)
- {
- uint i;
- bit dat;
- DQ=0;
- i++; // i++;¥Û∏≈1us
- DQ=1;
- i++;
- i++;
- dat=DQ;
- Delay(8);
- return(dat);
- }
- uchar tmrbyte(void) //∂¡“a∏ˆ±»Ãÿ
- {
- uchar i,j,dat;
- dat=0;
- for(i=1;i<=8;i++)
- {
- j=tmrbit();
- dat=(j<<7)|(dat>>1);
- }
- return(dat);
- }
- void tmwbyte(uchar dat) //–¥“a∏ˆ±»Ãÿ
- {
- uint i;
- uchar j;
- bit testb;
- for(j=1;j<=8;j++)
- {
- testb=dat&0x01;
- dat=dat>>1; // ¥”μÕŒaø™ o
- if(testb) // Write 1
- {
- DQ=0; // œ»¿≠μÕ
- i++;
- i++; // >1us
- DQ=1;
- Delay(4);
- }
- else // Write 0
- {
- DQ=0;
- Delay(4);
- DQ=1;
- i++;
- i++; // ‘Ÿ¿≠∏fl
- }
- }
- }
- void tmstart(void) //ds1820ø™ o◊™aa
- {
- dmsec(1);
- tmreset();
- tmpre();
- dmsec(1);
- tmwbyte(0xcc); // skip rom
- tmwbyte(0x44); // תaa
- }
- uchar tmrtemp(void) //∂¡»°Œ¬∂»
- {
- uchar a,b;
- tmreset();
- tmpre();
- dmsec(1);
- tmwbyte(0xcc); // skip rom
- tmwbyte(0xbe); // תaa
- a=tmrbyte(); // LSBμÕ8Œa
- b=tmrbyte(); // MSB∏fl8Œa
- y3=b;
- y3<<=8;
- y3=y3|a;
- ff=y3*0.0625;
- y3=ff*10+0.5;
- return(y3);
- }
- void keyscan(void)
- {
- if(key1==0)
- {
- dmsec(5);
- if(key1==0)
- {
- sheding++;
- if(sheding==100)
- sheding=20;
- }
- while(!key1);
- }
- else if(key2==0)
- {
- dmsec(5);
- if(key2==0)
- {
- sheding--;
- if(sheding==0)
- sheding=20;
- }
- while(!key2);
- }
- }
- void deal(uint tmp) //Œ¬∂»¥¶¿ì
- {
- if(tmp<=sheding)
- {
- gaonum=0;
- dinum=4;
- }
- else if((tmp>sheding)&&(tmp<=(sheding+5)))
- {
- gaonum=1;
- dinum=3;
- }
- else if((tmp>(sheding+5))&&(tmp<=(sheding+10)))
- {
- gaonum=2;
- dinum=2;
- }
- else if((tmp>(sheding+10))&&(tmp<=(sheding+15)))
- {
- gaonum=3;
- dinum=1;
- }
- else
- {
- gaonum=4;
- dinum=0;
- }
- }
- void dianjik() //μáa˙øÿ÷∆
- {
- uchar q,i;
- for(q=0;q<dinum;q++)
- {
- dianji=0;
- digitalshow(shi,ge,xiaoshu,sheding/10,sheding%10);
- for(i=255;i>0;i--)
- {
- digitalshow(shi,ge,xiaoshu,sheding/10,sheding%10);
- }
- }
- for(q=0;q<gaonum;q++)
- {
- dianji=1;
- digitalshow(shi,ge,xiaoshu,sheding/10,sheding%10);
- for(i=255;i>0;i--)
- {
- digitalshow(shi,ge,xiaoshu,sheding/10,sheding%10);
- }
- }
- }
- void main(void)
- {
- uint last;
- dianji=0;
- tmstart();
- dmsec(450); // ≥ı oaØds18b20
- while(1)
- {
- tmstart(); // ds1820ø™ o◊™aa
- dmsec(2);
- last=tmrtemp()+256; // ∂¡»°Œ¬∂»
- shi=last/100;
- ge=(last%100)/10;
- xiaoshu=(last%100)%10;
- keyscan();
- dmsec(2);
- deal(last/10);
- dianjik();
- }
- }
复制代码
|