[SOLVED] Help needed in SHT11 based temerature controller project using AT89C2051

Status
Not open for further replies.

Ganesh singh

Newbie level 5
Joined
Jan 23, 2010
Messages
10
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Location
Banglore
Activity points
1,379
Hi everybody,
I am using AT89C2051 for my Project . I copied and pasted the sample code3 from the SHT11 datasheet which is actually for AT89C51 0r AT89C52.... and changed the Pin's as P1^2 for data and P1^3 for clock. i am using the Kiel micro version compiler. now the problem is the data that i am sending in Serial port (9600 Bps,11.0592Mhz ) is very slow . per data it takes more that 4 seconds.
can any body help to solve the problem???.
the code is as below. and it is more than 2K
can anybody give the idea (or C program )how can i reduce the size?????


#include <AT892051.h>
#include <intrins.h>
#include <math.h>
#include <stdio.h>

/***** modul-var ******/

#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


sbit DATA = P1^2;
sbit SCK = P1^3;


typedef union
{ unsigned int i;
float f;
} value;

enum {TEMP,HUMI};



/***** writes a byte on the I2C-bus and checks the acknowledge *****/

char s_write_byte(unsigned char value)
{
unsigned char i,error=0;
for (i=0x80;i>0;i/=2) //shift bit for masking
{ if (i & value) DATA=1; //masking value with i , write to I2C-BUS
else DATA=0;
SCK=1; //clk for I2C-BUS
_nop_(); //pulswith approx. 5 us
_nop_();
_nop_();
// _nop_();
// _nop_();
// _nop_();
SCK=0;
}
DATA=1; //release DATA-line
SCK=1; //clk #9 for ack
error=DATA; //check ack (DATA will be pulled down by SHT11)
SCK=0;
return error; //error=1 in case of no acknowledge
}
/***** reads a byte form the I2C-bus and gives an acknowledge in case of "ack=1" *****/

char s_read_byte(unsigned char ack)
{
unsigned char i,val=0;
DATA=1; //release DATA-line
for (i=0x80;i>0;i/=2) //shift bit for masking
{ SCK=1; //clk for I2C-BUS
if (DATA) val=(val | i); //read bit
SCK=0;
}
DATA=!ack; //in case of "ack==1" pull down DATA-Line
SCK=1; //clk #9 for ack
// _nop_(); //pulswith approx. 5 us
// _nop_();
// _nop_();
_nop_();
_nop_();
_nop_();
SCK=0;
DATA=1; //release DATA-line
return val;
}

/***** generates a transmission start *****/

void s_transstart(void)

{
DATA=1; SCK=0; //Initial state
_nop_();
SCK=1;
_nop_();
DATA=0;
_nop_();
SCK=0;
// _nop_();
// _nop_();
// _nop_();
_nop_();
_nop_();
_nop_();
SCK=1;
_nop_();
DATA=1;
_nop_();
SCK=0;
}

/*** communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart ***/

void s_connectionreset(void)

{
unsigned char i;
DATA=1; SCK=0; //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{ SCK=1;
SCK=0;
}
s_transstart(); //transmission start
}

///**** makes a measurement (humidity/temperature) with checksum ****/
//
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
unsigned error=0;
unsigned int i;

s_transstart(); //transmission start
switch(mode){ //send command to sensor
case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
default : break;
}
for (i=0;i<65535;i++) if(DATA==0) break; //wait until sensor has finished the measurement
if(DATA) error+=1; // or timeout (~2 sec.) is reached
*(p_value) =s_read_byte(ACK); //read the first byte (MSB)
*(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
*p_checksum =s_read_byte(noACK); //read checksum
return error;
}

///********** SET RS232 PARAMETER **********/
//
void init_uart()
{SCON = 0x52; //9600 bps @ 11.059 MHz
TMOD = 0x20;
TCON = 0x69;
TH1 = 0xFD;
// TR1 = 1;
}


/********** calculate humidity & temperature **********/
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [°C]

void calc_sht11(float *p_humidity ,float *p_temperature)
{ const float C1=-4.0; // for 12 Bit
const float C2= 0.0405; // for 12 Bit
const float C3=-0.0000028; // for 12 Bit
const float T1=-0.01; // for 14 Bit
const float T2=0.00008; // for 14 Bit

float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature [íC]

t_C=t*0.01 - 40; //calc. temperature from ticks to [íC]
rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH]

*p_temperature=t_C; //return temperature [íC]
*p_humidity=rh_true; //return humidity[%RH]
}

/********** calculate dewpoint **********/
// calculates dew point
// input: humidity [%RH], temperature [íC]
// output: dew point [íC]

float calc_dewpoint(float h,float t)
{ float logEx,dew_point;
logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
return dew_point;
}

/********** MAIN **********/
// sample program that shows how to use SHT11 functions
// connection reset
// measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
// calculate humidity [%RH] and temperature [íC]
// calculate dew point [íC]
// print temperature, humidity, dew point to rs232
void main()

{
value humi_val,temp_val;

unsigned char checksum;
float dew_point;
init_uart();

while(1)
{
s_connectionreset();
s_measure((unsigned char *)&humi_val.i,&checksum,HUMI); //measure humidity
s_measure((unsigned char *)&temp_val.i,&checksum,TEMP); //measure temperature
humi_val.f=(float)humi_val.i; //converts integer to float

temp_val.f=(float)temp_val.i; //converts integer to float
calc_sht11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
printf("temp:%5.1fC humi:%5.1f%% dew point:%5.1fC\n",temp_val.f,humi_val.f,dew_point);


}
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…