|
楼主 |
发表于 2008-4-28 15:37:22
|
显示全部楼层
这几天出去了,一直没有上,不好意思啦,我的程序:
/***********************************************************************************
Project: SHTxx demo program (V2.1)
Filename: SHTxx_Sample_Code.c
Prozessor: mega128L
Compiler: icc avr
Cystal: 7.3728MHz
Autor: fxl
Copyrigth: (c) Sensirion AG
***********************************************************************************/
#include <iom128v.h> //Microcontroller specific library, e.g. port definitions
#include <macros.h> //Keil library (is used for _nop()_ operation)
#include <math.h> //Keil library
#include <stdio.h> //Keil library
#include "1602LCD.h"
#include <stdlib.h>
#include <string.h>
typedef union
{ unsigned int i;
float f;
} value;
//----------------------------------------------------------------------------------
// modul-var
//----------------------------------------------------------------------------------
enum {TEMP,HUMI};
//--------------------------------
//PORTA initial
//--------------------------------
unsigned char Table_1[5];
unsigned char Table_2[5];
unsigned char Table_3[5];
unsigned char Table_4[5];
#define DATA_H PORTA|0X01
#define DATA_L PORTA&0XFE
#define SCK_H PORTA|0X02
#define SCK_L PORTA&0XFD
#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
//----------------------------------------------------------------------------------
char s_write_byte(unsigned char value)
//----------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
{
unsigned char i,error=0;
DDRA = 0B00000011;
for (i=0x80;i>0;i/=2) //shift bit for masking
{ if (i & value) PORTA=DATA_H; //masking value with i , write to SENSI-BUS
else PORTA=DATA_L;
delay_nus(2);
PORTA =SCK_H; //clk for SENSI-BUS
delay_nus(5); //pulswith approx. 5 us
PORTA =SCK_L;
}
PORTA=DATA_H; //release DATA-line
delay_nus(2);
DDRA = 0B00000010;
PORTA=SCK_H; //clk #9 for ack
delay_nus(2);
if (PINA&0X01 == 0X01) error = 1;
else error=0; //check ack (DATA will be pulled down by SHT11)
//////////////////////////////
if(error!=0)
{ LCD_write_char(0,0x01); //显示清屏
delay_nms(2);
LCD_write_string(0,0,"Error1");
}
/////////////////////////////
delay_nus(2);
PORTA = SCK_L;
DDRA = 0B00000010;
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;
DDRA = 0B00000010;
PORTA = DATA_H; //release DATA-line
for (i=0x80;i>0;i/=2) //shift bit for masking
{ PORTA = SCK_H; //clk for SENSI-BUS
delay_nus(2);
if ((PINA&0X01) == 0X01) val=(val | i); //read bit
PORTA = SCK_L;
delay_nus(2);
}
DDRA = 0B00000011;
if(ack == 1) PORTA = DATA_L; //in case of "ack==1" pull down DATA-Line
delay_nms(5);
PORTA = SCK_H; //clk #9 for ack
delay_nus(5); //pulswith approx. 5 us
PORTA = SCK_L;
PORTA = DATA_H; //release DATA-line
DDRA =0B00000010;
return val;
}
//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
DDRA = 0B00000011;
PORTA = DATA_H; PORTF = SCK_L; //Initial state
delay_nus(2);
PORTA = SCK_H;
delay_nus(2);
PORTA = DATA_L;
delay_nus(2);
PORTA = SCK_L;
delay_nus(5);
PORTA = SCK_H;
delay_nus(2);
PORTA = DATA_H;
delay_nus(2);
PORTA = SCK_L;
DDRA = 0B00000010;
}
//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
DDRA = 0B00000011;
PORTA = DATA_H; PORTA = SCK_L; //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{ PORTA = SCK_H;
delay_nus(1);
PORTA = SCK_L;
delay_nus(1);
}
s_transstart(); //transmission start
DDRA = 0B00000010;
}
//----------------------------------------------------------------------------------
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;
}
DDRA = 0B00000010;
for(i=0;i<65535;i++)
{
if(PINA|0XFE == 0XFE)
{
delay_nms(1000);
break;
}
} //wait until sensor has finished the measurement or timeout (~2 sec.) is reached
if(PINA&0X01 == 0X01) error+=1;
if(error!=0)
{ LCD_write_char(0,0x01); //显示清屏
delay_nms(2);
LCD_write_string(0,0,"Error3");
}
*(p_value+1) =s_read_byte(ACK); //read the first byte (MSB)
*(p_value)=s_read_byte(ACK); //read the second byte (LSB)
*p_checksum =s_read_byte(noACK); //read checksum
return error;
}
//----------------------------------------------------------------------------------------
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
//----------------------------------------------------------------------------------------
// calculates temperature [癈] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [癈]
//----------------------------------------------------------------------------------------
void calc_sth11(float *p_humidity ,float *p_temperature)
{
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 [癈]
int y;
int z;
t_C=t*0.01 - 40; //calc. temperature from ticks to [癈]
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 ]
y=(int)t_C;
itoa(Table_1,y,10);
z=(int)((t_C-y)*100);
itoa(Table_2,z,10);
y=(int)rh_true;
itoa(Table_3,y,10);
z=(int)((rh_true-y)*100);
itoa(Table_4,z,10);
}
//--------------------------------------------------------------------
float calc_dewpoint(float h,float t)
//--------------------------------------------------------------------
// calculates dew point
// input: humidity [%RH], temperature [癈]
// output: dew point [癈]
{ 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;
}
//----------------------------------------------------------------------------------
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 [癈]
// 4. calculate dew point [癈]
// 5. print temperature, humidity, dew point
{
value humi_val,temp_val;
float dew_point;
unsigned char error,checksum;
PORTA = 0X00;
DDRA = 0X02;
DDRD |= LCD_DATA;
DDRC |= LCD_RS | LCD_EN;
LCD_EN_PORT &= ~LCD_EN; //EN=0
LCD_init();
LCD_write_char(0,0x01); //显示清屏
delay_nms(2);
LCD_write_string(3,0,"Testing...");
LCD_write_string(2,1,"Please wait!");
delay_nms(2000);
s_connectionreset();
while(1)
{
error=0;
delay_nms(200);
error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
delay_nms(200);
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
LCD_write_char(0,0x01); //显示清屏
delay_nms(2);
LCD_write_string(0,0,"TEMP:");
LCD_write_string(5,0,Table_1);
LCD_write_string(7,0,".");
LCD_write_string(8,0,Table_2);
LCD_write_string(0,1,"HUMI:");
LCD_write_string(5,1,Table_3);
LCD_write_string(7,1,".");
LCD_write_string(8,1,Table_4);
delay_nms(2000);
}
//----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
delay_nms(1000);
//(be sure that the compiler doesn't eliminate this line!)
//-----------------------------------------------------------------------------------
}
}
其中有部分是LCD1602的显示程序,明显的很,你去掉就行了。。。
还得提示你一下,我用是是SHT71,好多地方说它和SHT11是一样的,我不知道是不是,但是经过这次调试我发现,SHT71的测量顺序绝对不是手册上说的那样,具体的就是本帖前面的部分了,祝你成功啊... |
|