/*
*******************************************************************************************************************
*								SHT10 sensor demonstration program, test on dummy data
* Description:  This program tests calc_sht10() funtionality for precision against the original
*				sht11 demo program (V2.05) released by Sensirion.
*				ORIGINAL, floating point math:
*				void calc_sth11(float *p_humidity ,float *p_temperature)
*				NOW, 2*linear results:
*				void calc_sht10(unsigned int *p_humidity ,int *p_temperature)
*				Computation results calculated by floating point method is compared against 2*linear results
*				in an EXCEL file.
*				Use either Docklight or Serial Window #1 by Ctrl+F5 Debug session
* File:			SHTxx_dummy_code.c
* Software:		Keil C (C51) compiler, Eval version 7.09 under uVision2 IDE v2.40
* Programmer: 	John Leung, TechToys Co. (www.TechToys.com.hk)
* Hardware:		AT89S52-gLCD-STK1
* Date:			30th May 2006
* Version:		1.0
*******************************************************************************************************************
*/

#include <REGX52.h> 		//Microcontroller specific library, e.g. port definitions
#include <stdio.h>   		//Keil library
#include <math.h>

//----------------------------------------------------------------------------------
void init_uart()
//----------------------------------------------------------------------------------
//9600 bps @ 11.059 MHz 
{SCON  = 0x52;    
 TMOD  = 0x20;    
 TCON  = 0x69;	  
 TH1   = 0xfd;    
}

//See an application note Non-linearity Compensation from www.Sensirion.com for details
//----------------------------------------------------------------------------------------
void calc_sht10(unsigned int *p_humidity ,int *p_temperature)
//----------------------------------------------------------------------------------------
// calculates temperature [Deg Cel] and humidity [%RH] 
// input :  humi [Ticks] (12 bit) 
//          temp [Ticks] (14 bit)
// output:  humi [10x %RH] for 1 decimal place
//          temp [100x Deg Cel] for 2 decimal places
{ 
	unsigned int rh = *p_humidity;
	int t 	= *p_temperature;
	int t_C;
	unsigned int rh_lin;

	//t_C=t*0.01-40
	//multiplied by 100, t_C=t-4000
	t_C = t - 4000;

	//58<=rh<=1720, 	(1430*rh-5120*16)/4096, 	in x10
	//1721<=rh<=3273, 	(1110*rh+28930*16)/4096, 	in x10
	//From : Application Note Non-Linearity compensation, Sensirion
	if(rh<=1720)
	{
		rh_lin=(1430UL*(long)rh)>>12;
		(rh_lin>=20)?(rh_lin = rh_lin-20):(rh_lin=0);
	} 
	else
	{
		//rh_lin = (1110*rh + 12320 + 4096*110)/4096, in x10
		rh_lin=((1110UL*(long)rh+12320UL)>>12)+110;
		if(rh_lin>1000) rh_lin=1000;
	}

	//rh_true = (t_C-25)*(rh*0.00008+0.01)+rh_lin
	//~0.12 %RH /deg C, this factor has been ignored at this moment
	*p_temperature = t_C;
	*p_humidity = rh_lin;
}


//----------------------------------------------------------------------------------
void main()
//----------------------------------------------------------------------------------
// Sample program that shows how to use calc_sht10() function
// 1. input 12 bit values for humi_val, its is dummy data for RH
// 2. calculate humidity [10x %RH]
// 3. print humidity value in one decimal place
// 4. input 14 bit values for temp_val, it is dummy data for testing
// 5. calculate temperature [100x Deg Cel]
// 6. print temperature value in two decimal places
// 7. use either Docklight or Serial Window #1 by Ctrl+F5 Debug session

{ unsigned int 	humi_val;
  int 			temp_val;
  unsigned int 	raw_data;

  init_uart();

  	//test on humidity, 12-bit range
  	for(raw_data=100;raw_data<3400;raw_data+=100)
	{
		humi_val=raw_data;
		calc_sht10(&humi_val, &temp_val);
		printf("raw_data: %d humidity: %d.%u \n", raw_data, humi_val/10, humi_val%10);
	}
  
	printf("BREAK \n");

	//test on temperature, 14-bit range
	for(raw_data=0;raw_data<16384;raw_data+=406)
	{
		temp_val=raw_data;
		calc_sht10(&humi_val, &temp_val);
		printf("raw_data: %d temp: %d.%u\n", raw_data, temp_val/100, abs(temp_val)%100/10);
	}

	while(1);
}
