////////////////////////#includeHardWare.h////////////////////////////////////////////////////////////////////////#defineHTU21ADDR0x80//оƬµÄµØÖ·#defineACK0#defineNACK1ucharCRC=0;//¶Áζȴ«¸ÐÆ÷ËùÓõÄÒ»¸öУÑéucharFlag_BDXS=0;//=1µ±Ç°ÔÚucharFlag_Tempr_Start=0;//ζȲɼ¯ÐèÒª¿ªÊ¼±êÖ¾ucharFlag_Tempr_StartOK=0;//ζȲɼ¯ÒѾ¿ªÊ¼±êÖ¾ucharFlag_Tempr_OK=0;//ζȲɼ¯Íê³É±ê־λʪ¶ÈͬʱҲÍê³ÉÁËucharFlag_TemprINT=0;//=1ζȳõʼ»¯OK/////////////////////////////////////////////////////////////////////////////voidTempWaitUs(uinta)//µÈ´ýµÄ΢ÃëÊýÌØÖ¸µ±Ç°µÄÖ÷Ƶ16777216{uchari;do{i=0;i++;i++;i++;i++;i++;i++;i++;i++;i++;i++;}while(--a);}//I2CÆô¶¯º¯ÊývoidI2C_STARTpnn(){SENSORSDA_DIR&=~SENSORSDA;//SENSORSDAÅäÖÃΪÊäÈ룬À¸ß×ÜÏßSENSORSCL_OUT|=SENSORSCL;TempWaitUs(4);//ÑÓʱ3.3USSENSORSDA_OUT&=~SENSORSDA;//SENSORSDAÀµÍSENSORSDA_DIR|=SENSORSDA;//SENSORSDAÅäÖÃΪÊä³öTempWaitUs(7);//ÑÓʱ6.6USSENSORSCL_OUT&=~SENSORSCL;TempWaitUs(7);//ÑÓʱ6.6US}//I2CÍ£Ö¹º¯ÊývoidI2C2_Stoppnn()//Ó¦´ð{SENSORSDA_OUT&=~SENSORSDA;//SENSORSDAÀµÍSENSORSDA_DIR|=SENSORSDA;//SENSORSDAÅäÖÃΪÊä³öSENSORSCL_OUT&=~SENSORSCL;//SENSORSCL=0TempWaitUs(4);//ÑÓʱ3.3USSENSORSCL_OUT|=SENSORSCL;//SENSORSCL=1TempWaitUs(4);//ÑÓʱ3.3USSENSORSDA_DIR&=~SENSORSDA;//SENSORSDA=1TempWaitUs(4);//ÑÓʱ3.3US}//Ö÷·¢ËÍ´Ó½ÓÊÕ8λÊý¾Ý+¼ì²âACKº¯ÊýucharI2C_TXpnn(ucharI2C_TX_DATA)//·¢ËÍÒ»¸ö×Ö½Ú+¼ì²âAck{uchari;SENSORSCL_OUT&=~SENSORSCL;//SENSORSCL=0for(i=0;i8;i++){if(I2C_TX_DATA&0x80){SENSORSDA_DIR&=~SENSORSDA;}else{SENSORSDA_OUT&=~SENSORSDA;SENSORSDA_DIR|=SENSORSDA;}TempWaitUs(4);//ÑÓʱ3.3USSENSORSCL_OUT|=SENSORSCL;//SENSORSCLÀ¸ßTempWaitUs(7);//ÑÓʱ6.6USSENSORSCL_OUT&=~SENSORSCL;//SENSORSCL=0TempWaitUs(4);//ÑÓʱ3.3USI2C_TX_DATA=1;//Ïò×óÒƶ¯Ò»Î»}SENSORSDA_DIR&=~SENSORSDA;//SENSORSDAÅäÖÃΪÊäÈ룬À¸ß×ÜÏßSENSORSCL_OUT|=SENSORSCL;//SENSORSCL=0TempWaitUs(7);//ÑÓʱ6.6USif(SENSORSDA_IN&SENSORSDA)//´ÓÉ豸ÎÞÏìÓ¦{SENSORSCL_OUT&=~SENSORSCL;//SENSORSCLÀµÍTempWaitUs(17);//ÑÓʱ16.5USreturnNACK;}else//´ÓÉ豸ÏìÓ¦{SENSORSCL_OUT&=~SENSORSCL;//SENSORSCLÀµÍTempWaitUs(17);//ÑÓʱ16.5USreturnACK;}}//Ö÷½ÓÊÕ´Ó·¢ËÍ8λÊý¾ÝucharI2C_RXpnn(ucharAckValue){uchari;ucharI2C_RX_DATA=0;SENSORSCL_OUT&=~SENSORSCL;//SENSORSCLÀµÍSENSORSDA_DIR&=~SENSORSDA;//SENSORSDAÅäÖÃΪ½ÓÊÕ״̬for(i=0;i8;i++){I2C_RX_DATA=1;//Ïò×óÒƶ¯Ò»Î»SENSORSCL_OUT|=SENSORSCL;//SENSORSCLÀ¸ßTempWaitUs(7);//ÑÓʱ6.6USif(SENSORSDA_IN&SENSORSDA){I2C_RX_DATA|=0x01;}else{I2C_RX_DATA&=0xfe;}SENSORSCL_OUT&=~SENSORSCL;TempWaitUs(4);//ÑÓʱ3.3US}//Ö÷»úÊÇ·ñÒªÇó´Ó»úÍ£Ö¹·¢Ëͺ¯Êýif(AckValue){SENSORSDA_DIR&=~SENSORSDA;//SENSORSDA_OUT=1;//Èç¹ûΪ1£¬ÒªÇó´Ó»úÍ£Ö¹·¢ËÍ}else{SENSORSDA_OUT&=~SENSORSDA;SENSORSDA_DIR|=SENSORSDA;//SENSORSDAÅäÖÃΪÊäÈëÊÍ·Å×ÜÏß}TempWaitUs(4);//ÑÓʱ3.3USSENSORSCL_OUT|=SENSORSCL;//SENSORSCLÀ¸ßTempWaitUs(17);//ÑÓʱ16.5USSENSORSCL_OUT&=~SENSORSCL;//SENSORSCLÀ¸ßSENSORSDA_DIR&=~SENSORSDA;TempWaitUs(1);//ÑÓʱ0.3USreturnI2C_RX_DATA;}ucharCRC_check(ucharcrc_bytes,uchar*pbuf,ucharchecksum)//ÅжÏУÑéºÍÊÇ·ñ¶Ô¶Ô·µ»Ø1{ucharcrc=0;uchari;ucharbyteCtr;for(byteCtr=0;byteCtrcrc_bytes;++byteCtr){crc^=*(pbuf+crc_bytes-1-byteCtr);for(i=8;i0;i--){if(crc&0x80){crc=(crc1)^0x0131;}else{crc=(crc1);}}}if(crc==checksum){return(1);}else{return(0);}}voidHtu_Read(void)//¶ÁÎÂʪ¶È{ucharj,k;ucharwhatdo;ulongwendu_check_shidu;//ÓÉÓÚζȱ仯¶Ôʪ¶ÈÖµÒýÆðµÄÐÞÕýÁ¿ucharCRC_temp;//CRCУÑéÖµucharHandT[2];//ÓÃÓÚ´æ·Å16λÊýintlstemp;ucharHTU21_TEST;//ÎÂʪ¶ÈÏìÓ¦´ÎÊýulongtemp;//16λֵÔÝ´æÆ÷ulongshidu=0;ulongwendu=0;for(k=0;k2;k++){if(k==0)whatdo=0xf3;//¶Á»ØÀ´µÄÊÇζÈelsewhatdo=0xf5;//¶Á»ØÀ´µÄÊÇʪ¶ÈI2C_STARTpnn();if(I2C_TXpnn(HTU21ADDR&0xfe)==ACK)//дµØÖ·{if(I2C_TXpnn(whatdo)==ACK)//ת»¯ÃüÁî{I2C_STARTpnn();HTU21_TEST=1;while(I2C_TXpnn(HTU21ADDR|0x01)==NACK)//¼ì²âÊÇ·ñ¶Ô¶ÁÊýÏìÓ¦{DelayMs(1);//ÑÓʱ50MSת»¯I2C_STARTpnn();HTU21_TEST++;//×î¶àÔ¼20´ÎÓÐÏìÓ¦.if(HTU21_TEST=40)//return0;//5´Îºó×Ô¶¯Í˳ö£¬·µ»ØÖ÷º¯Êý{This.HumVal=333;This.TempVal=333;return;}}HandT[1]=I2C_RXpnn(ACK);//¶Á¸ß°ËλHandT[0]=I2C_RXpnn(ACK);//¶ÁµÍ°ËλCRC_temp=I2C_RXpnn(NACK);//CRCУÑéλI2C2_Stoppnn();if(CRC_check(2,HandT,CRC_temp)){This.TempBadCount=0;}//УÑéºÍ¶Ô//CRCУÑ麯Êýelse{This.HumVal=444;This.TempVal=444;if(This.TempBadCount250)This.TempBadCount++;return;}//УÑéºÍ´í½áÊøHandT[0]&=0xfc;temp=((ulong)HandT[1])*((ulong)256)+(ulong)HandT[0];//¿ÉÄÜÊÇζȻòʪ¶Èif(whatdo==0xf5)//ÕâÊÇʪ¶È{temp=temp*(12500);temp=temp/(65536);temp=temp-(600);if(2500wendu){wendu_check_shidu=12*wendu;wendu_check_shidu=30000-wendu_check_shidu;wendu_check_shidu=wendu_check_shidu/100;shidu=temp-wendu_check_shidu;}else{wendu_check_shidu=12*wendu;wendu_check_shidu=wendu_check_shidu-30000;wendu_check_shidu=wendu_check_shidu/100;shidu=temp+wendu_check_shidu;}shidu=shidu/10;This.HumVal0=shidu;//ʪ¶ÈÆ«ÒÆÁ¿ÐÞÕýThis.HumVal=This.HumVal0+This.HumCoe;//¼ÓÆ«ÒÆÁ¿if(This.HumVal0)This.HumVal=0;elseif(This.HumVal999)This.HumVal=999;}//else//ÕâÊÇζÈ{if(temp17473){j=0;temp=17572*temp;temp=temp/65536;wendu=temp-4685;}else//¸ºÎ¶È{j=1;temp=17572*temp;temp=temp/65536;wendu=4685-temp;}wendu=wendu/10;if(j){This.TempVal0=0-wendu;}else{This.TempVal0=wendu;}//ζÈÆ«ÒÆÁ¿ÐÞÕýlstem