1. DS1624基本原理 DS1624是美國DALLAS公司生產(chǎn)的集成了測量系統(tǒng)和存儲器于一體的芯片。數(shù)字接口電路簡單,與I2C總線兼容,且可以使用一片控制器控制多達(dá)8片的DS1624。其數(shù)字溫度輸出達(dá)13位,精度為0.03125℃。DS1624可工作在最低2.7V電壓下,適用于低功耗應(yīng)用系統(tǒng)。 1). DS1624基本特性 ◆ 無需外圍元件即可測量溫度 ◆ 測量范圍為-55℃~+125℃,精度為0.03125℃ ◆ 測量溫度的結(jié)果以13位數(shù)字量(兩字節(jié)傳輸)給出 ◆ 測量溫度的典型轉(zhuǎn)換時間為1秒 ◆ 集成了256字節(jié)的E2PROM非易性存儲器 ◆ 數(shù)據(jù)的讀出和寫入通過一個2-線(I2C)串行接口完成 ◆采用8腳DIP或SOIC封裝 DS1624在測量溫度時使用了獨(dú)有的在線溫度測量技術(shù)。它通過在一個由對溫度高度敏感的振蕩器決定的計數(shù)周期內(nèi)對溫度低敏感的振蕩器時鐘脈沖的計數(shù)值的計算來測量溫度。DS1624在計數(shù)器中預(yù)置了一個初值,它相當(dāng)于-55℃。如果計數(shù)周期結(jié)束之前計數(shù)器達(dá)到0,已預(yù)置了此初值的溫度寄存器中的數(shù)字就會增加,從而表明溫度高于-55℃。 與此同時,計數(shù)器斜坡累加電路被重新預(yù)置一個值,然后計數(shù)器重新對時鐘計數(shù),直到計數(shù)值為0。 通過改變增加的每1℃內(nèi)的計數(shù)器的計數(shù),斜坡累加電路可以補(bǔ)償振蕩器的非線性誤差,以提高精度,任意溫度下計數(shù)器的值和每一斜坡累加電路的值對應(yīng)的計數(shù)次數(shù)須為已知。 DS1624通過這些計算可以得到0.03125℃的精度,溫度輸出為13位,在發(fā)出讀溫度值請求后還會輸出兩位補(bǔ)償值。表2給出了所測的溫度和輸出數(shù)據(jù)的關(guān)系。這些數(shù)據(jù)可通過2線制串行口連續(xù)輸出,MSB在前,LSB在后。 由于數(shù)據(jù)在總線上傳輸時MSB在前,所以DS1624讀出的數(shù)據(jù)可以是一個字節(jié)(分辨率為1℃),也可以是兩個字節(jié),第二個字節(jié)包含的最低位為0.03125℃。 - #include <AT89X52.H>
- #include <INTRINS.H>
- unsigned char code displaybit[]={0xfe,0xfd,0xfb,0xf7,
- 0xef,0xdf,0xbf,0x7f};
- unsigned char code displaycode[]={0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71,0x00};
-
- unsigned char code dotcode[32]={0,3,6,9,12,16,19,22,
- 25,28,31,34,38,41,44,48,
- 50,53,56,59,63,66,69,72,
- 75,78,81,84,88,91,94,97};
- sbit SDA=P1^6;
- sbit SCL=P1^7;
-
- unsigned char displaybuffer[8]={0,1,2,3,4,5,6,7};
- unsigned char eepromdata[8];
- unsigned char temperdata[2];
-
- unsigned char timecount;
- unsigned char displaycount;
-
- bit secondflag=0;
- unsigned char secondcount=0;
- unsigned char retn;
- unsigned int result;
- unsigned char x;
- unsigned int k;
- unsigned int ks;
-
- void delay(void);
- void delay10ms(void);
- void i_start(void);
- void i_stop(void);
- void i_init(void);
- void i_ack(void);
- bit i_clock(void);
- bit i_send(unsigned char i_data);
- unsigned char i_receive(void);
- bit start_temperature_T(void);
- bit read_temperature_T(unsigned char *p);
- void delay(void)
- {
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }
-
- void delay10ms(void)
- {
- unsigned int i;
- for(i=0;i<1000;i++)
- {
- delay();
- }
- }
-
- void i_start(void)
- {
- SCL=1;
- delay();
- SDA=0;
- delay();
- SCL=0;
- delay();
- }
-
- void i_stop(void)
- {
- SDA=0;
- delay();
- SCL=1;
- delay();
- SDA=1;
- delay();
- SCL=0;
- delay();
- }
- void i_init(void)
- {
- SCL=0;
- i_stop();
- }
-
- void i_ack(void)
- {
- SDA=0;
- i_clock();
- SDA=1;
- }
-
- bit i_clock(void)
- {
- bit sample;
-
- SCL=1;
- delay();
- sample=SDA;
- _nop_();
- _nop_();
- SCL=0;
- delay();
- return(sample);
- }
-
- bit i_send(unsigned char i_data)
- {
- unsigned char i;
-
- for(i=0;i<8;i++)
- {
- SDA=(bit)(i_data & 0x80);
- i_data=i_data<<1;
- i_clock();
- }
- SDA=1;
- return(~i_clock());
- }
- unsigned char i_receive(void)
- {
- unsigned char i_data=0;
- unsigned char i;
-
- for(i=0;i<8;i++)
- {
- i_data*=2;
- if(i_clock()) i_data++;
- }
- return(i_data);
- }
-
- bit start_temperature_T(void)
- {
- i_start();
- if(i_send(0x90))
- {
- if(i_send(0xee))
- {
- i_stop();
- delay();
- return(1);
- }
- else
- {
- i_stop();
- delay();
- return(0);
- }
- }
- else
- {
- i_stop();
- delay();
- return(0);
- }
- }
-
- bit read_temperature_T(unsigned char *p)
- {
- i_start();
- if(i_send(0x90))
- {
- if(i_send(0xaa))
- {
- i_start();
- if(i_send(0x91))
- {
- *(p+1)=i_receive();
- i_ack();
- *p=i_receive();
- i_stop();
- delay();
- return(1);
- }
- else
- {
- i_stop();
- delay();
- return(0);
- }
- }
- else
- {
- i_stop();
- delay();
- return(0);
- }
- }
- else
- {
- i_stop();
- delay();
- return(0);
- }
- }
-
- void main(void)
- {
- P1=0xff;
- timecount=0;
- displaycount=0;
- TMOD=0x21;
- TH1=0x06;
- TL1=0x06;
- TR1=1;
- ET1=1;
- ET0=1;
- EA=1;
-
- if(start_temperature_T()) //向DS1624發(fā)送啟動A/D溫度轉(zhuǎn)換命令,成功則啟動T0定時1s。
- {
- secondflag=0;
- secondcount=0;
- TH0=55536/256;
- TL0=55536%256;
- TR0=1;
- }
- while(1)
- {
- if(secondflag==1)
- {
- secondflag=0;
- TR0=0;
- if(read_temperature_T(temperdata)) //T0定時1s時間到,讀取DS1624的溫度值
- {
- for(x=0;x<8;x++)
- {
- displaybuffer[x]=16;
- }
- x=2;
- result=temperdata[1]; //將讀取的溫度值進(jìn)行數(shù)據(jù)處理,并送到顯示緩沖區(qū)
- while(result/10)
- {
- displaybuffer[x]=result%10;
- result=result/10;
- x++;
- }
- displaybuffer[x]=result;
- result=temperdata[0];
- result=result>>3;
- displaybuffer[0]=(dotcode[result])%10;
- displaybuffer[1]=(dotcode[result])/10;
- if(start_temperature_T()) //溫度值數(shù)據(jù)處理完畢,重新啟動DS1624開始溫度轉(zhuǎn)換
- {
- secondflag=0;
- secondcount=0;
- TH0=55536/256;
- TL0=55536%256;
- TR0=1;
- }
- }
- }
- }
- }
- void t0(void) interrupt 1 using 0 //T0用于定時1s時間到
- {
- secondcount++;
- if(secondcount==100)
- {
- secondcount=0;
- secondflag=1;
- }
- TH0=55536/256;
- TL0=55536%256;
- }
- void t1(void) interrupt 3 using 0 //T1定時1ms用數(shù)碼管的動態(tài)刷新
- {
- timecount++;
- if(timecount==4) //T1定時1ms到
- {
- timecount=0;
- if (displaycount==5)
- {
- P0=(displaycode[displaybuffer[7-displaycount]] | 0x80); //在該位同時還要顯示小數(shù)點(diǎn)
- }
- else
- {
- P0=displaycode[displaybuffer[7-displaycount]];
- }
- P2=displaybit[displaycount];
- displaycount++;
- if(displaycount==8)
- {
- displaycount=0;
- }
- }
- }
復(fù)制代碼
|