Difference between revisions of "SHT7"

From Pinguino
Jump to: navigation, search
(Save mail from Jean-Pierre Mandon about interfacing a SHT7)
 
m (Re-indent)
Line 10: Line 10:
 
#include <string.h>
 
#include <string.h>
  
#define DATA 0           // SDA is connected to pin 0 of Pinguino
+
#define DATA 0             // SDA is connected to pin 0 of Pinguino
 
#define CLOCK 1            // SCK is connected to pin 1 of Pinguino
 
#define CLOCK 1            // SCK is connected to pin 1 of Pinguino
  
 
void setup()
 
void setup()
 
{
 
{
pinMode(DATA,OUTPUT);
+
pinMode(DATA,OUTPUT);
pinMode(CLOCK,OUTPUT);
+
pinMode(CLOCK,OUTPUT);
digitalWrite(DATA,1);
+
digitalWrite(DATA,1);
digitalWrite(CLOCK,0);
+
digitalWrite(CLOCK,0);
 
}
 
}
  
 
unsigned int send_command(unsigned char value)
 
unsigned int send_command(unsigned char value)
 
{
 
{
unsigned char i;
+
unsigned char i;
unsigned char lowbyte;
+
unsigned char lowbyte;
unsigned char highbyte;
+
unsigned char highbyte;
  
// start condition
+
// start condition
digitalWrite(DATA,1);
+
digitalWrite(DATA,1);
delay(1);
+
delay(1);
digitalWrite(CLOCK,HIGH);
+
digitalWrite(CLOCK,HIGH);
delay(1);
+
delay(1);
digitalWrite(DATA,LOW);
+
digitalWrite(DATA,LOW);
delay(1);
+
delay(1);
digitalWrite(CLOCK,LOW);
+
digitalWrite(CLOCK,LOW);
delay(1);
+
delay(1);
digitalWrite(CLOCK,HIGH);
+
digitalWrite(CLOCK,HIGH);
delay(1);
+
delay(1);
digitalWrite(DATA,HIGH);
+
digitalWrite(DATA,HIGH);
delay(1);
+
delay(1);
digitalWrite(CLOCK,LOW);
+
digitalWrite(CLOCK,LOW);
delay(1);
+
delay(1);
// send command
+
 
for (i=0;i<8;i++)
+
// send command
    {
+
for (i=0;i<8;i++)
    if ((value&0x80)==0x80) digitalWrite(DATA,1);
+
{
    else digitalWrite(DATA,0);
+
if ((value&0x80)==0x80)
    value=value<<1;
+
digitalWrite(DATA,1);
    delay(1);
+
else
    digitalWrite(CLOCK,1);
+
digitalWrite(DATA,0);
    delay(1);
+
value=value<<1;
    digitalWrite(CLOCK,0);
+
delay(1);
    delay(1);
+
digitalWrite(CLOCK,1);
    }
+
delay(1);
// wait for acknowledge   
+
digitalWrite(CLOCK,0);
pinMode(DATA,INPUT);
+
delay(1);
digitalWrite(CLOCK,1);
+
}
delay(5);
+
 
if (digitalRead(DATA)) return 0xFFFF;    // bad acknowledge
+
// wait for acknowledge   
digitalWrite(CLOCK,0);
+
pinMode(DATA,INPUT);
// wait for conversion time
+
digitalWrite(CLOCK,1);
delay(500);
+
delay(5);
// check the data line to know if measurement is OK
+
if (digitalRead(DATA))  
if (digitalRead(DATA)) return 0xFFFF;
+
return 0xFFFF;    // bad acknowledge
// reading measurement value high byte
+
digitalWrite(CLOCK,0);
highbyte=0;
+
 
for (i=0;i<8;i++)
+
// wait for conversion time
    {
+
delay(500);
    digitalWrite(CLOCK,1);
+
 
    delay(1);
+
// check the data line to know if measurement is OK
    if (digitalRead(DATA)) highbyte=highbyte+1;   
+
if (digitalRead(DATA))
    digitalWrite(CLOCK,0);
+
return 0xFFFF;
    delay(1);
+
 
    highbyte=highbyte<<1;
+
// reading measurement value high byte
    delay(1);
+
highbyte=0;
    }
+
for (i=0;i<8;i++)
// acknowledge MSB   
+
{
pinMode(DATA,OUTPUT);
+
digitalWrite(CLOCK,1);
digitalWrite(DATA,0);
+
delay(1);
digitalWrite(CLOCK,1);
+
if (digitalRead(DATA))
delay(1);
+
highbyte=highbyte+1;   
digitalWrite(CLOCK,0);
+
digitalWrite(CLOCK,0);
delay(1);
+
delay(1);
// reading measurement value low byte
+
highbyte=highbyte<<1;
pinMode(DATA,INPUT);
+
delay(1);
lowbyte=0;
+
}
for (i=0;i<8;i++)
+
 
    {
+
// acknowledge MSB   
    digitalWrite(CLOCK,1);
+
pinMode(DATA,OUTPUT);
    delay(1);
+
digitalWrite(DATA,0);
    if (digitalRead(DATA)) lowbyte=lowbyte+1;       
+
digitalWrite(CLOCK,1);
    digitalWrite(CLOCK,0);
+
delay(1);
    delay(1);
+
digitalWrite(CLOCK,0);
    lowbyte=lowbyte<<1;
+
delay(1);
    delay(1);
+
 
    }
+
// reading measurement value low byte
// Set High to DATA line to end communication   
+
pinMode(DATA,INPUT);
pinMode(DATA,OUTPUT);
+
lowbyte=0;
digitalWrite(DATA,1);   
+
for (i=0;i<8;i++)
return(lowbyte+(highbyte*256));
+
{
 +
digitalWrite(CLOCK,1);
 +
delay(1);
 +
if (digitalRead(DATA)) lowbyte=lowbyte+1;       
 +
digitalWrite(CLOCK,0);
 +
delay(1);
 +
lowbyte=lowbyte<<1;
 +
delay(1);
 +
}
 +
 
 +
// Set High to DATA line to end communication   
 +
pinMode(DATA,OUTPUT);
 +
digitalWrite(DATA,1);   
 +
return(lowbyte+(highbyte*256));
 
}
 
}
  
Line 105: Line 118:
 
void reset_SHT7()
 
void reset_SHT7()
 
{
 
{
unsigned char i;
+
unsigned char i;
pinMode(DATA,OUTPUT);   
+
pinMode(DATA,OUTPUT);   
digitalWrite(DATA,1);
+
digitalWrite(DATA,1);
for (i=0;i<10;i++)
+
for (i=0;i<10;i++)
    {
+
{
    digitalWrite(CLOCK,1);
+
digitalWrite(CLOCK,1);
    delay(1);
+
delay(1);
    digitalWrite(CLOCK,0);
+
digitalWrite(CLOCK,0);
    delay(1);
+
delay(1);
    }
+
}
 
}
 
}
  
 
void loop()
 
void loop()
 
{
 
{
unsigned int i;
+
unsigned int i;
unsigned char chaine[];
+
unsigned char chaine[];
float temperature;
+
float temperature;
  
delay(3000);
+
delay(3000);
CDC.print("start conversion\n\r",18);
+
CDC.print("start conversion\n\r",18);
while(1)
+
while(1)
    {
+
{
    delay(500);
+
delay(500);
    i=send_command(3);                           // send command 3 to SHT7 ( read temperature )
+
i=send_command(3);                         // send command 3 to SHT7 ( read temperature )
    if (i<65535)                                // if no error
+
if (i<65535)                                // if no error
        {
+
{
        temperature=(-40+(0.01*i))/4;           // convert measurement to real value
+
temperature=(-40+(0.01*i))/4;       // convert measurement to real value
        x_ftoa(temperature,chaine,2,2);           // convert float value to ascii
+
x_ftoa(temperature,chaine,2,2);     // convert float value to ascii
        strcat(chaine,"\n\r");                   // add CR/LF to string
+
strcat(chaine,"\n\r");             // add CR/LF to string
        CDC.print(chaine,strlen(chaine));       // print string on CDC
+
CDC.print(chaine,strlen(chaine));   // print string on CDC
        }
+
}
    reset_SHT7();
+
reset_SHT7();
    }
+
}
 
}
 
}
 
</source>
 
</source>

Revision as of 15:42, 28 March 2011

The SHT7 is a temperature and relative humidity sensor. It works like an I2C device, except it is not a really I2C.

This code use float to ascii conversion ( stdlib.h ) and print a float result on CDC.

// How to communicate with Pinguino and a SHT7 sensor
// Jean-Pierre Mandon 2011

#include <stdlib.h>
#include <string.h>

#define DATA 0             // SDA is connected to pin 0 of Pinguino
#define CLOCK 1            // SCK is connected to pin 1 of Pinguino

void setup()
{
	pinMode(DATA,OUTPUT);
	pinMode(CLOCK,OUTPUT);
	digitalWrite(DATA,1);
	digitalWrite(CLOCK,0);
}

unsigned int send_command(unsigned char value)
{
	unsigned char i;
	unsigned char lowbyte;
	unsigned char highbyte;

	// start condition
	digitalWrite(DATA,1);
	delay(1);
	digitalWrite(CLOCK,HIGH);
	delay(1);
	digitalWrite(DATA,LOW);
	delay(1);
	digitalWrite(CLOCK,LOW);
	delay(1);
	digitalWrite(CLOCK,HIGH);
	delay(1);
	digitalWrite(DATA,HIGH);
	delay(1);
	digitalWrite(CLOCK,LOW);
	delay(1);

	// send command
	for (i=0;i<8;i++)
	{
		if ((value&0x80)==0x80)
			digitalWrite(DATA,1);
		else
			digitalWrite(DATA,0);
		value=value<<1;
		delay(1);
		digitalWrite(CLOCK,1);
		delay(1);
		digitalWrite(CLOCK,0);
		delay(1);
	}

	// wait for acknowledge   
	pinMode(DATA,INPUT);
	digitalWrite(CLOCK,1);
	delay(5);
	if (digitalRead(DATA)) 
		return 0xFFFF;    // bad acknowledge
	digitalWrite(CLOCK,0);

	// wait for conversion time
	delay(500);

	// check the data line to know if measurement is OK
	if (digitalRead(DATA))
		return 0xFFFF;

	// reading measurement value high byte
	highbyte=0;
	for (i=0;i<8;i++)
	{
		digitalWrite(CLOCK,1);
		delay(1);
		if (digitalRead(DATA))
			highbyte=highbyte+1;   
		digitalWrite(CLOCK,0);
		delay(1);
		highbyte=highbyte<<1;
		delay(1);
	}

	// acknowledge MSB   
	pinMode(DATA,OUTPUT);
	digitalWrite(DATA,0);
	digitalWrite(CLOCK,1);
	delay(1);
	digitalWrite(CLOCK,0);
	delay(1);

	// reading measurement value low byte
	pinMode(DATA,INPUT);
	lowbyte=0;
	for (i=0;i<8;i++)
	{
		digitalWrite(CLOCK,1);
		delay(1);
		if (digitalRead(DATA)) lowbyte=lowbyte+1;       
		digitalWrite(CLOCK,0);
		delay(1);
		lowbyte=lowbyte<<1;
		delay(1);
	}

	// Set High to DATA line to end communication   
	pinMode(DATA,OUTPUT);
	digitalWrite(DATA,1);   
	return(lowbyte+(highbyte*256));
}

// reset sensor
void reset_SHT7()
{
	unsigned char i;
	pinMode(DATA,OUTPUT);   
	digitalWrite(DATA,1);
	for (i=0;i<10;i++)
	{
		digitalWrite(CLOCK,1);
		delay(1);
		digitalWrite(CLOCK,0);
		delay(1);
	}
}

void loop()
{
	unsigned int i;
	unsigned char chaine[];
	float temperature;

	delay(3000);
	CDC.print("start conversion\n\r",18);
	while(1)
	{
		delay(500);
		i=send_command(3);                          // send command 3 to SHT7 ( read temperature )
		if (i<65535)                                // if no error
		{
			temperature=(-40+(0.01*i))/4;       // convert measurement to real value
			x_ftoa(temperature,chaine,2,2);     // convert float value to ascii
			strcat(chaine,"\n\r");              // add CR/LF to string
			CDC.print(chaine,strlen(chaine));   // print string on CDC
		}
		reset_SHT7();
	}
}