00001 #include "sht15.h"
00002 #include "pic_utils.h"
00003 #include "pic_serial.h"
00004
00005 #define sht15_write_sda() clear_bit(tris_array[sht15_sda_port - PORTA], sht15_sda_pin); // output
00006 #define sht15_read_sda() set_bit(tris_array[sht15_sda_port - PORTA], sht15_sda_pin); // input
00007
00008
00009 #define CHECK_TEMP 0b00000011
00010 #define CHECK_HUMD 0b00000101
00011 #define CHECK_STAT 0b00000111
00012 #define WRITE_STAT 0b00000110
00013
00014
00015 void sht15_setup(void)
00016 {
00017 clear_bit(tris_array[sht15_sda_port - PORTA], sht15_sda_pin);
00018 clear_bit(tris_array[sht15_sck_port - PORTA], sht15_sck_pin);
00019 }
00020
00021 uns16 sht15_read_humidity(void) {
00022
00023 uns16 response;
00024
00025 sht15_start();
00026 sht15_send_byte(CHECK_HUMD);
00027 response = sht15_read_byte16();
00028 response = response >> 4;
00029 response = sht15_fix_humidity_l(response);
00030 return response;
00031 }
00032
00033 uns16 sht15_read_temperature(void) {
00034
00035 uns16 response;
00036
00037 sht15_start();
00038 sht15_send_byte(CHECK_TEMP);
00039 response = sht15_read_byte16();
00040 print_str("\nT=");
00041 print_int(response);
00042 response = sht15_fix_temperature_h(response);
00043 print_str("\nT=");
00044 print_int(response);
00045
00046 }
00047
00048
00049 void sht15_read(void)
00050 {
00051 uns16 response;
00052
00053
00054 sht15_start();
00055
00056
00057 sht15_send_byte(CHECK_HUMD);
00058 response = sht15_read_byte16();
00059 print_str("\nH=");
00060 print_int(response);
00061 response = response >> 4;
00062 response = sht15_fix_humidity_l(response);
00063 print_str("\nH=");
00064 print_int(response);
00065
00066 sht15_start();
00067 sht15_send_byte(CHECK_TEMP);
00068 response = sht15_read_byte16();
00069 print_str("\nT=");
00070 print_int(response);
00071 response = sht15_fix_temperature_h(response);
00072 print_str("\nT=");
00073 print_int(response);
00074
00075 }
00076
00077 void sht15_send_byte(uns8 sht15_command)
00078 {
00079 uns8 i;
00080
00081
00082 sht15_write_sda();
00083
00084
00085 for(i = 0 ; i < 8 ; i++)
00086 {
00087 clear_pin(sht15_sck_port, sht15_sck_pin);
00088 change_pin(sht15_sda_port, sht15_sda_pin, sht15_command.7);
00089 sht15_command = sht15_command << 1;
00090 set_pin(sht15_sck_port, sht15_sck_pin);
00091 }
00092
00093
00094 clear_pin(sht15_sck_port, sht15_sck_pin);
00095 sht15_read_sda();
00096
00097 while (test_pin(sht15_sda_port, sht15_sda_pin) == 1);
00098 set_pin(sht15_sck_port, sht15_sck_pin);
00099 clear_pin(sht15_sck_port, sht15_sck_pin);
00100
00101 while (test_pin(sht15_sda_port, sht15_sda_pin) == 0);
00102
00103
00104
00105 i = 0;
00106 while ((test_pin(sht15_sda_port, sht15_sda_pin) == 1))
00107 {
00108 i++;
00109 if (i == 255) break;
00110
00111 delay_ms(10);
00112 }
00113
00114
00115 i *= 10;
00116
00117
00118
00119
00120 }
00121
00122
00123 void sht15_start(void)
00124 {
00125 sht15_write_sda();
00126 set_pin(sht15_sda_port, sht15_sda_pin);
00127 set_pin(sht15_sck_port, sht15_sck_pin);
00128
00129 clear_pin(sht15_sda_port, sht15_sda_pin);
00130 clear_pin(sht15_sck_port, sht15_sck_pin);
00131 set_pin(sht15_sck_port, sht15_sck_pin);
00132 set_pin(sht15_sda_port, sht15_sda_pin);
00133 clear_pin(sht15_sck_port, sht15_sck_pin);
00134 }
00135
00136
00137 uns16 sht15_read_byte16(void)
00138 {
00139 uns8 j;
00140 uns16 in_byte;
00141
00142 bit my_store_gie = intcon.GIE;
00143 kill_interrupts();
00144
00145 clear_pin(sht15_sck_port, sht15_sck_pin);
00146
00147 sht15_read_sda();
00148
00149 for(j = 0 ; j < 16 ; j++)
00150 {
00151 clear_pin(sht15_sck_port, sht15_sck_pin);
00152 nop();
00153 nop();
00154 nop();
00155 nop();
00156 nop();
00157 set_pin(sht15_sck_port, sht15_sck_pin);
00158 nop();
00159 in_byte = in_byte << 1;
00160 in_byte.0 = test_pin(sht15_sda_port, sht15_sda_pin);
00161 }
00162 intcon.GIE = my_store_gie;
00163 return(in_byte);
00164 }
00165
00166
00167 uns16 sht15_fix_humidity_h(uns16 sensor_out)
00168 {
00169
00170 long result;
00171
00172 if ( sensor_out <= 1712 ) {
00173 result = 143 * sensor_out - 8192;
00174
00175
00176 } else {
00177 result = 111 *sensor_out + 46288;
00178
00179
00180 }
00181
00182 result = result >> 12;
00183 return result;
00184 }
00185
00186 uns16 sht15_fix_humidity_l(uns8 sensor_out)
00187 {
00188
00189 uns16 result;
00190
00191 if ( sensor_out <= 107 ) {
00192 result = 143 * sensor_out;
00193 if (result < 512) { result = 512; }
00194 result -= 512;
00195 } else {
00196 result = 111 * sensor_out;
00197 result += 2893;
00198 if (result > 25600) { result = 25600; }
00199 }
00200
00201 result = result >> 8;
00202 return result;
00203 }
00204
00205 int16 sht15_fix_temperature_h(uns16 sensor_out)
00206 {
00207 int16 result;
00208
00209 result = -3963 + sensor_out;
00210 return result;
00211 }