Temprature measurement with SHT11 sensor

Status
Not open for further replies.
bascom sht11

hey asena,
could you post the code you say you have to interface with pic18F series controllers. i have been working on this for some time now and am feeling a little lost. i would appreciate it very much.
thanks, eth0rupt
 

sht75 avr

as lot's of you asked for it, here is the ccs code for sensirion sht11 :

//------------------------------------------------------------------------------
// Module hygrometre-thermometre sensirion sht11
//
// adaptations JYP 2003
//
//------------------------------------------------------------------------------

#include "math.h"

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

enum {TEMP,HUMI};

#define sht_noACK 0
#define sht_ACK 1
//adr command r/w
#define sht_STATUS_REG_W 0x06 //000 0011 0
#define sht_STATUS_REG_R 0x07 //000 0011 1
#define sht_MEASURE_TEMP 0x03 //000 0001 1
#define sht_MEASURE_HUMI 0x05 //000 0010 1
#define sht_RESET 0x1e //000 1111 0

//------------------------------------------------------------------------------
char sht11_write_byte(unsigned char value)
//------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
{
unsigned char i,error=0;

for (i=0x80;i>0;i/=2) //shift bit for masking
{
if (i & value)
output_high(sht11_data); //masking value with i , write to SENSI-BUS
else
output_low(sht11_data);
output_high(sht11_sck); //clk for SENSI-BUS
delay_us( 5); //pulswith approx. 5 us
output_low(sht11_sck);
}
output_high(sht11_data); //release DATA-line
output_high(sht11_sck); //clk #9 for ack
error=input(sht11_data) ; //check ack (DATA will be pulled down by SHT11)
output_low(sht11_sck);
return error; //error=1 in case of no acknowledge
}

//------------------------------------------------------------------------------
char sht11_read_byte(unsigned char ack)
//------------------------------------------------------------------------------
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
{
unsigned char i,val=0;

output_high(sht11_data); //release DATA-line
for (i=0x80;i>0;i/=2) //shift bit for masking
{
output_high(sht11_sck); //clk for SENSI-BUS
if (input(sht11_data)==1)
val=(val | i); //read bit
output_low(sht11_sck);
}
output_bit(sht11_data,!ack); //in case of "ack==1" pull down DATA-Line
output_high(sht11_sck); //clk #9 for ack
delay_us( 5); //pulswith approx. 5 us
output_low(sht11_sck);
output_high(sht11_data); //release DATA-line
return val;
}

//------------------------------------------------------------------------------
void sht11_transstart(void)
//------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
output_high(sht11_data);
output_low(sht11_sck); //Initial state
delay_us( 1);
output_high(sht11_sck);
delay_us( 1);
output_low(sht11_data);
delay_us( 1);
output_low(sht11_sck);
delay_us( 3);
output_high(sht11_sck);
delay_us( 1);
output_high(sht11_data);
delay_us( 1);
output_low(sht11_sck);
}

//------------------------------------------------------------------------------
void sht11_connectionreset(void)
//------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles
// followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
output_high(sht11_data);
output_low(sht11_sck); //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{
output_high(sht11_sck);
output_low(sht11_sck);
}
sht11_transstart(); //transmission start
}

//------------------------------------------------------------------------------
char sht11_softreset(void)
//------------------------------------------------------------------------------
// resets the sensor by a softreset
{
unsigned char error=0;
sht11_connectionreset(); //reset communication
error+=sht11_write_byte(sht_RESET); //send RESET-command to sensor
return error; //error=1 in case of no response form the sensor
}

//------------------------------------------------------------------------------
char sht11_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
//------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
{
unsigned char error=0;
sht11_transstart(); //transmission start
error=sht11_write_byte(sht_STATUS_REG_R); //send command to sensor
*p_value=sht11_read_byte(sht_ACK); //read status register (8-bit)
*p_checksum=sht11_read_byte(sht_noACK); //read checksum (8-bit)
return error; //error=1 in case of no response form the sensor
}

//------------------------------------------------------------------------------
char sht11_write_statusreg(unsigned char *p_value)
//------------------------------------------------------------------------------
// writes the status register with checksum (8-bit)
{
unsigned char error=0;
sht11_transstart(); //transmission start
error+=sht11_write_byte(sht_STATUS_REG_W);//send command to sensor
error+=sht11_write_byte(*p_value); //send value of status register
return error; //error>=1 in case of no response form the sensor
}

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

sht11_transstart(); //transmission start
switch(mode)
{ //send command to sensor
case TEMP : error+=sht11_write_byte(sht_MEASURE_TEMP); break;
case HUMI : error+=sht11_write_byte(sht_MEASURE_HUMI); break;
default : break;
}

for (i=0;i<65535;i++)
if(input(sht11_data)==0)
break; //wait until sensor has finished the measurement
if(input(sht11_data)==1)
error+=1; // or timeout (~2 sec.) is reached

*(p_value+1) =sht11_read_byte(sht_ACK); //read the first byte (MSB)
*(p_value)=sht11_read_byte(sht_ACK); //read the second byte (LSB)
*p_checksum =sht11_read_byte(sht_noACK); //read checksum
return error;
}
char sht11_measure_temp(unsigned char *p_value, unsigned char *p_checksum)
{
return sht11_measure( p_value, p_checksum, TEMP);
}
char sht11_measure_humi(unsigned char *p_value, unsigned char *p_checksum)
{
return sht11_measure( p_value, p_checksum, HUMI);
}

//------------------------------------------------------------------------------
void sth11_calc(float *p_humidity ,float *p_temperature)
//------------------------------------------------------------------------------
// calculates temperature [°C] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [°C]
{ 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 @ 5V
const float T2=+0.00008; // for 14 Bit @ 5V

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

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]
if(rh_true>100)rh_true=100; //cut if the value is outside of
if(rh_true<0.1)rh_true=0.1; //the physical possible range

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

//--------------------------------------------------------------------
int sht11_calc_humid_int( int16 w_humidity)
//--------------------------------------------------------------------
{
// calcul de l'humidite en entier (sans calcul float)

int32 h1,h2;

h1 = ((int32) w_humidity) * ((int32) w_humidity);
h1 = h1 / (int32)1000;
h1 = h1 * (int32)28;
h2 = ((int32) w_humidity) * (int32)405;
h2 = h2 - h1;
h2 = h2 / (int32)1000;
h2 = h2 - (int32)40;
h2 = h2 / (int32)10;
return (h2);
}

//--------------------------------------------------------------------
int sht11_calc_temp_int( int16 w_temperature)
//--------------------------------------------------------------------
{
// calcul de la temperature en entier (sans calcul float)

int16 temp1;

temp1 = w_temperature / (int16)100;
temp1 = temp1 - (int16)40;
return (temp1);
}

//--------------------------------------------------------------------
int sht11_calc_temp_frac10( int16 w_temperature)
//--------------------------------------------------------------------
{
// calcul de la temperature en fractionnaire 0.X (sans calcul float)
// exemple si t=25.367 ° renvoie 3

int16 temp1;

temp1 = w_temperature / (int16)10;
temp1 = w_temperature - (int16)400;
temp1 = abs(temp1) - ((int16)10 * abs(sht11_calc_temp_int(w_temperature)));
return (temp1);
}

//--------------------------------------------------------------------
int sht11_calc_temp_frac100( int16 w_temperature)
//--------------------------------------------------------------------
{
// calcul de la temperature en fractionnaire 0.XX (sans calcul float)
// exemple si t=25.367 ° renvoie 36

int16 temp1;

temp1 = w_temperature - (int16)4000;
temp1 = abs(temp1) - ((int16)100 * abs(sht11_calc_temp_int(w_temperature)));
return (temp1);
}

//--------------------------------------------------------------------
float sht11_calc_dewpoint(float h,float t)
//--------------------------------------------------------------------
// calculates dew point
// input: humidity [%RH], temperature [°C]
// output: dew point [°C]
{
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;
}
 

sht11 + avr + circuit

I'm very pleased for your reply about the code.
But I still have a doubt: Which microcontroller is this code for?

Regards,
Sylvia Karla
 

sht71 pic assembler

this is ccs source code for the sht11 humidity sensor, and it will compile on I think all pic the ccs can compile to !
I tested it with a 16f876 and it works fine.
 
Reactions: galgo

    galgo

    Points: 2
    Helpful Answer Positive Rating
winavr sht11

hey there people,

could anyone post the picbasic code to interface with pic18F or 16F series controllers. this would be a huge help. thanks
 

i2c sht11

Kripton,

Would you mind to send me the code you tested with the PIC16f876?
I have doubts about how to use the CSS comands to read the temperature and humidity from the code you post. Which variables did you use to represent temperature and humidity? There are lots of them. I just want to show the temperature and humidity values in an LCD display; could you help me?


Thank you in advance.
Regards,
Sylvia
sylvia_karla@yahoo.com.br
 

sht11 avr code

Hi Kripton2035,

your code should use as a driver?
Regards.
 

avr + sht15

In which order (sequence) and format, in the main program, should I call the functions in the ccs code for sensirion sht11 posted here by Kripton ?

For example:
Sample program that shows how to use SHT11 functions

1. sht11_softreset();
2. sht11_connectionreset();
3. sht11_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);
4. sth11_calc(float *p_humidity,float *p_temperature);
5. sht11_calc_dewpoint(float h,float t);
6. print temperature, humidity, dew point.

void main()
{

float *p_humidity;
float *p_temperature;


sht11_softreset();
sht11_connectionreset();
sht11_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);
sth11_calc(*p_humidity,p_temperature);
sht11_calc_dewpoint(float h,float t);

printf("\fTemp:%3.2fC\nHumi:%3.2f%%",p_temperature,p_humidity);

}

Is this correct ?
Can anyone help me ?

Regards,
Sylvia Karla
 

sht11 sample

the file I uploaded is the transcription for CCS of the original work made by sensirion itself for the 805x in their appnote available here :
**broken link removed**

just read it and program the same way with the file I provided.

I did this thing some times ago and I dont remember all from it now !
 

atmel sht11

DOES ANYONE HAVE THE CODE FOR PICBASIC???? :?

seriously, any help in this matter would be GREATLY APPRECIATED
 

jonathan avr sht11

Hello,

I am currently designing a microprocessor contolled parrot incubator. I am using the SHT15 sensor (the SHT11 is out of stock), the ATMEL ATMega8 micro-controller, and the LM016L (2x16) LCD display. The trouble is I have never had to programme before and have no clue on how to write the code for the micro-controller. Please Help!

I was hoping someone could provide me with the AVR code which would interface the SHT15 with the ATMega8, and then output it to the LCD. By looking at the picture of the LCD on the first link, the connections are identical to the LM016L LCD.

Any help offered would be VERY MUCH appreciated! I can't afford to buy a proper incuabtor, they are way too expensive!

Many thanks indeed,

Steve.
E-mail: captaingrice@blueyonder.co.uk.
 

sensirion et picbasic

Hi
Take a look at w_w.fastavr.com
they have code for avr and sht11, also Bascom AVR site.
Hope this will help, but sht11 is difficult sensor to start with

anyway wish you luck
 

sht11 source code

Hi, thanks for the reply.

The trouble is, I have to do it in assembly code, using the AVR Studio 3.56. So far I have my 2x16 display outputting a user defined sentence, in my case, I've put in "Roll on Easter, 11 weeks to go"

My code so far is:

--------------------------

.include "m8def.inc"

.def temp =r16
.def disp =r17
.equ width =$00dd


;********************************************************
.equ LCD_O =portd ;Output to LCD
.equ LCD_I =pinb ;Input from LCD
.equ CTRL_O =portc ;Control output
.equ E =2 ;Active high enable
.equ DC =0 ;Data/Command
.equ BF =7 ;Busy Flag
.equ FS =$3f ;Function set
.equ DISP_ON =$0c ;Display on w/o blink or flash
.equ DISP_OFF =$08 ;Display, blink, cursor off
.equ DISP_CL =$01 ;Clear display
.equ DISP_EN =$06 ;Data entry mode
.equ LINE_1 =$80 ;DDRAM address for first line
.equ LINE_2 =$c0 ;DDRAM address for second line
.equ SH_LFT =$18 ;Left-shift display
.equ SH_RT =$1c ;Right-shift display
.equ HOME =$02 ;Return home
;********************************************************


reset: ldi temp, low(ramend)
out SPL, temp
ldi temp, high(ramend)
out SPH, temp

ser temp
out DDRB, temp
out DDRC, temp
out DDRD, temp

;rjmp pwm
rcall lcd_en
rcall del
rcall del
rcall del
rcall del
rcall del
ldi zl, low(Steve*2)
ldi zh, high(Steve*2)
loop2: lpm disp, z+
cpi disp, $ff
breq label
rcall wrt_d
rcall del
rjmp loop2

label: ldi disp, Line_2
rcall wrt_c
ldi zl, low(date*2)
ldi zh, high(date*2)
loop: lpm disp, z+
cpi disp, $ff
breq pwm
rcall wrt_d
rcall del
rjmp loop

;*********************************
pwm: ldi temp, $a1
out TCCR1A, temp
ldi temp, high(width)
out OCR1AH, temp
out OCR1BH, temp
ldi temp, low(width)
out OCR1AL, temp
out OCR1BL, temp
ldi temp, $01
out TCCR1B, temp


here: rjmp here


;********************************************************
;Set up lcd
LCD_EN: rcall del
rcall del
rcall del
rcall del
ldi disp, FS
rcall wrt_c
rcall wrt_c
rcall wrt_c
rcall wrt_c
rcall del
rcall del
ldi disp, DISP_ON
rcall wrt_c
ldi disp, DISP_EN
rcall wrt_c
ldi disp, DISP_CL
rcall wrt_c
rcall del
rcall del
ret
;********************************************************

;********************************************************
;Writes the command in the 'disp' register to the LCD.
wrt_c: rcall del
cbi CTRL_O, DC
sbi CTRL_O, E
out LCD_O, disp
cbi CTRL_O, E
ret
;********************************************************
;Writes data in the 'disp' register to the LCD.
wrt_d: rcall del
sbi CTRL_O, DC
sbi CTRL_O, E
out LCD_O, disp
cbi CTRL_O, E
ret
;********************************************************

;********************************************************
;64us delay using 8-bit timer
del: push temp
ldi temp, $df
out TCNT0, temp
ldi temp, $02
out TCCR0, temp
dloop: in temp, TIFR
andi temp, $01
cpi temp, $01
breq cflg
cpi temp, $08
brne dloop
cflg: ldi temp, $01
out TIFR, temp
clr temp
out TCCR0, temp
pop temp
ret
;********************************************************

Steve: .db "ROLL ON EASTER--",$ff,$ff
date: .db "11 WEEKS TO GO!!",$ff

------------------
The PWM is to control power resistors which I am using as a heater. The code you pointed me in the direction os great, but its in BASIC. I have to do it in assembly language. Can that code be converted to Assembly? If so, how?

Can you help me please? I don't have a clue! By the way, this code is not mine, it was given to me by someone - please dont think I can program :0).

Anyway, I hope you can help.

Many thanks,
Steve (captaingrice@blueyonder.co.uk)
 

sht11 with cvavr

Hello,
i wrote code for the sht-11 using avr-gcc some time ago.
Perhaps it will help you. I put the code below, like it is.
That means the comments are in german and there are some
missing code-parts (helper-functions like delay) and some parts
not necessary (because it was intended to run two of these sensors on
different inputs).
If this is a problem, tell me, and i will translate it.
Furthermore i do not garantie error-freeness.
Greets,

J. B.
 

sht11 winavr

JB,

Many thanks, can I ask as Im not familiar with programming, is this Assembly language?

If you have the time to translate into English that would be great!

Thank you very much indeed for your help, I really appreciate it.

Steve.
 

sht15 pic source

Hi Steve,

i added some comments and translated the existing ones to english. The programming-language is C. Good Luck

Johann
 
code avr sht11

addon: i know you need the assembly, but i have only the C-sources. The only thing i can do for you is to give you the disassembled C-Code. But disassembled C-Code isn't too easy to read. If it helps, here it is:
 

cvavr sht11

Hi i'm need help with my project , i'm building a device that read temp and humidity , i use a pic16f84 and a sht11 sencor also my device has a lcd display , please some one can help me with the source code for the pic , of any thing that can help me
 

sht11 pic basic

Do you have to use the pic16f84? And what language do you have to program in?

Steve
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…