Các bạn,ace cho em hỏi với ạ.em có chương trình này. mà nó cứ bảo lỗi ở dòng 48. ace xem rồi sửa hộ em với ạ. em sửa mãi không dc. đây là code em lấy trên mạng. chỉ dịch ra thôi mà n báo lỗi. em muốn dựa vào cái code này để viết cho SHT75 mà nó báo lỗi như vậy
#include <REGX52.H>
#include <string.h>
#include <stdio.h>
sbit KEY_0 = P1^0;
sbit KEY_1 = P1^1;
sbit KEY_2 = P1^2;
sbit KEY_3 = P1^3;
sbit KEY_4 = P1^4;
sbit KEY_5 = P1^5;
sbit KEY_6 = P1^6;
sbit KEY_7 = P1^7;
sbit EN_75LBC184 = P3^5;
// */ lúc đầu báo lỗi ở đoạn này. sau khi xóa đi thì lại báo lỗi ở dòng 48.
typedef struct
{
unsigned int i;
float f;
} value;
enum {TEMP,HUMI};
sbit SHT15_SDA = P2^0; //SHT15_SHT75
sbit SHT15_SCK = P2^1; //SHT15_SHT75
#define noACK 0
#define ACK 1
#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
unsigned char SHT15_Write_Byte(unsigned char value)
{
unsigned char i,error=0;
for (i=0x80;i>0;i/=2)
{
if (i & value)
SHT15_SDA=1;
else
SHT15_SDA=0;
SHT15_SCK=1;
_nop_();
_nop_();
_nop_();
SHT15_SCK=0;
}
SHT15_SDA=1;
SHT15_SCK=1;
error=SHT15_SDA;
SHT15_SCK=0;
return error;
}
unsigned char SHT15_Read_Byte(unsigned char ack)
{
unsigned char i,val=0;
SHT15_SDA=1;
for (i=0x80;i>0;i/=2)
{
SHT15_SCK=1;
if (SHT15_SDA)
val=(val | i);
SHT15_SCK=0;
}
SHT15_SDA=!ack;
SHT15_SCK=1;
_nop_(); // nó báo lỗi ở đây ạ.
_nop_();
_nop_();
SHT15_SCK=0;
SHT15_SDA=1;
return val;
}
void SHT15_Trans_Start(void)
{
SHT15_SDA=1;
SHT15_SCK=0;
_nop_();
SHT15_SCK=1;
_nop_();
SHT15_SDA=0;
_nop_();
SHT15_SCK=0;
_nop_();
_nop_();
_nop_();
SHT15_SCK=1;
_nop_();
SHT15_SDA=1;
_nop_();
SHT15_SCK=0;
}
void SHT15_Connection_Reset(void)
{
unsigned char i;
SHT15_SDA=1;
SHT15_SCK=0;
for(i=0;i<9;i++)
{
SHT15_SCK=1;
SHT15_SCK=0;
}
SHT15_Trans_Start();
}
unsigned char s_softreset(void)
{
unsigned char error=0;
SHT15_Connection_Reset(); //ͨÐÅÁ¬½ÓÇ¿ÖƸ´Î»
error+=SHT15_Write_Byte(RESET); //·¢ËÍÈí¸´Î»ÃüÁî
return error; //error=1 ±íʾ´«¸ÐÆ÷ûÓÐÏìÓ¦£¨Ïß·¹ÊÕÏ£©
}
//unsigned char SHT15_Read_Status_Reg(unsigned char *p_value, unsigned char *p_checksum)
//{
// unsigned char error=0;
// SHT15_Trans_Start();
// error=SHT15_Write_Byte(STATUS_REG_R);
// *p_value=SHT15_Read_Byte(ACK);
// *p_checksum=SHT15_Read_Byte(noACK);
// return error; //error=1 ±íʾ´«¸ÐÆ÷ûÓÐÏìÓ¦£¨Ïß·¹ÊÕÏ£©
//}
//unsigned char SHT15_Write_Status_Reg(unsigned char *p_value)
//{
// unsigned char error=0;
// SHT15_Trans_Start();
// error+=SHT15_Write_Byte(STATUS_REG_W);
// error+=SHT15_Write_Byte(*p_value);
// return error; //error=1 ±íʾ´«¸ÐÆ÷ûÓÐÏìÓ¦£¨Ïß·¹ÊÕÏ£©
//}
unsigned char SHT15_Measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
unsigned error=0;
unsigned int i;
SHT15_Trans_Start();
switch(mode)
{
case TEMP : error+=SHT15_Write_Byte(MEASURE_TEMP);
break;
case HUMI : error+=SHT15_Write_Byte(MEASURE_HUMI);
break;
default : break;
}
for (i=0;i<65535;i++)//µÈ´ý²âÁ¿½áÊø£¬²¢¼ÓÈ볬ʱ´íÎó¼ì²â£¨·ÀËÀ£©
{
if(SHT15_SDA == 0)
{
break;
}
}
if(SHT15_SDA)
{
error+=1;
}
*(p_value) = SHT15_Read_Byte(ACK);
*(p_value+1) = SHT15_Read_Byte(ACK);
*p_checksum = SHT15_Read_Byte(noACK);
return error;
}
/*
void Init_Uart()
{
SCON = 0x40; //´®Ðз½Ê½1£¬²»ÔÊÐí´®ÐнÓÊÕ
TMOD = 0x20; //¶¨Ê±Æ÷T1,·½Ê½2
//**** ¸ù¾Ýµ¥Æ¬»úʱÖÓģʽÐÞ¸ÄÏÂÃæµÄ´úÂë ****
// TH1 = 0xFA; //6ʱÖÓģʽ
TH1 = 0xFD; //12ʱÖÓģʽ
//******************************************
TR1 = 1; //Æô¶¯¶¨Ê±Æ÷
TI = 1; //·¢Ë͵ÚÒ»¸ö×Ö·û
}
*/
void SHT15_Calculate(float *p_humidity ,float *p_temperature)
{
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 SHT15_Calculate_Dew_Point(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;
}
void SHT75_Demo(void)
{
static idata unsigned char count=0;
static idata value humi_val[_SmoothNum],temp_val[_SmoothNum];
float humi,temp;
float dew_point;
unsigned char error,checksum;
unsigned int i;
// Init_Uart();
s_softreset();
T0_Delay_ms(10);
SHT15_Connection_Reset();
// EN_75LBC184 = 1;
// LED_RXD = 1; /
error=0;
count ++;
if(count>=_SmoothNum)
count=0;
error+=SHT15_Measure((unsigned char*) &temp_val[count].i,&checksum,TEMP);
error+=SHT15_Measure((unsigned char*) &humi_val[count].i,&checksum,HUMI);
if(error!=0)
SHT15_Connection_Reset();
else
{
humi_val[count].f=(float)humi_val[count].i;
temp_val[count].f=(float)temp_val[count].i;
for(i=0;i<_SmoothNum;i++)
{
humi += humi_val[i].f;
temp += temp_val[i].f;
}
humi /= (float)_SmoothNum;
temp /= (float)_SmoothNum;
SHT15_Calculate(&humi,&temp);
dew_point=SHT15_Calculate_Dew_Point(humi,temp);
// LED_TXD=0;
printf("temp:%5.2fC humi:%5.2f%% dew point:%5.2fC\r\n",
temp,humi,dew_point);
// LED_TXD=1;
}
}
#include <REGX52.H>
#include <string.h>
#include <stdio.h>
sbit KEY_0 = P1^0;
sbit KEY_1 = P1^1;
sbit KEY_2 = P1^2;
sbit KEY_3 = P1^3;
sbit KEY_4 = P1^4;
sbit KEY_5 = P1^5;
sbit KEY_6 = P1^6;
sbit KEY_7 = P1^7;
sbit EN_75LBC184 = P3^5;
// */ lúc đầu báo lỗi ở đoạn này. sau khi xóa đi thì lại báo lỗi ở dòng 48.
typedef struct
{
unsigned int i;
float f;
} value;
enum {TEMP,HUMI};
sbit SHT15_SDA = P2^0; //SHT15_SHT75
sbit SHT15_SCK = P2^1; //SHT15_SHT75
#define noACK 0
#define ACK 1
#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
unsigned char SHT15_Write_Byte(unsigned char value)
{
unsigned char i,error=0;
for (i=0x80;i>0;i/=2)
{
if (i & value)
SHT15_SDA=1;
else
SHT15_SDA=0;
SHT15_SCK=1;
_nop_();
_nop_();
_nop_();
SHT15_SCK=0;
}
SHT15_SDA=1;
SHT15_SCK=1;
error=SHT15_SDA;
SHT15_SCK=0;
return error;
}
unsigned char SHT15_Read_Byte(unsigned char ack)
{
unsigned char i,val=0;
SHT15_SDA=1;
for (i=0x80;i>0;i/=2)
{
SHT15_SCK=1;
if (SHT15_SDA)
val=(val | i);
SHT15_SCK=0;
}
SHT15_SDA=!ack;
SHT15_SCK=1;
_nop_(); // nó báo lỗi ở đây ạ.
_nop_();
_nop_();
SHT15_SCK=0;
SHT15_SDA=1;
return val;
}
void SHT15_Trans_Start(void)
{
SHT15_SDA=1;
SHT15_SCK=0;
_nop_();
SHT15_SCK=1;
_nop_();
SHT15_SDA=0;
_nop_();
SHT15_SCK=0;
_nop_();
_nop_();
_nop_();
SHT15_SCK=1;
_nop_();
SHT15_SDA=1;
_nop_();
SHT15_SCK=0;
}
void SHT15_Connection_Reset(void)
{
unsigned char i;
SHT15_SDA=1;
SHT15_SCK=0;
for(i=0;i<9;i++)
{
SHT15_SCK=1;
SHT15_SCK=0;
}
SHT15_Trans_Start();
}
unsigned char s_softreset(void)
{
unsigned char error=0;
SHT15_Connection_Reset(); //ͨÐÅÁ¬½ÓÇ¿ÖƸ´Î»
error+=SHT15_Write_Byte(RESET); //·¢ËÍÈí¸´Î»ÃüÁî
return error; //error=1 ±íʾ´«¸ÐÆ÷ûÓÐÏìÓ¦£¨Ïß·¹ÊÕÏ£©
}
//unsigned char SHT15_Read_Status_Reg(unsigned char *p_value, unsigned char *p_checksum)
//{
// unsigned char error=0;
// SHT15_Trans_Start();
// error=SHT15_Write_Byte(STATUS_REG_R);
// *p_value=SHT15_Read_Byte(ACK);
// *p_checksum=SHT15_Read_Byte(noACK);
// return error; //error=1 ±íʾ´«¸ÐÆ÷ûÓÐÏìÓ¦£¨Ïß·¹ÊÕÏ£©
//}
//unsigned char SHT15_Write_Status_Reg(unsigned char *p_value)
//{
// unsigned char error=0;
// SHT15_Trans_Start();
// error+=SHT15_Write_Byte(STATUS_REG_W);
// error+=SHT15_Write_Byte(*p_value);
// return error; //error=1 ±íʾ´«¸ÐÆ÷ûÓÐÏìÓ¦£¨Ïß·¹ÊÕÏ£©
//}
unsigned char SHT15_Measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
unsigned error=0;
unsigned int i;
SHT15_Trans_Start();
switch(mode)
{
case TEMP : error+=SHT15_Write_Byte(MEASURE_TEMP);
break;
case HUMI : error+=SHT15_Write_Byte(MEASURE_HUMI);
break;
default : break;
}
for (i=0;i<65535;i++)//µÈ´ý²âÁ¿½áÊø£¬²¢¼ÓÈ볬ʱ´íÎó¼ì²â£¨·ÀËÀ£©
{
if(SHT15_SDA == 0)
{
break;
}
}
if(SHT15_SDA)
{
error+=1;
}
*(p_value) = SHT15_Read_Byte(ACK);
*(p_value+1) = SHT15_Read_Byte(ACK);
*p_checksum = SHT15_Read_Byte(noACK);
return error;
}
/*
void Init_Uart()
{
SCON = 0x40; //´®Ðз½Ê½1£¬²»ÔÊÐí´®ÐнÓÊÕ
TMOD = 0x20; //¶¨Ê±Æ÷T1,·½Ê½2
//**** ¸ù¾Ýµ¥Æ¬»úʱÖÓģʽÐÞ¸ÄÏÂÃæµÄ´úÂë ****
// TH1 = 0xFA; //6ʱÖÓģʽ
TH1 = 0xFD; //12ʱÖÓģʽ
//******************************************
TR1 = 1; //Æô¶¯¶¨Ê±Æ÷
TI = 1; //·¢Ë͵ÚÒ»¸ö×Ö·û
}
*/
void SHT15_Calculate(float *p_humidity ,float *p_temperature)
{
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 SHT15_Calculate_Dew_Point(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;
}
void SHT75_Demo(void)
{
static idata unsigned char count=0;
static idata value humi_val[_SmoothNum],temp_val[_SmoothNum];
float humi,temp;
float dew_point;
unsigned char error,checksum;
unsigned int i;
// Init_Uart();
s_softreset();
T0_Delay_ms(10);
SHT15_Connection_Reset();
// EN_75LBC184 = 1;
// LED_RXD = 1; /
error=0;
count ++;
if(count>=_SmoothNum)
count=0;
error+=SHT15_Measure((unsigned char*) &temp_val[count].i,&checksum,TEMP);
error+=SHT15_Measure((unsigned char*) &humi_val[count].i,&checksum,HUMI);
if(error!=0)
SHT15_Connection_Reset();
else
{
humi_val[count].f=(float)humi_val[count].i;
temp_val[count].f=(float)temp_val[count].i;
for(i=0;i<_SmoothNum;i++)
{
humi += humi_val[i].f;
temp += temp_val[i].f;
}
humi /= (float)_SmoothNum;
temp /= (float)_SmoothNum;
SHT15_Calculate(&humi,&temp);
dew_point=SHT15_Calculate_Dew_Point(humi,temp);
// LED_TXD=0;
printf("temp:%5.2fC humi:%5.2f%% dew point:%5.2fC\r\n",
temp,humi,dew_point);
// LED_TXD=1;
}
}