wolfei
Junior Member level 1
- Joined
- Mar 15, 2010
- Messages
- 19
- Helped
- 11
- Reputation
- 22
- Reaction score
- 9
- Trophy points
- 1,283
- Activity points
- 1,413
/*******************************************************************************
*******************************************************************************/
#include "delay.h"
#include "lcd3310.c"
#include <htc.h>
__CONFIG(WDTDIS & PWRTEN & UNPROTECT & BORDIS & LVPDIS & DEBUGEN & WP2 & DUNPROT);
#include <math.h>
#include <stdio.h>
enum {TEMP,HUMI};
#define TRIS_DATA TRISB1 //data port condition: input/output
#define TRIS_SCK TRISB0
#define DATA RB1 //SHT11 DATA pin
#define SCK RB0 //STH11 SCK pin
#define noACK 0
#define ACK 1
//adrress commands from Sensirion //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
//Constants used for SHT Humidity Measurment
#define C1 -4.0
#define C2 0.0405
#define C3 -0.0000028
//Constants used for SHT Temeperature Measurment (14 Bit)
#define D1 -40.0
#define D2 0.01
// constant use for SHT True Humidity Measurement (12 Bit)
#define T1 0.01
#define T2 0.00008
/************************************************************
// writes a byte on the Sensibus and checks the acknowledge
***************************************************************/
//------------------------------------------------------------------------------
unsigned char s_write_byte(unsigned char value)
//------------------------------------------------------------------------------
{
unsigned char i,error=0;
TRIS_DATA = 0; //SHT11 WRITE...make DATA-line output
SCK=0; //initial state
for (i=0x80;i>0;i/=2) //shift bit for masking
{
DelayUs(5);
if (i & value)
DATA=1; //masking value with i , write to SENSI-BUS
else
DATA=0;
SCK=1; //clk for SENSI-BUS
DelayUs(5); //pulswith approx. 5 us
SCK=0;
}
TRIS_DATA = 1; //SHT11 READ..make DATA-line input
DelayUs(5);
SCK=1; //clk #9 for ack
DelayUs(5);
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 from the Sensibus and gives an acknowledge in case of "ack=1"
/****************************************************************************/
unsigned char s_read_byte(unsigned char ack)
{
unsigned char i,val=0;
TRIS_DATA = 1; //SHT11 READ
//DATA=1; //release DATA-line
for (i=0x80;i>0;i/=2) //shift bit for masking
{
DelayUs(5);
SCK=1; //clk for SENSI-BUS
if (DATA)
val=(val | i); //read bit
DelayUs(5);
SCK=0;
}
TRIS_DATA = 0; //make DATA-Line output
DATA=!ack; //in case of "ack==1" pull down DATA-Line
SCK=1; //clk #9 for ack
DelayUs(5); //pulswith approx. 5 us
SCK=0;
TRIS_DATA=1;
//DATA=1; //release DATA-line
return val;
}
//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
TRIS_DATA = 0; //SHT11 WRITE - DATA_Line output
DATA=1;
SCK=0; //Initial state
DelayUs(5);
SCK=1;
DelayUs(5);
DATA=0;
DelayUs(5);
SCK=0;
DelayUs(5);
SCK=1;
DelayUs(5);
DATA=1;
DelayUs(5);
SCK=0;
}
//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
TRIS_DATA = 0; //SHT11 WRITE
DATA=1; SCK=0; //Initial state
for(i=0;i<12;i++) //12 SCK cycles....more than 9 for safe being
{
SCK=1;
DelayUs(5);
SCK=0;
}
s_transstart(); //transmission start
}
void initialize(void)
{
lcd_init(); //initialize 3310LCD
lcd_clear(); //clear screen
}
void afisare (int numar, unsigned char linia, unsigned char coloana)
{
if(numar>99)
{
display_digit(linia,coloana,(numar/100)%10); //sute
display_digit(linia,coloana+10,(numar/10)%10); //zeci
display_digit(linia,coloana+20,numar%10); //unitati
}
else
{
lcd_gotoxy(coloana-1,linia-1); lcd_puts(" ");
lcd_gotoxy(coloana-1,linia); lcd_puts(" ");
display_digit(linia,coloana+10,(numar/10)%10); //zeci
display_digit(linia,coloana+20,numar%10); //unitati
}
}
//----------------------------------------------------------------------------------
void main()
//----------------------------------------------------------------------------------
// 1. Define Ports
// 2. Initalize LCD and reset connection to sensor
// 3. While Loop: calculate humidity [%RH] and temperature []C in 12BIT mod
// 4. print temperature, humidity, dew point
{
unsigned char error,checksum, byte_1, byte_2;
int lValue_Temp, lValue_Hum, i;
int fTemp_true,Rh_lin,fRh_True;
//CONFIG1 = 0b1110000011000101;
// OSCCON = 0b01100101;
// OSCTUNE = 0b00001111;
TRISD = 0x00;
TRISA = 0x00;
TRISB = 0x00;
TRISC = 0x00;
INTCON = 0x00; //disable interrupts
// ANSEL=0x00; //Digital AN[0:7]
// ANSELH = 0x00; //Digital AN[13:8]
ADCON0=0; //turn off A/D
initialize();
s_connectionreset();
DelayUs(20);
// s_write_statusreg(0x03); //low resolution data
while(1)
{
/********************************************************
Measure Temperature
********************************************************/
s_connectionreset();
s_transstart(); //transmission start
s_write_byte(MEASURE_TEMP); //measure Temperature
while (DATA); //wait until SHT11 ends measurements
//Save highest byte read, in RAM
byte_1 = s_read_byte(ACK);
//Save lowest byte read, in RAM
byte_2 = s_read_byte(noACK);
lValue_Temp = (byte_1*256) + byte_2; //combines two bytes (8 bit) into a 16 bit (word)
/********************************************************
temperature calculation
********************************************************/
fTemp_true = (D1+(D2*lValue_Temp)*100);
/********************************************************
Meausure Humidity
********************************************************/
s_connectionreset(); //Reset Sensor BUS
s_transstart(); //transmission start
s_write_byte(MEASURE_HUMI); //measure Humidity
while (DATA); //wait until SHT11 ends measurements
//Save highest byte read, in RAM
byte_1 =(unsigned char) s_read_byte(ACK);
//Save lowest byte read, in RAM
byte_2 = (unsigned char) s_read_byte(noACK);
lValue_Hum = ((unsigned char)byte_1*256) + (unsigned char)byte_2; //combines two bytes (8 bit) into a 16 bit (word)
/********************************************************
Humidity calculation
********************************************************/
Rh_lin = (C1+(C2*lValue_Hum)+(C3*lValue_Hum*lValue_Hum));
fRh_True = (((fTemp_true-25)*(T1+(T2*lValue_Hum)))+Rh_lin)*100;
if((fRh_True/100)>100) fRh_True=10000;
//if((fRh_True/100)<0.1)fRh_True=0.001;
/********************************************************
Print to LCD
********************************************************/
/*
char buffer[20];
sprintf(buffer, "T=%d.%d C H=%d.%d %%", fTemp_true/100, fTemp_true%100, fRh_True/100, fRh_True%100);
lcd_goto(0); // select first line
lcd_puts(buffer);
*/
afisare( fTemp_true, 1, 30);
afisare(fRh_True, 3, 30);
//----------wait approx. 0.8s to avoid heating up SHTxx----------------------
//-----------------------------------------------------------------------------------
for(i=0;i<2000;i++)
DelayUs(8000);
}
}
//------------------------------------------------------------------------------
// Module hygrometre-thermometre sensirion sht11
//
// adaptations JYP 2003
//
//------------------------------------------------------------------------------
#include "math.h"
#include <pic.h>
#include "delay.h"
typedef union
{ unsigned int i;
float f;
}
value;
enum {TEMP,HUMI};
#define SHT11_DATA RB1
#define SHT11_SCK RB0
#define SHT11_TRIS_DATA TRISB1
#define SHT11_TRIS_SCK TRISB0
#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
{
DelayUs(5);
if (i & value)
SHT11_DATA=1; //masking value with i , write to SENSI-BUS
else
SHT11_DATA=0;
SHT11_SCK=1; //clk for SENSI-BUS
DelayUs( 5); //pulswith approx. 5 us
SHT11_SCK=0;
}
SHT11_TRIS_DATA=1; //release DATA-line
DelayUs(5);
SHT11_SCK=1; //clk #9 for ack
error=SHT11_DATA; //check ack (DATA will be pulled down by SHT11)
SHT11_SCK=0;
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;
SHT11_TRIS_DATA=1; //release DATA-line
for (i=0x80;i>0;i/=2) //shift bit for masking
{
DelayUs(5);
SHT11_SCK=1; //clk for SENSI-BUS
if (SHT11_DATA)
val=(val | i); //read bit
DelayUs(5);
SHT11_SCK=0;
}
SHT11_TRIS_DATA=0; //make DATA-Line output
SHT11_DATA=!sht_ACK; //in case of "ack==1" pull down DATA-Line
SHT11_SCK=1; //clk #9 for ack
DelayUs( 5); //pulswith approx. 5 us
SHT11_SCK=0;
SHT11_TRIS_DATA=1; //release DATA-line
return val;
}
//------------------------------------------------------------------------------
void sht11_transstart(void)
//------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
SHT11_TRIS_DATA = 0; //SHT11 WRITE - DATA_Line output
SHT11_DATA=1;
SHT11_SCK=0; //Initial state
DelayUs( 5);
SHT11_SCK=1;
DelayUs( 5);
SHT11_DATA=0;
DelayUs( 5);
SHT11_SCK=0;
DelayUs( 5);
SHT11_SCK=1;
DelayUs( 1);
SHT11_DATA=1;
DelayUs( 1);
SHT11_SCK=0;
}
//------------------------------------------------------------------------------
void sht11_connectionreset(void)
//------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles
// followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
SHT11_TRIS_DATA=1; //SHT11 WRITE
SHT11_DATA=1;
SHT11_SCK=0; //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{
SHT11_SCK=1;
DelayUs(5);
SHT11_SCK=0;
}
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(SHT11_DATA==0)
break; //wait until sensor has finished the measurement
if(SHT11_DATA==1)
error+=1; // or timeout (~2 sec.) is reached
*(p_value) =sht11_read_byte(sht_ACK); //read the first byte (MSB)
*(p_value+1)=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 sht11_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( int w_humidity)
//--------------------------------------------------------------------
{
// calcul de l'humidite en entier (sans calcul float)
int h1,h2;
h1 = ((int) w_humidity) * ((int) 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;
}
/*******************************************************************************
*******************************************************************************/
#include "delay.h"
#include "lcd3310.c"
#include <htc.h>
#include "math.h"
#include "stdio.h"
//#include "serial.h"
enum {TEMP,HUMI};
#define TRIS_DATA TRISB1 //data port condition: input/output
#define TRIS_SCK TRISB0
#define DATA RB1 //SHT11 DATA pin
#define SCK RB0 //STH11 SCK pin
#define noACK 0
#define ACK 1
//adrress commands from Sensirion //adr ||command || r/w
#define MEASURE_TEMP 0x03 //000 0001 1
#define MEASURE_HUMI 0x05 //000 0010 1
//Constants used for SHT Humidity Measurment
#define C1 -4
#define C2 0.0405
#define C3 -0.0000028
//Constants used for SHT Temeperature Measurment (14 Bit)
#define D1 -40.0
#define D2 0.01
// constant use for SHT True Humidity Measurement (12 Bit)
#define T1 0.01
#define T2 0.00008
//PWM Heat Tape
#define HEAT_ON RB5=1
#define HEAT_OFF RB5=0
/************************************************************
// writes a byte on the Sensibus and checks the acknowledge
***************************************************************/
//------------------------------------------------------------------------------
unsigned char s_write_byte(unsigned char value)
//------------------------------------------------------------------------------
{
unsigned char i,error=0;
TRIS_DATA = 0; //SHT11 WRITE...make DATA-line output
SCK=0; //initial state
for (i=0x80;i>0;i/=2) //shift bit for masking
{
DelayUs(20);
if (i & value)
DATA=1; //masking value with i , write to SENSI-BUS
else
DATA=0;
SCK=1; //clk for SENSI-BUS
DelayUs(20); //pulswith approx. 5 us
SCK=0;
}
TRIS_DATA = 1; //SHT11 READ..make DATA-line input
DelayUs(20);
SCK=1; //clk #9 for ack
DelayUs(20);
SCK=0;
error = DATA; //check ack (DATA will be pulled down by SHT11)
return error; //error=1 in case of no acknowledge
}
/****************************************************************************/
// reads a byte from the Sensibus and gives an acknowledge in case of "ack=1"
/****************************************************************************/
unsigned char s_read_byte(unsigned char ack)
{
unsigned char i; int val=0;
TRIS_DATA = 1; //SHT11 READ
for (i=0x80;i>0;i/=2) //shift bit for masking
{
DelayUs(20);
SCK=1; //clk for SENSI-BUS
if (DATA)
val=(val | i); //read bit
DelayUs(20);
SCK=0;
}
TRIS_DATA = 0; //make DATA-Line output
DATA=!ack; //in case of "ack==1" pull down DATA-Line
SCK=1; //clk #9 for ack
DelayUs(20); //pulswith approx. 5 us
SCK=0;
TRIS_DATA=1;
return val;
}
//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
TRIS_DATA = 0; //SHT11 WRITE - DATA_Line output
DATA=1;
SCK=0; //Initial state
DelayUs(20);
SCK=1;
DelayUs(20);
DATA=0;
DelayUs(20);
SCK=0;
DelayUs(20);
SCK=1;
DelayUs(20);
DATA=1;
DelayUs(20);
SCK=0;
}
//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
TRIS_DATA = 0; //SHT11 WRITE
DATA=1; SCK=0; //Initial state
for(i=0;i<12;i++) //12 SCK cycles....more than 9 for safe being
{
SCK=1;
DelayUs(20);
SCK=0;
}
s_transstart(); //transmission start
}
float prev_Temp;
float prev_Hum;
float temp_true=23, Rh_lin, RH_true=50,dew_point=10,p;
float k;
void initialize(void)
{
lcd_init(); //initialize 3310LCD
lcd_clear(); //clear screen
}
void afisare (int numar, unsigned char linia, unsigned char coloana)
{
if(numar>99)
{
display_digit(linia,coloana,(numar/100)%10); //sute
display_digit(linia,coloana+10,(numar/10)%10); //zeci
display_digit(linia,coloana+20,numar%10); //unitati
}
else
{
lcd_gotoxy(coloana-1,linia-1); lcd_puts(" ");
lcd_gotoxy(coloana-1,linia); lcd_puts(" ");
display_digit(linia,coloana+10,(numar/10)%10); //zeci
display_digit(linia,coloana+20,numar%10); //unitati
}
}
//----------------------------------------------------------------------------------
void main()
//----------------------------------------------------------------------------------
// 1. Define Ports
// 2. Initalize LCD and reset connection to sensor
// 3. While Loop: calculate humidity [%RH] and temperature []C in 12BIT mod
// 4. print temperature, humidity, dew point
{
int lValue_Temp, lValue_Hum,i;
float k;
char buffer[20];
long int time=0;
unsigned char error,checksum, byte_1, byte_2,byte_0;
TRISA = 0x00;
TRISB = 0x00;
TRISC = 0x00;
ADCON0=0; //turn off A/D
initialize();
s_connectionreset();
DelayUs(20);
while(1)
{
/********************************************************
DewPoint Calculation
********************************************************/
if(temp_true!=NULL){
k = (log10(RH_true)-2)/0.4343 + (17.62*temp_true)/(243.12+temp_true);
//k+= (17.62*temp_true)/(243.12+temp_true);
dew_point = 243.12*k/(17.62-k);
}
prev_Temp = temp_true;
prev_Hum = RH_true;
/********************************************************
Measure Temperature
********************************************************/
s_connectionreset();
s_write_byte(MEASURE_TEMP); //measure Temperature
while(DATA);
//while (DATA); //wait until SHT11 ends measurements
//Save highest byte read, in RAM
byte_1 = s_read_byte(ACK);
//Save lowest byte read, in RAM
byte_2 = s_read_byte(noACK);
lValue_Temp = (byte_1*256) + byte_2; //combines two bytes (8 bit) into a 16 bit (word)
/********************************************************
temperature calculation
********************************************************/
temp_true = (D1+(D2*lValue_Temp));
/********************************************************
Meausure Humidity
********************************************************/
s_connectionreset(); //Reset Sensor BUS
s_transstart(); //transmission start
s_write_byte(MEASURE_HUMI); //measure Humidity
for(i=0;i<9999;i++)
{if (DATA==0) break;}
if (DATA=1) s_connectionreset();
//while (DATA); //wait until SHT11 ends measurements
//Save highest byte read, in RAM
byte_1 =(unsigned char) s_read_byte(ACK);
//Save lowest byte read, in RAM
byte_2 = (unsigned char) s_read_byte(noACK);
lValue_Hum = (byte_1*256) + byte_2; //combines two bytes (8 bit) into a 16 bit (word)
/********************************************************
Humidity calculation
********************************************************/
Rh_lin = (C1+(C2*lValue_Hum)+(C3*lValue_Hum*lValue_Hum));
RH_true = (((temp_true-25)*(T1+(T2*lValue_Hum)))+Rh_lin);
if((RH_true)>100) RH_true=100; //Ceiling for RH measurment
if((RH_true)<0.1) RH_true=0.1; //Floor for RH measurment
/********************************************************
Print to LCD
********************************************************/
/*
sprintf(buffer, "T=%.2f C RH=%.2f %%", temp_true, RH_true);
lcd_goto(0x00); // select first line
lcd_puts(buffer);
sprintf(buffer, "Dew Point= %.2f C", dew_point);
lcd_goto(0x40);
lcd_puts(buffer);
lcd_goto(0x14);
sprintf(buffer,"PREV T=%.2f C",prev_Temp);
lcd_puts(buffer);
lcd_goto(0x54);
sprintf(buffer,"PREV RH=%.2f %%",prev_Hum);
lcd_puts(buffer);
/********************************************************
Print to SERIAL
********************************************************
time+=10;
sprintf(buffer,"\rT= %.2f RH= %.2f Time= %d ",temp_true,RH_true,time);
puts(buffer);
*/
afisare( temp_true, 1, 30);
afisare(RH_true, 3, 30);
//----------wait approx. 0.8s to avoid heating up SHTxx----------------------
//-----------------------------------------------------------------------------------
DelayMs(220);
DelayMs(220);
}
}
// PIC16F877 + Nokia 3310 LCD
// Hi-Tech C
#include <pic.h>
__CONFIG(HS & WDTDIS & PWRTEN & BORDIS & LVPDIS & DUNPROT & WRTEN & DEBUGEN & UNPROTECT);
#include "delay.h"
#include "stdlib.h"
#include "lcd3310.c"
#include "SHT11.c"
void initialize(void)
{
GIE=0; // disable interrupts
ADCS1 = 0; //select Fosc/8
ADCS0 = 1;
ADCON1=0; // A/D port configuration 0
ADFM = 1; //right justified result
ADON=1; // turn on the AD conversion module
lcd_init(); //initialize 3310LCD
lcd_clear(); //clear screen
}
/* return a 10-bit result */
unsigned int read_adc(unsigned char channel)
{
channel&=0x07; // truncate channel to 3 bits
ADCON0&=0xC5; // clear current channel select
ADCON0|=(channel<<3); // apply the new channel select
DelayMs(10);
ADGO=1; // initiate conversion on the selected channel
while(ADGO)continue;
return(((ADRESH&0x03)<<8)+ADRESL); // return the 10-bit result
}
float mpx4115a_read(void) // for pressure sensor MPX4115A
{
int p_an,adc_sample;
float pres_mmHg;
p_an=0; // reset value
DelayMs(5);
adc_sample = read_adc(1);
DelayMs(10);
// 10 bits ADC is 1024 steps --> -Vref is 0 V and +Vref is 5 V
// 760 mmHg = 101.325 kPa
// Vout = VDD*(0.009*P - 0.095) it's a linear ecuation --> y = mX + b
// m = Prange / ADrange = 100 / 920 and b = Pmax - (m * ADmax) = 115 - (0.1087 * 975)
// P = 0.1087 * ADval + 9.0175
if (adc_sample>1023)return; // error detection
p_an=adc_sample;
pres_mmHg=0.1087*p_an+9.0175;
return (pres_mmHg);
}
void afisare (int numar, unsigned char linia, unsigned char coloana)
{
if(numar>99)
{
display_digit(linia,coloana,(numar/100)%10); //sute
display_digit(linia,coloana+10,(numar/10)%10); //zeci
display_digit(linia,coloana+20,numar%10); //unitati
}
else
{
lcd_gotoxy(coloana-1,linia-1); lcd_puts(" ");
lcd_gotoxy(coloana-1,linia); lcd_puts(" ");
display_digit(linia,coloana+10,(numar/10)%10); //zeci
display_digit(linia,coloana+20,numar%10); //unitati
}
}
void afisare_float (float numar_float, unsigned char linia, unsigned char coloana)
{
int numar;
numar=((int)numar_float);
if(numar>99)
{
display_digit(linia,coloana,(numar/100)%10); //sute
display_digit(linia,coloana+10,(numar/10)%10); //zeci
display_digit(linia,coloana+20,numar%10); //unitati
}
else
{
lcd_gotoxy(coloana-1,linia-1); lcd_puts(" ");
lcd_gotoxy(coloana-1,linia); lcd_puts(" ");
display_digit(linia,coloana+10,(numar/10)%10); //zeci
display_digit(linia,coloana+20,numar%10); //unitati
}
}
bit bAfisare;
void main(void)
{
int temp,pres;
float temp_sht, umid_sht, p_roua;
initialize();
DelayUs(20);
TRISB = 0x01;
TRISC = 0x00;
while(1)
{
s_connectionreset();
if (RB0)
{
DelayMs(100); /* soft debouncing*/
if (RB0)
{
RC1^=1; /* inverseaza RD1*/
bAfisare^=1; /*inverseaza bAfisare */
}
}
switch (bAfisare)
{
case 0 : lcd_init();
lcd_clear();
lcd_gotoxy(0,0); lcd_puts("T in: ");
lcd_gotoxy(70,0); lcd_puts("o");
lcd_gotoxy(0,3); lcd_puts("T out: ");
lcd_gotoxy(70,2); lcd_puts("o");
temp =read_adc(0)*((5.0*100.0)/1023.0);
afisare(temp, 1, 40);
DelayMs(10);
temp_sht=temperatura_sht();
afisare_float(temp_sht, 3, 40);
DelayMs(10);
break;
case 1:
default :
lcd_init();
lcd_clear();
lcd_gotoxy(0,0); lcd_puts("Pres: ");
lcd_gotoxy(60,0); lcd_puts("mmHg");
pres =mpx4115a_read();
DelayUs(250);
afisare(pres,1, 30);
lcd_gotoxy(0,3); lcd_puts("H: ");
lcd_gotoxy(70,3); lcd_puts("%");
umid_sht=umiditate_sht();
DelayUs(250);
afisare_float(umid_sht, 3, 30);
lcd_gotoxy(0,5); lcd_puts("DP: ");
lcd_gotoxy(70,5); lcd_puts("C");
p_roua=punct_roua();
DelayUs(250);
afisare_float(p_roua, 5, 30);
DelayMs(10);
break;
} //switch
DelayUs(250);
} //while(1)
}
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 /*********************************************************************************** Project: SHT1x/7x demo program (V2.4) Filename: SHT1x_sample_code.c Prozessor: 80C51 family Compiler: Keil Version 6.23a Autor: MST Copyrigth: (c) Sensirion AG ***********************************************************************************/ // Revisions: // V2.4 calc_sht11() Coefficients for humidity and temperature conversion // changed (for V4 sensors) // calc_dewpoint() New formula for dew point calculation #include <htc.h> #include <math.h> //Keil library #include <stdio.h> //Keil library #include "Def.h" typedef union { unsigned int i; float f; } value; //---------------------------------------------------------------------------------- // modul-var //---------------------------------------------------------------------------------- enum { SHT_TEMP, SHT_HUMI }; #define SHT_DATA RC6 // I/O #define SHT_SCK RC7 // O #define SHT_DATA_DIR TRISC6 #define SHT_DATA_RELEASE() SHT_DATA_DIR = INPUT_PIN #define SHT_DATA_OUTPUT() SHT_DATA_DIR = OUTPUT_PIN #define _nop_() asm("nop") #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 s_write_byte(unsigned char value) //---------------------------------------------------------------------------------- // writes a byte on the Sensibus and checks the acknowledge { unsigned char i,error=0; SHT_DATA_OUTPUT(); for (i=0x80;i>0;i/=2) //shift bit for masking { if (i & value) SHT_DATA=1; //masking value with i , write to SENSI-BUS else SHT_DATA=0; _nop_(); //observe setup time SHT_SCK=1; //clk for SENSI-BUS _nop_();_nop_();_nop_(); //pulswith approx. 5 us SHT_SCK=0; _nop_(); //observe hold time } SHT_DATA_RELEASE(); //SHT_DATA=1; //release SHT_DATA-line _nop_(); //observe setup time SHT_SCK=1; //clk #9 for ack error=SHT_DATA; //check ack (SHT_DATA will be pulled down by SHT11) SHT_SCK=0; return error; //error=1 in case of no acknowledge } //---------------------------------------------------------------------------------- char s_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; SHT_DATA_RELEASE(); //SHT_DATA=1; //release SHT_DATA-line for (i=0x80;i>0;i/=2) //shift bit for masking { SHT_SCK=1; //clk for SENSI-BUS if (SHT_DATA) val=(val | i); //read bit SHT_SCK=0; } SHT_DATA_OUTPUT(); SHT_DATA=!ack; //in case of "ack==1" pull down SHT_DATA-Line _nop_(); //observe setup time SHT_SCK=1; //clk #9 for ack _nop_();_nop_();_nop_(); //pulswith approx. 5 us SHT_SCK=0; _nop_(); //observe hold time SHT_DATA_RELEASE(); return val; } //---------------------------------------------------------------------------------- void s_transstart(void) //---------------------------------------------------------------------------------- // generates a transmission start // _____ ________ // SHT_DATA: |_______| // ___ ___ // SHT_SCK : ___| |___| |______ { SHT_DATA_OUTPUT(); SHT_DATA=1; SHT_SCK=0; //Initial state _nop_(); SHT_SCK=1; _nop_(); SHT_DATA=0; _nop_(); SHT_SCK=0; _nop_();_nop_();_nop_(); SHT_SCK=1; _nop_(); SHT_DATA=1; _nop_(); SHT_SCK=0; } //---------------------------------------------------------------------------------- void s_connectionreset(void) //---------------------------------------------------------------------------------- // communication reset: SHT_DATA-line=1 and at least 9 SHT_SCK cycles followed by transstart // _____________________________________________________ ________ // SHT_DATA: |_______| // _ _ _ _ _ _ _ _ _ ___ ___ // SHT_SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______ { unsigned char i; SHT_DATA_OUTPUT(); SHT_DATA=1; SHT_SCK=0; //Initial state for(i=0;i<9;i++) //9 SHT_SCK cycles { SHT_SCK=1; SHT_SCK=0; } s_transstart(); //transmission start } //---------------------------------------------------------------------------------- char s_softreset(void) //---------------------------------------------------------------------------------- // resets the sensor by a softreset { unsigned char error=0; s_connectionreset(); //reset communication error+=s_write_byte(SHT_RESET); //send SHT_RESET-command to sensor return error; //error=1 in case of no response form the sensor } //---------------------------------------------------------------------------------- char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum) //---------------------------------------------------------------------------------- // reads the status register with checksum (8-bit) { unsigned char error=0; s_transstart(); //transmission start error=s_write_byte(SHT_STATUS_REG_R); //send command to sensor *p_value=s_read_byte(SHT_ACK); //read status register (8-bit) *p_checksum=s_read_byte(SHT_noACK); //read checksum (8-bit) return error; //error=1 in case of no response form the sensor } //---------------------------------------------------------------------------------- char s_write_statusreg(unsigned char *p_value) //---------------------------------------------------------------------------------- // writes the status register with checksum (8-bit) { unsigned char error=0; s_transstart(); //transmission start error+=s_write_byte(SHT_STATUS_REG_W);//send command to sensor error+=s_write_byte(*p_value); //send value of status register return error; //error>=1 in case of no response form the sensor } //---------------------------------------------------------------------------------- char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode) //---------------------------------------------------------------------------------- // makes a measurement (humidity/temperature) with checksum { unsigned char error=0; unsigned int i; s_transstart(); //transmission start switch(mode) { //send command to sensor case SHT_TEMP : error+=s_write_byte(SHT_MEASURE_TEMP); break; case SHT_HUMI : error+=s_write_byte(SHT_MEASURE_HUMI); break; default : break; } SHT_DATA_RELEASE(); for (i=0;i<65535;i++) { if(SHT_DATA==0) break; //wait until sensor has finished the measurement } if(SHT_DATA) error+=1; // or timeout (~2 sec.) is reached *(p_value) =s_read_byte(SHT_ACK); //read the first byte (MSB) *(p_value+1)=s_read_byte(SHT_ACK); //read the second byte (LSB) *p_checksum =s_read_byte(SHT_noACK); //read checksum return error; } //---------------------------------------------------------------------------------------- void calc_sth11(float *p_humidity ,float *p_temperature) //---------------------------------------------------------------------------------------- // calculates temperature [癈] and humidity [%RH] // input : humi [Ticks] (12 bit) // temp [Ticks] (14 bit) // output: humi [%RH] // temp [癈] { const float C1=-2.0468; // for 12 Bit RH const float C2=+0.0367; // for 12 Bit RH const float C3=-0.0000015955; // for 12 Bit RH const float T1=+0.01; // for 12 Bit RH const float T2=+0.00008; // for 12 Bit RH 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 [癈] t_C=t*0.01 - 40.1; //calc. temperature [癈] from 14 bit temp. ticks @ 5V 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 [癈] *p_humidity=rh_true; //return humidity[%RH] } UCHAR str[20]; //---------------------------------------------------------------------------------- void test_humi() //---------------------------------------------------------------------------------- // sample program that shows how to use SHT11 functions // 1. connection reset // 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit) // 3. calculate humidity [%RH] and temperature [癈] // 4. calculate dew point [癈] // 5. print temperature, humidity, dew point { value humi_val,temp_val; float dew_point; unsigned char error,checksum; unsigned int i; //init_uart(); s_connectionreset(); while(1) { error=0; error+=s_measure((unsigned char*) &humi_val.i,&checksum,SHT_HUMI); //measure humidity error+=s_measure((unsigned char*) &temp_val.i,&checksum,SHT_TEMP); //measure temperature if(error!=0) { LCD_Gotoxy(0, 0); LCD_DispText("DHT error!! "); s_connectionreset(); //in case of an error: connection reset } else { humi_val.f=(float)humi_val.i; //converts integer to float temp_val.f=(float)temp_val.i; //converts integer to float calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature sprintf(str, "T:%4.1f HR:%4.1f%%",temp_val.f,humi_val.f); LCD_Gotoxy(0, 0); LCD_DispText(str); } //----------wait approx. 0.8s to avoid heating up SHTxx------------------------------ for (i=0;i<40000;i++); //(be sure that the compiler doesn't eliminate this line!) //----------------------------------------------------------------------------------- } }
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?