| 
	
	|  |  |  
	
		| View previous topic :: View next topic |  
		| Author | Message |  
		| rrb011270 
 
 
 Joined: 07 Sep 2003
 Posts: 51
 
 
 
			      
 
 | 
			
				| Humidity Sensor Help...... |  
				|  Posted: Thu Sep 25, 2003 4:48 am |   |  
				| 
 |  
				| Mabuhay! 
 My code below will print to serial port monitor the linear relative humidity of a sensirion SHT15 humidity sensor.....
 
 Unfortunately, it displays none to the monitor...
 
 Anybody could check the code if I am missing something... or can provide me tips on how to improve the code....
 
 Tenkyu
   
 Code Below:
 
 #include <18F452.h> //Microcontroller specific library, e.g. port definitions
 //#device ICD=TRUE                  // with an ICD debugger
 #fuses HS,NOLVP,NOWDT,PUT           // high speed, no watch dog timer
 #use delay (clock=20000000)         // 20MHz clock
 //#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
 #use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7)
 #use fast_io(A)
 
 #include <math.h>
 
 #define DATA  PIN_A0
 #define SCK   PIN_A1
 
 #define noACK 0
 #define ACK   1
 //adr  command  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
 
 
 void sht1x_xmission_start()
 /*{
 output_high(DATA);
 output_low(SCK);
 delay_cycles(1);
 output_high(SCK);
 delay_cycles(1);
 output_low(DATA);
 delay_cycles(1);
 output_low(SCK);
 delay_cycles(1);
 delay_cycles(1);
 output_high(SCK);
 delay_cycles(1);
 output_high(DATA);
 delay_cycles(1);
 output_low(SCK);
 }*/
 {
 output_high(DATA);         // DATA=1;
 output_low(SCK);           // SCK=0;
 delay_us(2);               // _nop_();
 output_high(SCK);          // SCK=1;
 delay_us(2);               // _nop_();
 output_low(DATA);          // DATA=0;
 delay_us(2);               // _nop_();
 output_low(SCK);           // SCK=0;
 delay_us(2);               // _nop_();
 delay_us(2);               // _nop_();
 delay_us(2);               // _nop_();
 output_high(SCK);          // SCK=1;
 delay_us(2);               // _nop_();
 output_high(DATA);         // DATA=1;
 delay_us(2);               // _nop_();
 output_low(SCK);           // SCK=0;
 delay_us(2);
 }
 
 
 void sht1x_connection_reset()
 {
 char i;
 
 output_high(DATA);
 
 for (i=0; i<9; i++)
 {
 output_high(SCK);
 delay_us(2);
 output_low(SCK);
 delay_us(2);
 }
 sht1x_xmission_start();
 }
 
 
 main()
 {
 int i;
 float rh_lin;
 long value=0;
 long temp;
 
 float const C1 = -4.0;
 float const C2 = 0.0405;
 float const C3 = -0.0000028;
 
 
 // connection reset
 sht1x_connection_reset();
 
 while (TRUE)
 {
 //start transmission
 sht1x_xmission_start();
 
 //measure humidity (write address & command)
 for (i=128; i>0; i/=2)
 {
 if (i & MEASURE_HUMI) output_high(DATA);
 else output_low(DATA);
 
 output_high(SCK);
 delay_us(2);
 output_low(SCK);
 }
 
 set_tris_a(0x01);
 output_high(SCK);
 delay_us(2);
 output_low(SCK);
 
 //measure humidity (read data from SHT1x)
 for (i=0; i<17; i++)
 {
 if (i=8)
 {
 output_high(SCK);
 delay_us(2);
 output_low(SCK);
 }
 else
 {
 value<<=1;
 output_high(SCK);
 temp = input(DATA);
 
 if (temp) value|=1;
 
 delay_us(2);
 output_low(SCK);
 }
 }
 output_low(SCK);  // skip checksum
 set_tris_a(0x00);
 
 rh_lin = (C1+(C2*value)+(C3*value*value));
 printf("\nRelative Humidity: %2.2f",rh_lin);
 delay_ms(2000);
 }
 }
 
 Need Help on this....
 |  |  
		|  |  
		| Neutone 
 
 
 Joined: 08 Sep 2003
 Posts: 839
 Location: Houston
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Thu Sep 25, 2003 8:19 am |   |  
				| 
 |  
				| A lot of people think of decimal and base 10 as one and the same. I like to think as interger and decimal as types of numbers that are independant of the base. 
 float const C1 = -4.0;
 float const C2 = 0.0405;
 float const C3 = -0.0000028;
 rh_lin = (C1+(C2*value)+(C3*value*value));
 
 
 
 is more better expressed as
 
 const int8 C1 = 4;
 const int16 const C2 = 2654;    // 0.0405 * 2^16
 const int8 const C3 = 46;         // 0.0000028 * 2^24
 
 int32 temp1;
 int32 temp1;
 temp1 = C2*value;
 temp1 = temp1>>16;     // temp * 2^-16
 temp2=C3*value*value;
 temp2=temp2>>24;      // temp * 2^-24
 rh_lin = (temp1-temp2-C1);
 |  |  
		|  |  
		| rrb011270 
 
 
 Joined: 07 Sep 2003
 Posts: 51
 
 
 
			      
 
 | 
			
				|  |  
				|  Posted: Mon Sep 29, 2003 1:23 am |   |  
				| 
 |  
				| Mabuhay! 
 This is a working code for a sensirion SHT1x humidity/temperature sensor.
 
 I share it to everybody.. maybe u can use it in your project.
 
 ===================================
 
 #include <18F452.h>           // MCU specific library
 //#device ICD=TRUE              // with an ICD debugger
 #fuses HS,NOLVP,NOWDT,PUT     // high speed, no watch dog timer
 #use delay (clock=20000000)   // 20MHz clock
 
 //#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
 #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
 
 //#include <math.h>
 
 // set DATA to PortA1 and SCK to portA2
 #define DATA  PIN_A1
 #define SCK   PIN_A2
 
 #define noACK 0
 #define ACK   1
 
 // SHT1x address=000 is currently supported
 // SHT1x command code
 //adr  command  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
 
 // constant use for SHT1x Humidity Measurement
 #define C1  -4.0
 #define C2  0.0405
 #define C3  -0.0000028
 
 // constant use for SHT1x Temperature Measurement
 #define D1  -40.0
 #define D2  0.01
 
 // constant use for SHT1x True Humidity Measurement
 #define T1  0.01
 #define T2  0.00008
 
 
 // SHT1x Transmission Start Sequence
 void sht1x_xmission_start()
 {
 output_high(DATA);
 output_low(SCK);
 delay_us(2);
 output_high(SCK);
 delay_us(2);
 output_low(DATA);
 delay_us(2);
 output_low(SCK);
 delay_us(2);
 delay_us(2);
 delay_us(2);
 output_high(SCK);
 delay_us(2);
 output_high(DATA);
 delay_us(2);
 output_low(SCK);
 delay_us(2);
 }
 
 
 // SHT1x Connection Reset Sequence
 void sht1x_connection_reset()
 {
 int i;
 
 output_high(DATA);
 
 for (i=0; i<9; i++)
 {
 output_high(SCK);
 delay_us(2);
 output_low(SCK);
 delay_us(2);
 }
 sht1x_xmission_start();
 }
 
 
 // SHT1x Address & Command Mode with address=000
 void sht1x_command_mode(int iMode)
 {
 int i;
 
 for (i=128; i>0; i/=2)
 {
 if (i & iMode) output_high(DATA);
 else  output_low(DATA);
 
 delay_us(2);
 output_high(SCK);
 delay_us(2);
 output_low(SCK);
 }
 
 output_float(DATA);
 delay_us(2);
 output_high(SCK);
 delay_us(2);
 output_low(SCK);
 
 // delay >11ms for 8-bit measurement
 // delay >55ms for 12-bit measurement
 // delay >210ms for 14-bit measurement
 // before data read from SHT1x
 delay_ms(250);
 }
 
 
 // SHT1x Soft Reset
 // resets the interface, clears the status register to default values
 // wait minimum 11ms before next command
 void sht1x_soft_reset()
 {
 sht1x_connection_reset();
 sht1x_command_mode(RESET);
 }
 
 
 // read data from SHT1x and store
 long sht1x_read_data()
 {
 int i;
 long lTmp;
 long lVal1=0;
 long lVal2=0;
 long lValue;
 
 // get MSB from SHT1x
 for (i=0; i<8; i++)
 {
 lVal1<<=1;
 output_high(SCK);
 lTmp = input(DATA);
 delay_us(2);
 output_low(SCK);
 delay_us(2);
 
 if (lTmp) lVal1|=1;
 }
 
 // acknowledge routine
 output_high(SCK);
 output_low(DATA);
 delay_us(2);
 output_float(DATA);
 output_low(SCK);
 delay_us(2);
 
 // get LSB from SHT1x
 for (i=0; i<8; i++)
 {
 lVal2<<=1;
 output_high(SCK);
 lTmp = input(DATA);
 delay_us(2);
 output_low(SCK);
 delay_us(2);
 
 if (lTmp) lVal2|=1;
 }
 
 lValue = make16(lVal1,lVal2);
 return(lValue);
 }
 
 
 
 // Main Program
 main()
 {
 float fRh_lin;
 float fRh_true;
 float fTemp_true;
 
 //long lValue_lin;
 long lValue_rh;
 long lValue_temp;
 
 setup_adc_ports(NO_ANALOGS);
 
 sht1x_connection_reset();
 
 
 while (TRUE)
 {
 // delay >11ms before next command
 delay_ms(12);
 
 // start transmission
 sht1x_xmission_start();
 
 // measure temperature
 sht1x_command_mode(MEASURE_TEMP);
 lValue_temp = sht1x_read_data();
 //printf("\nValue_temp: %lu ",lValue_temp);
 
 fTemp_true = (D1+(D2*lValue_temp));
 
 printf("\nTemperature: %2.2fC",fTemp_true);
 //printf("\n");
 
 // delay 11ms before next command
 delay_ms(12);
 
 // start transmission
 sht1x_xmission_start();
 
 // measure linear
 sht1x_command_mode(MEASURE_HUMI);
 lValue_rh = sht1x_read_data();
 //printf("\nValue_lin: %lu ",lValue_lin);
 
 fRh_lin = (C1+(C2*lValue_rh)+(C3*lValue_rh*lValue_rh));
 fRh_true = (((fTemp_true-25)*(T1+(T2*lValue_rh)))+fRh_lin);
 
 printf("\nLinear Relative Humidity: %2.2f%%",fRh_lin);
 printf("\nRelative Humidity: %2.2f%%",fRh_true);
 printf("\n");
 delay_ms(2000);
 }
 }[/i]
 |  |  
		|  |  
		| picprogrammer 
 
 
 Joined: 10 Sep 2003
 Posts: 35
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Nov 23, 2004 3:23 pm |   |  
				| 
 |  
				| Nearly good, the program will not read the LSB. Change the sht1x_read_DATA() with this, the ack routine is changed. 
 
 // read DATA from SHT1x and store
 long sht1x_read_DATA()
 {
 int i;
 long lTmp;
 long lVal1=0;
 long lVal2=0;
 long lValue;
 
 // get MSB from SHT1x
 for (i=0; i<8; i++)
 {
 lVal1<<=1;
 output_high(SCK);
 lTmp = input(DATA);
 delay_us(2);
 output_low(SCK);
 delay_us(2);
 
 if (lTmp) lVal1|=1;
 }
 
 // acknowledge routine
 output_low(Data);
 delay_us(2);
 output_high(SCK);
 delay_us(2);
 output_low(SCK);
 output_float(Data);
 delay_us(2);
 
 // get LSB from SHT1x
 for (i=0; i<8; i++)
 {
 lVal2<<=1;
 output_high(SCK);
 lTmp = input(DATA);
 delay_us(2);
 output_low(SCK);
 delay_us(2);
 
 if (lTmp) lVal2|=1;
 }
 
 // acknowledge routine
 output_high(Data);
 delay_us(2);
 output_high(SCK);
 delay_us(2);
 output_low(SCK);
 output_float(Data);
 delay_us(2);
 
 lValue = make16(lVal1,lVal2);
 return(lValue);
 }
 |  |  
		|  |  
		|  |  
  
	| 
 
 | You cannot post new topics in this forum You cannot reply to topics in this forum
 You cannot edit your posts in this forum
 You cannot delete your posts in this forum
 You cannot vote in polls in this forum
 
 |  
 Powered by phpBB © 2001, 2005 phpBB Group
 
 |