ببینید بر طبق این دیتاشیتی که از اینترنت دیدم نوع برقراری ارتباط با این سنسور از طریق پروتکل İ2C هست . با این تفاسیر شما باید از این قسمت در CodeVision استفاده کنید . به لینک زیر یه نگاه بندازید ببینید مشکلتون برطرف میشه؟
سلام
من هم دارم روی این سنسور کار می کنم . اما اگه دیتاشیت رو دقت کرده باشید نوشته کاملا سازگار با I2C نیست.
باید حتما bitbang بشه.
من به نتیجه رسیدم میزارم توی همین پست.
ایم سنسور 2 حالت داره
هم دما رو تشخیص میده هم رطوبت
با i2c هم خروجی میده و دقت بسیار خوبی دار.
سنسور های دیگه رطوبت با دقت خوب خروجیش تغییر ظرفیت خازنی هست و درایو کردنشون با دقت خوب خیلی عذاب آوره.این عالیه.
درضمن این قطعه جزو قطعات شناسنامه داره.
فقط یکم گرونه
خدا گفت : به جهنم ببریدش، او برگشت و با تعجب به خدا نگاه کرد. خدا گفت : به بهشت ببریدش. فرشتگان پرسیدند: چرا؟! خدا گفت : او هنوز به من امیدوار است...
Project :
Version :
Date : 2008/12/06
Author : F4CG
Company : F4CG
Comments:
Chip type : ATmega16
Program type : Application
Clock frequency : 2.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
************************************************** ***/
/*********************************************
SHT1X Humidity and Temperature Sensor
AVR
Pin 1 GND
Pin 2 Data (PB0)
Pin 3 Serial Clock (PB1)
Pin 4 VDD (2.4V to 5.5V)
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
unsigned char key=16,uch_TempTime_Number=0;
int intTemp1=0,intTime1=0,intTemp2=0,intTime2=0,intTem p3=0,intTime3=0;
int intTemp4=0,intTime4=0,intTemp5=0,intTime5=0,intTem p6=0,intTime6=0;
PORTB.1 = 0; // ClockLow
DDRB.1 = 1; // SCK is an output
PORTB.0 = 0; // Always Zero
// Toggle DDRB.0 for Data
// LCD module initialization
lcd_init(16);
Start:
s_softreset();
print_temp_humy();
while (1)
{
delay_ms(500);
print_temp_humy();
goto Start;
}
}
//----------------------------------------------------------------------------------
// tulis byte ke SHT
//----------------------------------------------------------------------------------
char SHT_WriteByte(unsigned char value)
{
unsigned char i,error=0;
for (i=0x80;i>0;i/=2)
{
if (i & value) SHT_DATA_OUT=0;
else SHT_DATA_OUT=1;
SHT_SCK=1;
delay_us(5); //pulswith approx. 5 us
SHT_SCK=0;
}
SHT_DATA_OUT=0; //release DATA-line
SHT_SCK=1; //clk #9 for ack
error=SHT_DATA_IN; //check ack (DATA will be pulled down by SHT11)
SHT_SCK=0;
return error; //error=1 in case of no acknowledge
}
//----------------------------------------------------------------------------------
// baca byte dari SHT, dan berikan ACK
//----------------------------------------------------------------------------------
char SHT_ReadByte(unsigned char ack)
{
unsigned char i,val=0;
SHT_DATA_OUT=0; //release DATA-line
for (i=0x80;i>0;i/=2)
{
SHT_SCK=1;
if (SHT_DATA_IN) val=(val | i); //read bit
SHT_SCK=0;
}
SHT_DATA_OUT=ack; //in case of "ack==1" pull down DATA-Line
SHT_SCK=1; //clk #9 for ack
delay_us(5); //pulswith approx. 5 us
SHT_SCK=0;
SHT_DATA_OUT=0; //release DATA-line
return val;
}
char s_softreset(void)
{
unsigned char error=0;
s_connectionreset(); //reset communication
error+=SHT_WriteByte(RESET); //send RESET-command to sensor
return error; //error=1 in case of no response form the sensor
}
s_transstart(); //transmission start
switch(mode){ //send command to sensor
case TEMP : error+=SHT_WriteByte(MEASURE_TEMP); break;
case HUMI : error+=SHT_WriteByte(MEASURE_HUMI); break;
default : break;
}
for (i=0;i<65535;i++) if(SHT_DATA_IN==0) break; //wait until sensor has finished the measurement
if(SHT_DATA_IN) error+=1; // or timeout (~2 sec.) is reached
*(p_value+1) =SHT_ReadByte(ACK); //read the first byte (MSB)
*(p_value) =SHT_ReadByte(ACK); //read the second byte (LSB)
*p_checksum =SHT_ReadByte(noACK); //read checksum
return error;
}
//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=*p_temperature*0.01 - 40; //calc. temperature from ticks to [°C]
rh_lin=C3*(*p_humidity)*(*p_humidity) + C2*(*p_humidity) + C1; //calc. humidity from ticks to [%RH]
rh_true=(t_C-25)*(T1+T2*(*p_humidity))+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]
}
//--------------------------------------------------------------------
// calculates dew point
// input: humidity [%RH], temperature [°C]
// output: dew point [°C]
//--------------------------------------------------------------------
float calc_dewpoint(float h,float t)
{
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;
}
اینم برنامه کامل پایه های دیتا و کلاک sht باید بره به پایه های یک و 2 میکرو و lcd هم به پورت d وصل کنید طبق دستور کد ویژن دیگه چیز خاصی نداره
سه کس را شنیدم که غیبت رواست وز این درگذشتی چهارم خطاست
یکی پادشاهی ملامت پسند کز او بر دل خلق بینی گزند
حلال است از او نقل کردن خبر مگر خلق باشند از او بر حذر
دوم پرده بر بی حیائی متن که خود میآدرد پرده بر خویشتن
ز حوضش مدار ای برادر نگاه که او میآدرافتد به گردن به چاه
سوم کژ ترازوی ناراست خوی ز فعل بدش هرچه دانی بگوی
راستی یادم رفت بگم این برنامه دما و رطوبت رو بدست میاره و روی lcd با فاصله حدود 500 میلی ثانیه نمایش میده راستی مقاومت pull up پایه 2 میکرو که وصل میشه به پایه دیتای shtxx یادتون نره بدم اون کار نخواهد کرد در ضمن کریستال رو حتما داخلی 2 مگا انتخاب کنید چون در غیر این صورت برنامه جواب نخواهد داد البته اگه ضرورت داره که کریستال خارجی یاداخلی با فرکانس دیگه ای استفاده بشه راهش وجود داره یه تغییر کوچولو باید توی نرم افزارش بدی اون باشه واسه بعد اگه لازم شد بگو برات توضیح میدم حله؟؟
سه کس را شنیدم که غیبت رواست وز این درگذشتی چهارم خطاست
یکی پادشاهی ملامت پسند کز او بر دل خلق بینی گزند
حلال است از او نقل کردن خبر مگر خلق باشند از او بر حذر
دوم پرده بر بی حیائی متن که خود میآدرد پرده بر خویشتن
ز حوضش مدار ای برادر نگاه که او میآدرافتد به گردن به چاه
سوم کژ ترازوی ناراست خوی ز فعل بدش هرچه دانی بگوی
unsigned char s_write_byte(unsigned char value)
{
unsigned char i, error=0;
DATA_REG_OUT; //DATA as output
CLEAR_SCK;
for (i=0x80;i>0; i/=2) //mask
{
if (( i & value ))
{
SET_DATA;
}
else
{
CLEAR_DATA;
}
SET_SCK;
asm("nop"
CLEAR_SCK;
}
DATA_REG_IN; //DATA as input
asm("nop"
SET_SCK;
سلام من این برنامه رو تو avr stdio کامپایلش کردم این error ها رو داد
دلیلش چیه؟
Build started 29.8.2010 at 1831
avr-gcc -mmcu=atmega16 -Wall -gdwarf-2 -O0 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wp,-M,-MP,-MT,sh.o,-MF,dep/sh.o.d -c ../sh.c -o sh.o
../sh.c error: parse error before '...' token
../sh.c error: parse error before "volatile"
../sh.c error: parse error before "volatile"
../sh.c:56: error: parse error before "volatile"
../sh.c error: parse error before "volatile"
../sh.c error: parse error before "volatile"
../sh.c:62: error: parse error before "volatile"
../sh.c error: parse error before "volatile"
../sh.c error: parse error before "volatile"
../sh.c error: parse error before '}' token
../sh.c: In function `s_connectionreset':
../sh.c:175: warning: implicit declaration of function `s_transstart'
../sh.c: In function `main':
../sh.c:225: warning: implicit declaration of function `lcd_init'
../sh.c:227: warning: implicit declaration of function `write_text'
make: *** [sh.o] Error 1
Build failed with 11 errors and 3 warnings...
//----------------------------------------------------------------------------------
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
DATA=0; // else
SCK=1; //clk for SENSI-BUS
delay_us(5); //pulswith approx. 5 us
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
delay_us(5); //pulswith approx. 5 us
SCK=0;
DATA=1; //release DATA-line
return val;
}
//----------------------------------------------------------------------------------
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 init_usart()
{
SCON = 0x52;
TMOD = 0x20;
TCON = 0x69;
TH1 = 0xfd;
}
*/
//----------------------------------------------------------------------------------------
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]
}
//----------------------------------------------------------------------------------
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;
PORTB.1 = 0; // ClockLow
DDRB.1 = 1; // SCK is an output
PORTB.0 = 0; // Always Zero
// Toggle DDRB.0 for Data
// LCD module initialization
lcd_init(16);
// init_usart();
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
printf("temp:%5.1fC humi:%5.1f%% dew point:%5.1fC\n",temp_val.f,humi_val.f,dew_poi nt);
}
//----------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!)
//-----------------------------------------------------------------------------------
}
}
Warning: D:\micro\avr\project14\sht75.c(314): local variable 'humi_val' is used before it's value is set
Warning: D:\micro\avr\project14\sht75.c(314): local variable 'humi_val' is used before it's value is set
Error: D:\micro\avr\project14\sht75.c(46): invalid combination of type specifiers
ببخشید که این پست رو بعد از این همه مدت بالا آوردم ؛ من هم با این سنسور کار کردم و یه سری اشکالات تو برنامش داشت که برطرف کردم.
یکیش رو که دوستمون نوشتند، دیگریش هم عدم تطبیق با فرکانس های مختلف بود.
دیدگاه