problem in atmega32 interfacing with sht75

Status
Not open for further replies.

iydahdavid

Newbie level 3
Joined
Aug 27, 2012
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,328
I am working in atmega32 to read the temperature and humidity by using sht75.. I am working on the avr studio...My code compiled successfully but i didn't get o/p..I use the Proteus simulation ,it shows the clock pin as high and data pin as blinking,but i doesn't read anything from sht75..In Proteus simulation an error msg is shown(error in logic contentions)..I think my error is in software,where i use the DDR and PORT registers..already i got the exact o/p in AT89c52,in this case there is no DDR registers so i use the code directly from the sensirion(sht75) site..pls help me
My o/p is like -40.00
+0.10
My code is here

Code:
#include <stdint.h>
#include <stdio.h>
#include <avr/io.h>
#include <util/delay.h>
#include "global.h"
#include "rprintf.h"
#include "timer.h"
//#include "lcd.h"
#include "uart.h"
#include "math.h"


//typedef union
//{ 
//unsigned int i;
//float f;
//} value;
//----------------------------------------------------------------------------------
// modul-var
//----------------------------------------------------------------------------------
enum {TEMP,HUMI};
#define SHT_DDR			DDRC		// Port with clock and data pins
#define SHT_PORT		PORTC		// Port with clock and data pins
#define SHT_PIN			PINC		// Port with clock and data pins
#define SHT_CLOCK		7		// Pin used to output clock to SHT
#define SHT_DATA		6		// Pin used to read/output data from/to SHT
#define SHT_DELAY		25		// uS delay between clock rise/fall



//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 noACK   0
#define ACK     1

void enable_data(void) 	{ SHT_DDR |= (1 << SHT_DATA); }	// Drive DATA pin

// Set the DDR Register to 0 (low) and the PORT Register to 1 (high)
// Result: PIN is ready for Input with Pull-UP

void disable_data(void)	{ SHT_DDR  &= ~(1 << SHT_DATA);
			  SHT_PORT |= (1 << SHT_DATA); }

// Set DATA PORT Register to 1 (high)

void data_high(void)	{ SHT_PORT |= _BV(SHT_DATA); }

// Set DATA PORT Register to 0 (low)

void data_low(void)	{ SHT_PORT &= ~_BV(SHT_DATA); }

// Set CLOCK PORT Register to 1 (high)

void clock_high(void)	{ SHT_PORT |= _BV(SHT_CLOCK); }

// Set CLOCK PORT Register to 0 (low)

void clock_low(void)	{ SHT_PORT &= ~_BV(SHT_CLOCK); }

// Set CLOCK DDR Register to 1 (high)

void enable_clock(void)	{ SHT_DDR |= (1 << SHT_CLOCK); }






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

	// Set the DATA Line to Output

	enable_data();
	asm volatile ("nop"::);

	// Write each bit one at a time, LSB first

	for (i=0x80;i>0;i/=2)
	{
		if (i & value)
		{
			data_high(); // Write a 1
		}
		else
		{
			data_low();  // Write a 0
		}

		clock_high();
		asm volatile ("nop"::);
		asm volatile ("nop"::);
		clock_low();
		asm volatile ("nop"::);
		asm volatile ("nop"::);

	}
	disable_data();
	asm volatile ("nop"::);

	// Read ACK

	clock_high();
	asm volatile ("nop"::);
	asm volatile ("nop"::);

	error = bit_is_set(SHT_PIN, SHT_DATA);

	clock_low();

	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;

	// Set the DATA Line to Input. 
	// Important: If there is no external Pull-Up Resistor

	disable_data();
	asm volatile ("nop"::);

	// Read in 8 bits, LSB first

	for (i=0x80;i>0;i/=2)
	{
		clock_high();
		asm volatile ("nop"::);
		asm volatile ("nop"::);

		if (bit_is_set(SHT_PIN, SHT_DATA))
		{
			val=(val | i); // Got a Bit
		}
		clock_low();
		asm volatile ("nop"::);
		asm volatile ("nop"::);
	}

	// Send ACK
	enable_data();
	asm volatile ("nop"::);
	data_low();
	asm volatile ("nop"::);
	clock_high();
	asm volatile ("nop"::);
	asm volatile ("nop"::);
	asm volatile ("nop"::);
	clock_low();
	asm volatile ("nop"::);
	asm volatile ("nop"::);
	disable_data();

	return val;

}
//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
enable_data();
	asm volatile ("nop"::);
	data_high();
	clock_low();
	asm volatile ("nop"::);
	asm volatile ("nop"::);
	clock_high(); 
	asm volatile ("nop"::);
	asm volatile ("nop"::);
	data_low();
	asm volatile ("nop"::);
	asm volatile ("nop"::);
	clock_low();
	asm volatile ("nop"::);
	asm volatile ("nop"::);
	asm volatile ("nop"::);
	asm volatile ("nop"::);
	asm volatile ("nop"::);
	asm volatile ("nop"::);
	clock_high();
	asm volatile ("nop"::);
	asm volatile ("nop"::);
	data_high();
	asm volatile ("nop"::);
	asm volatile ("nop"::);
	clock_low();

}
//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;

	// Set DATA Line to Input

	enable_data();
	asm volatile ("nop"::);
	data_high();

	// Prepare CLOCK-Line for Pulses

	clock_low();
	
	// Send 9 Pulses on the CLOCK-Line

	for(i=0;i<10;i++)
	{
		clock_high();
		asm volatile ("nop"::);
		asm volatile ("nop"::);
		clock_low();
		asm volatile ("nop"::);
		asm volatile ("nop"::);
	}
	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 char 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 (! bit_is_set(SHT_PIN, SHT_DATA))
		{
			break;
		}
		_delay_ms(300);
//
}
if (! bit_is_set(SHT_PIN, SHT_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= -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 [°C]

t_C= ((t*0.01) - 40.1); //calc. temperature[°C]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 [°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;
}
//----------------------------------------------------------------------------------
int main(void)
//----------------------------------------------------------------------------------
// 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;
// initialize our libraries
	// initialize the UART (serial port)
	uartInit();
	// set the baud rate of the UART for our debug/reporting output
	uartSetBaudRate(9600);
	// initialize the timer system
	timerInit();

	// initialize rprintf system
	// - use uartSendByte as the output for all rprintf statements
	//   this will cause all rprintf library functions to direct their
	//   output to the uart
	// - rprintf can be made to output to any device which takes characters.
	//   You must write a function which takes an unsigned char as an argument
	//   and then pass this to rprintfInit like this: rprintfInit(YOUR_FUNCTION);
	rprintfInit(uartSendByte);

	// initialize vt100 library
	vt100Init();
	
	// clear the terminal screen
	vt100ClearScreen();
float humi_val,temp_val;
float dew_point;
unsigned char error,checksum;
unsigned int i;

//DDRC=0xFF; 
//DDRA=0xFF; 
//DDRB=0x07; 
//DDRD=0x00;
//PORTC=0x00; 

//sht init
enable_clock();	
clock_low();	
disable_data();


s_connectionreset();

while(1)
{ 
error=0;
//error+=s_read_statusreg((unsigned char *)&die_val.i);
error+=s_measure((unsigned char*) &humi_val,&checksum,HUMI); //measure humidity
error+=s_measure((unsigned char*) &temp_val,&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
//rprintfFloat(6,myflt2);

calc_sth11(&humi_val,&temp_val); //calculate humidity,temperature
dew_point=calc_dewpoint(humi_val,temp_val); //calculate dew point
//printf("temp:%5.1fC humi:%5.1f%% dew point:%5.1fC\n",temp_val.f,humi_val.f,dew_point);
//printf("id %5",die_val.i);
rprintfFloat(3,temp_val);
rprintf("\n");
rprintfFloat(3,humi_val);
rprintf("\n");
}
for (i=0;i<40000;i++); //(be sure that the compiler doesn't eliminate this line!)
}
return 0;
}
 

thanks..
Actually delay routine is the problem..I have changed my delay ,instead of using nop(),i use delayms and resets he watchdog timer.. Now i can get temp and humidity value
 

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…