shtxx_self.lst
来自「这是单片机驱动温湿度传感器SHT11温湿度传感器的源代码」· LST 代码 · 共 1,493 行 · 第 1/5 页
LST
1,493 行
860 1 unsigned char i,error=0;
861 1 for (i=0x80;i>0;i/=2) //shift bit for masking
862 1 { if (i & value) DATA=1; //masking value with i , write to SENSI-BUS
863 2 else DATA=0;
864 2 SCK=1; //clk for SENSI-BUS
865 2 _nop_();_nop_();_nop_(); //pulswith approx. 5 us
866 2 SCK=0;
867 2 }
868 1 DATA=1; //release DATA-line
869 1 SCK=1; //clk #9 for ack
870 1 error=DATA; //check ack (DATA will be pulled down by SHT11)
871 1 SCK=0;
872 1 return error; //error=1 in case of no acknowledge
873 1 }
874
875 //----------------------------------------------------------------------------------
876 char s_read_byte(unsigned char ack)
877 //----------------------------------------------------------------------------------
878 // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
879 {
880 1 unsigned char i,val=0;
881 1 DATA=1; //release DATA-line
882 1 for (i=0x80;i>0;i/=2) //shift bit for masking
883 1 { SCK=1; //clk for SENSI-BUS
884 2 if (DATA) val=(val | i); //read bit
885 2 SCK=0;
886 2 }
887 1 DATA=!ack; //in case of "ack==1" pull down DATA-Line
888 1 SCK=1; //clk #9 for ack
889 1 _nop_();_nop_();_nop_(); //pulswith approx. 5 us
890 1 SCK=0;
891 1 DATA=1; //release DATA-line
892 1 return val;
893 1 }
894
895 //----------------------------------------------------------------------------------
896 void s_transstart(void)
897 //----------------------------------------------------------------------------------
898 // generates a transmission start
899 // _____ ________
900 // DATA: |_______|
901 // ___ ___
902 // SCK : ___| |___| |______
903 {
904 1 DATA=1; SCK=0; //Initial state
905 1 _nop_();
906 1 SCK=1;
907 1 _nop_();
908 1 DATA=0;
909 1 _nop_();
910 1 SCK=0;
911 1 _nop_();_nop_();_nop_();
912 1 SCK=1;
913 1 _nop_();
914 1 DATA=1;
915 1 _nop_();
916 1 SCK=0;
917 1 }
918
919 //----------------------------------------------------------------------------------
920 void s_connectionreset(void)
921 //----------------------------------------------------------------------------------
C51 COMPILER V7.07 SHTXX_SELF 06/14/2007 21:21:35 PAGE 16
922 // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
923 // _____________________________________________________ ________
924 // DATA: |_______|
925 // _ _ _ _ _ _ _ _ _ ___ ___
926 // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
927 {
928 1 unsigned char i;
929 1 DATA=1; SCK=0; //Initial state
930 1 for(i=0;i<9;i++) //9 SCK cycles
931 1 { SCK=1;
932 2 SCK=0;
933 2 }
934 1 s_transstart(); //transmission start
935 1 }
936
937 //----------------------------------------------------------------------------------
938 char s_softreset(void)
939 //----------------------------------------------------------------------------------
940 // resets the sensor by a softreset
941 {
942 1 unsigned char error=0;
943 1 s_connectionreset(); //reset communication
944 1 error+=s_write_byte(RESET); //send RESET-command to sensor
945 1 return error; //error=1 in case of no response form the sensor
946 1 }
947
948 //----------------------------------------------------------------------------------
949 char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
950 //----------------------------------------------------------------------------------
951 // reads the status register with checksum (8-bit)
952 {
953 1 unsigned char error=0;
954 1 s_transstart(); //transmission start
955 1 error=s_write_byte(STATUS_REG_R); //send command to sensor
956 1 *p_value=s_read_byte(ACK); //read status register (8-bit)
957 1 *p_checksum=s_read_byte(noACK); //read checksum (8-bit)
958 1 return error; //error=1 in case of no response form the sensor
959 1 }
960
961 //----------------------------------------------------------------------------------
962 char s_write_statusreg(unsigned char *p_value)
963 //----------------------------------------------------------------------------------
964 // writes the status register with checksum (8-bit)
965 {
966 1 unsigned char error=0;
967 1 s_transstart(); //transmission start
968 1 error+=s_write_byte(STATUS_REG_W);//send command to sensor
969 1 error+=s_write_byte(*p_value); //send value of status register
970 1 return error; //error>=1 in case of no response form the sensor
971 1 }
972
973 //----------------------------------------------------------------------------------
974 char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
975 //----------------------------------------------------------------------------------
976 // makes a measurement (humidity/temperature) with checksum
977 {
978 1 unsigned error=0;
979 1 unsigned int i;
980 1
981 1 s_transstart(); //transmission start
982 1 switch(mode){ //send command to sensor
983 2 case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
C51 COMPILER V7.07 SHTXX_SELF 06/14/2007 21:21:35 PAGE 17
984 2 case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
985 2 default : break;
986 2 }
987 1 for (i=0;i<65535;i++) if(DATA==0) break; //wait until sensor has finished the measurement
988 1 if(DATA) error+=1; // or timeout (~2 sec.) is reached
989 1 *(p_value) =s_read_byte(ACK); //read the first byte (MSB)
990 1 *(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
991 1 *p_checksum =s_read_byte(noACK); //read checksum
992 1 return error;
993 1 }
994
995 //----------------------------------------------------------------------------------
996 void init_uart()
997 //----------------------------------------------------------------------------------
998 //9600 bps @ 11.059 MHz
999 {SCON = 0x52;
1000 1 TMOD = 0x20;
1001 1 TCON = 0x69;
1002 1 TH1 = 0xfd;
1003 1 }
1004
1005 //----------------------------------------------------------------------------------------
1006 void calc_sth11(float *p_humidity ,float *p_temperature)
1007 //----------------------------------------------------------------------------------------
1008 // calculates temperature [癈] and humidity [%RH]
1009 // input : humi [Ticks] (12 bit)
1010 // temp [Ticks] (14 bit)
1011 // output: humi [%RH]
1012 // temp [癈]
1013 { const float C1=-4.0; // for 12 Bit
1014 1 const float C2=+0.0405; // for 12 Bit
1015 1 const float C3=-0.0000028; // for 12 Bit
1016 1 const float T1=+0.01; // for 14 Bit @ 5V
1017 1 const float T2=+0.00008; // for 14 Bit @ 5V
1018 1
1019 1 float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
1020 1 float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
1021 1 float rh_lin; // rh_lin: Humidity linear
1022 1 float rh_true; // rh_true: Temperature compensated humidity
1023 1 float t_C; // t_C : Temperature [癈]
1024 1
1025 1 t_C=t*0.01 - 40; //calc. temperature from ticks to [癈]
1026 1 rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
1027 1 rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH]
1028 1 if(rh_true>100)rh_true=100; //cut if the value is outside of
1029 1 if(rh_true<0.1)rh_true=0.1; //the physical possible range
1030 1
1031 1 *p_temperature=t_C; //return temperature [癈]
1032 1 *p_humidity=rh_true; //return humidity[%RH]
1033 1 }
1034
1035 //--------------------------------------------------------------------
1036 float calc_dewpoint(float h,float t)
1037 //--------------------------------------------------------------------
1038 // calculates dew point
1039 // input: humidity [%RH], temperature [癈]
1040 // output: dew point [癈]
1041 { float logEx,dew_point;
1042 1 logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
1043 1 dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
1044 1 return dew_point;
1045 1 }
C51 COMPILER V7.07 SHTXX_SELF 06/14/2007 21:21:35 PAGE 18
1046
1047
1048 void cal_temp_humi(void) //计算温度和湿度
1049 {
1050 1 value humi_val,temp_val;
1051 1 unsigned char error,checksum;
1052 1 unsigned int i = 0;
1053 1
1054 1 // init_uart();
1055 1 s_connectionreset();
1056 1 // while(1)
1057 1 // {
1058 1 error=0;
1059 1 error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
1060 1 error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
1061 1 if(error!=0) s_connectionreset(); //in case of an error: connection reset
1062 1 else
1063 1 {
1064 2 humi_val.f=(float)humi_val.i; //converts integer to float
1065 2 temp_val.f=(float)temp_val.i; //converts integer to float
1066 2 calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
1067 2 // temp_s = (uint)temp_val.f;
1068 2 // humi_s = (uint)humi_val.f;
1069 2 b_data.temp = (uint)temp_val.f;
1070 2 b_data.humi = (uint)humi_val.f;
1071 2
1072 2 // dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
1073 2 // printf("temp:%5.1fC humi:%5.1f%% dew point:%5.1fC\n",temp_val.f,humi_val.f,dew_point);
1074 2 }
1075 1 //----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
1076 1 // for (i=0;i<40000;i++); //(be sure that the compiler doesn't eliminate this line!)
1077 1 //-----------------------------------------------------------------------------------
-
1078 1 }
1079 void cal_basic(void)
1080 {
1081 1 lcd_read_time();
1082 1 cal_temp_humi();
1083 1 }
1084 void main()
1085 {
1086 1 /*
1087 1 P0 = 0XFF;
1088 1 P1 = 0XFF;
1089 1 P2 = 0XFF;
1090 1 P3 = 0XFF;
1091 1
1092 1 P0 = 0X00;
1093 1 P1 = 0X00;
1094 1 P2 = 0X00;
1095 1 P3 = 0X00;
1096 1 */
1097 1
1098 1 uchar i = 0;
1099 1 uchar v = 0;
1100 1 uchar keydata = 0;
1101 1 uint dat_ad = 0;
1102 1 uchar num_ad = 0;
1103 1 float dat_ad_xian = 0;
1104 1 lcd_off();
1105 1 lcd_on();
1106 1 lcd_ini();
C51 COMPILER V7.07 SHTXX_SELF 06/14/2007 21:21:35 PAGE 19
1107 1 // lcd_wrc(0x80);
1108 1 /*
1109 1 for(i=0;i<60;i++)
1110 1 {
1111 1 lcd_wrd(0x00+i);
1112 1 }
1113 1 */
1114 1 delay(100);
1115 1 lcd_add(1,1);
1116 1
1117 1 for(i=0;i<4;i++)
1118 1 {
1119 2 lcd_dis_ch(0xb5,0xa1+i);
1120 2 }
1121 1
1122 1 // lcd_dis_ch(0xb5,0xa1);
1123 1 // lcd_dis_ch(0xb5,0xa1);
1124 1 // lcd_dis_ch(0xb5,0xa1);
1125 1 delay(200);
1126 1 // i=lcd_read_ac();
1127 1 // i=lcd_read_ac();
1128 1 i=lcd_read_ac();
1129 1 // delay(200);
1130 1 lcd_dis_num(i);
1131 1 lcd_dis_num(12458);
1132 1
1133 1 // I2C
1134 1 WP = 0;
1135 1
1136 1 // write to rom
1137 1 /*
1138 1 lcd_add(3,1);
1139 1
1140 1 i = ISendStr_16(0xa0,0x0000,19);
1141 1 lcd_wrd(0x30+i);
1142 1 */
1143 1 /*
1144 1 lcd_add(4,1);
1145 1 for(v=0x0000;v<8;v++)
1146 1 {
1147 1 i = 0;
1148 1 while(i == 0)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?