【正文】
數(shù)據(jù)傳輸 //adr mand r/w define STATUS_REG_W 0x06 //000 0011 0 define STATUS_REG_R 0x07 //000 0011 1 define MEASURE_TEMP 0x03 //000 0001 1 define MEASURE_HUMI 0x05 //000 0010 1 define RESET 0x1e //000 1111 0 /****************定義函數(shù)****************/void s_transstart(void)。 //連接復(fù)位函數(shù)char s_write_byte(unsigned char value)。 //SHT10讀函數(shù)char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)。//溫濕度補(bǔ)償/************************************************ 模塊名稱:s_transstart()。 SCK=0。 SCK=1。 DATA=0。 SCK=0。_ nop_()。 SCK=1。 DATA=1。 SCK=0。 功 能:連接復(fù)位函數(shù)************************************************/ void s_connectionreset(void) // munication reset: DATAline=1 and at least 9 SCK cycles followed by transstart { unsigned char i。 SCK=0。i9。 SCK=0。 //transmission start } /********************************************** 模塊名稱:s_write_byte()。 for (i=0x80。i/=2) //shift bit for masking { if (i amp。 //masking value with i , write to SENSIBUS else DATA=0。 //clk for SENSIBUS _nop_()。_nop_()。 } DATA=1。 //clk 9 for ack error=DATA。 _nop_()。_nop_()。 DATA=1。 //返回:0成功,1失敗} /**********************************************模塊名稱:s_read_byte()。 DATA=1。i0。 //clk for SENSIBUS if (DATA) val=(val | i)。_nop_()。 //pulswith approx. 3 us SCK=0。 //in case of ack==1 pull down DATALine else DATA=1。_nop_()。 //pulswith approx. 3 us SCK=1。_nop_()。 //pulswith approx. 3 us SCK=0。_nop_()。 //pulswith approx. 3 us DATA=1。 } /************************************************模塊名稱:s_measure()。 unsigned int i。 //transmission start switch(mode){ //send mand to sensor case TEMP : error+=s_write_byte(MEASURE_TEMP)。 case HUMI : error+=s_write_byte(MEASURE_HUMI)。 default : break。i65535。 //wait until sensor has finished the measurement if(DATA) error+=1。 //read the first byte (MSB) *(p_value+1)=s_read_byte(ACK)。 //read checksum return error。 功 能:溫濕度補(bǔ)償函數(shù)******************************************************/ void calc_sht10(float *p_humidity ,float *p_temperature)// calculates temperature [C] and humidity [%RH] // input : humi [Ticks] (12 bit) // temp [Ticks] (14 bit)// output: humi [%RH]// temp [C]{ const float C1=。 // for 12 Bit const float C3=。 // for 14 Bit 5V const float T2=+。 // rh: Humidity [Ticks] 12 Bit float t=*p_temperature。 // rh_lin: Humidity linear float rh_true。 // t_C: Temperature [C] t_C=t* 40。 //calc. humidity from ticks to [%RH] rh_true=(t_C25)*(T1+T2*rh)+rh_lin。 //cut if the value is outside of if(rh_true)rh_true=。 //return temperature [C] *p_humidity=rh_true。//定義上限值,初始值設(shè)定為65 ,設(shè)置為全局變量uchar key。 //+sbit SUBK=P1^4。 SUBK=1。amp。 if(key==0) { delay_n10us(100)。 } else { flag=0。 } if(SUBK==0) { if(flag0) { flag。 } while(!SUBK)。 //指示燈sbit beep=P3^6。 uint shijishidu。 uint wendu,shidu。 s_connectionreset()。 LCD_disp_str(0,2,R)。/*****************************初始化濕度顯示區(qū)***********************************/ LCD_disp_str(1,2, . %)。 // while(1) { error=0。,amp。 //measure humidity error+=s_measure((unsigned char*) amp。checksum,TEMP)。 //in case of an error: connection reset } else { keyscan()。039。 LCD_disp_char(9,1,(flag%10)+39。)。 //converts integer to float =(float)。,amp。 //calculate humidity, temperature wendu=10*。 LCD_disp_char(10,2,(wendu%1000)/100+39。)。039。 //顯示溫度個(gè)位 LCD_disp_char(12,2,0x2e)。039。 //顯示溫度小數(shù)點(diǎn)后第一位 LCD_disp_char(14,2,0xdf)。 shidu=10*。 LCD_disp_char(2,2,(shidu%1000)/100+39。)。039。 //顯示濕度個(gè)位 LCD_disp_char(5,2,(shidu%10)+39。)。 //實(shí)際的濕度值; if(flag=shijishidu) { led=0。 } if(flagshijishidu) { led=1。 } } //wait approx. to avoid heating up SHTxx delay_n10us(80000)。s needs. Until recently, all the temperature sensors on the market provided analog outputs. Thermistors, RTDs, and thermocouples were followed by another analogoutput device, the silicon temperature sensor. In most applications, unfortunately, these analogoutput devices require a parator, an ADC, or an amplifier at their output to make them useful. Thus, when higher levels of integration became feasible, temperature sensors with digital interfaces became available. These ICs are sold in a variety of forms, from simple devices that signal when a specific temperature has been exceeded to those that report both remote and local temperatures while providing warnings at programmed temperature settings. The choice now isn39。 there is a broad range of sensor types from which to choose. Classes of Temperature SensorsFour temperaturesensor types are illustrated in Figure 1. An ideal analog sensor provides an output voltage that is a perfectly linear function of temperature (A). In the digital I/O class of sensor (B), temperature data in the form of multiple 1s and 0s are passed to the microcontroller, often via a serial bus. Along the same bus, data are sent to the temperature sensor from the microcontroller, usually to set the temperature limit at which the alert pin