Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem In Reading Data From SHT11 By ATMEGA32

Status
Not open for further replies.

bolandgoo

Newbie level 3
Joined
Dec 6, 2008
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,377
Hi,
I want to read temperature and humidity data from SHT11 by ATMEGA32. I use Code Vision compiler. The hardware is based on Figure 10 in the SHT11 datasheet.
The source code that I found was related to AT89s53 microcontroller. I changed something to prepare it to use with ATMEGA32. However the code had some errors that I think they don’t related to the kind of microcontroller. Here are some:
1) The “value” variable in the “s_write_byte” function cause an error.
After I changed this name to another, the compiler can compile it without any error.
2) I think the statement “if (error! = 0) “in the while is true every time and so I can’t read the data. After I changed it to “if (error==0) “the data replied.
3) The compiler has two important Warning. It says:
a) local variable ‘humi_val’ is used before it’s value is set.
b) local variable ‘temp_val’ is used before it’s value is set.
I was wondering if anybody would mind helping me to solve the problem.
Finally, I want to know, how can I translate the data that SHT11 will response to temperature and humidity? For example in the page 7 of its data sheet we can see 1073(Hex) equals 35.50% RH. How? What about temperature?
Thanks
 

Can you upload or post your code? We need to see your code before we can help you correct any errors.

---------- Post added at 04:24 ---------- Previous post was at 04:02 ----------

Finally, I want to know, how can I translate the data that SHT11 will response to temperature and humidity? For example in the page 7 of its data sheet we can see 1073(Hex) equals 35.50% RH. How? What about temperature?

Here are some projects which use the SHT11 with an AVR ATMEGA:

**broken link removed**

Temperature and humidity measurements with the AVR web-server

This example project is written in BASIC.
However it should be fairly easy for you to adapt the section which reads the SHTII and converts the reading to a meaningful value:

AN #116 - Reading the SHT11 Humidity sensor

After we see your code, we can help you further.

BigDog
 

Thnx, here is my code:


#include <mega32.h>
#include <delay.h>
#include <math.h>
#include <stdio.h>
typedef union
{
unsigned int i;
float f;
}
value;
enum {TEMP,HUMI};
#define DATA PORTC.1
#define SCK PORTC.0
#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
#define FOSC 8000000// Clock Speed
#define BAUD 9600
#define MYUBRR (FOSC/(16*BAUD))-1
#define CR 0X0D
#define LF 0X0A
//*************************************************************************************
char s_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) DATA=1; //masking value with i , write to SENSI-BUS
else DATA=0;
SCK=1; //clk for SENSI-BUS
//_nop_();_nop_();_nop_(); //pulswith approx. 5 us
delay_us(5);
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
}
//*************************************************************************************
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;
DATA=1; //release DATA-line
for (i=0x80;i>0;i/=2) //shift bit for masking
{
SCK=1; //clk for SENSI-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_();_nop_();_nop_(); //pulswith approx. 5 us
delay_us(5);
SCK=0;
DATA=1; //release DATA-line
return val;
}
//*************************************************************************************
void s_transstart(void)
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
DATA=1; SCK=0; //Initial state
//_nop_();
delay_us(2);
SCK=1;
//_nop_();
delay_us(2);
DATA=0;
//_nop_();
delay_us(2);
SCK=0;
//_nop_();_nop_();_nop_();
delay_us(5);
SCK=1;
//_nop_();
delay_us(2);
DATA=1;
//_nop_();
delay_us(2);
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;
DATA=1; SCK=0; //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{ SCK=1;
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(RESET); //send 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(STATUS_REG_R); //send command to sensor
*p_value=s_read_byte(ACK); //read status register (8-bit)
*p_checksum=s_read_byte(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(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 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;
}
//*************************************************************************************
void calc_sth11(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=*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]
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]
}
//*************************************************************************************
float calc_dewpoint(float h,float t)
// calculates dew point
// input: humidity [%RH], temperature [C]
// output: dew point [C]
{
float k,dew_point ;
k = (log10(h)-2)/0.4343 + (17.62*t)/(243.12+t);
dew_point = 243.12*k/(17.62-k);
return dew_point;
}
//*************************************************************************************
void USART_Init(unsigned int ubrr)
{
UCSRA=0X00;
UBRRH = (unsigned char)(ubrr>>8); // Set baud rate//
UBRRL = (unsigned char)ubrr;
UCSRB = 0X08; // Enable Transmitter//
UCSRC = 0B10000110; // Set frame format: 8data, 1stop bit//
}
//*************************************************************************************
void USART_Transmit(unsigned char info)
{
while ((UCSRA & 0b00100000)!=0b00100000); // Wait for empty transmit buffer //
UDR = info; // Put data into buffer, sends the data//
}
//*************************************************************************************
void main()
// 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 [C]
// 4. calculate dew point [C]
// 5. print temperature, humidity, dew point
{
value humi_val,temp_val;
float dew_point;
unsigned char error,checksum;
unsigned int i;
DDRC.0=1;
//init_uart();
USART_Init(MYUBRR);
s_connectionreset();
while(1)
{
error=0;
error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
if(error!=0) 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
dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
//send final data to serial interface (USART)
printf(“temp:%5.1fC humi:%5.1f%% dew point:%5.1fC\n”,temp_val.f,humi_val.f,dew_point);
}
//----------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!)
delay_ms(800);
//-----------------------------------------------------------------------------------
}
}
//*************************************************************************************
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top