Thông báo

Collapse
No announcement yet.

Sửa lỗi giúp mình với

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • Sửa lỗi giúp mình với

    đây là toàn bộ chường trình mình dựa vào để viết cho con sht75 hiển thị nhiệt độ, độ ẩm lên máy tính qua rs232.nhưng chưa xong vì còn thiếu kết nối với cổng Com. ai giúp mình với Click image for larger version

Name:	Untitled.png
Views:	1
Size:	25.5 KB
ID:	1421583


    #include <REGX52.H>
    #include <intrins.h>
    #include <math.h>


    /
    //#include "commond.h"
    //#include "SHT15_SHT75.h"
    //#include "delay.h"
    //#include <math.h>
    //#include <stdio.h>

    //sbit LED_RXD = P2^4; //´®ÐнÓÊÕָʾµÆ
    //sbit LED_TXD = P2^5; //´®Ðз¢ËÍָʾµÆ
    /*
    sbit KEY_0 = P1^0; //²¦Â뿪¹Ø0
    sbit KEY_1 = P1^1; //²¦Â뿪¹Ø1
    sbit KEY_2 = P1^2; //²¦Â뿪¹Ø2
    sbit KEY_3 = P1^3; //²¦Â뿪¹Ø3
    sbit KEY_4 = P1^4; //²¦Â뿪¹Ø4
    sbit KEY_5 = P1^5; //²¦Â뿪¹Ø5
    sbit KEY_6 = P1^6; //²¦Â뿪¹Ø6
    sbit KEY_7 = P1^7; //²¦Â뿪¹Ø7
    sbit EN_75LBC184 = P3^5; //485×ÜÏßÇý¶¯Æ÷SN75LBC184ʹÄÜ¿ØÖÆ
    */
    // unsigned char data1=0;

    typedef struct//union
    {
    unsigned int i;
    float f;
    } value;

    /************************************************** *********************************
    I/O¿Ú£¬³£Á¿£¬ÃüÁî×ֵȶ¨Òå
    ************************************************** *********************************/
    enum {TEMP,HUMI}; //ÓÃÓÚÅжÏÊÇζȻ¹ÊÇʪ¶ÈµÄö¾Ù³£Á¿

    sbit SHT15_SDA = P2^0; //SHT15_SHT75 Êý¾ÝÏß
    sbit SHT15_SCK = P2^1; //SHT15_SHT75 ʱÖÓÏß

    #define noACK 0 //¡°ÎÞÓ¦´ð¡±ÐźÅ
    #define ACK 1 //¡°ÓÐÓ¦´ð¡±ÐźÅ
    //Addr 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

    //ÒÔÏÂΪµ×²ã×Óº¯Êý
    /************************************************** *****************************************
    * º¯ÊýÃû£º SHT15_Write_Byte
    * ¹¦ÄÜÃèÊö£º дÈëÒ»¸ö×Ö½Ú£¬²¢¼ì²é´ÓÉ豸Ӧ´ð£º¡°ÓÐÓ¦´ð¡±·µ»Ø0£»¡ °ÎÞÓ¦´ð¡±·µ»Ø1
    * Èë¿Ú²ÎÊý£º ҪдÈëµÄ×Ö½Ú£¬ucharÐÍ
    * ³ö¿Ú²ÎÊý£º ³É¹¦£¨0£©/ʧ°Ü£¨1£©±êÖ¾
    * ±¸×¢£º
    ************************************************** *****************************************/
    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;
    }

    /************************************************** *****************************************
    * º¯ÊýÃû£º SHT15_Read_Byte
    * ¹¦ÄÜÃèÊö£º ¶Á³öÒ»¸ö×Ö½Ú£¬²¢¸ù¾ÝÊäÈë²ÎÊý¾ö¶¨ÊÇ·ñ·¢ËÍÓ¦´ð룬1£ ­·¢ËÍ¡°ÓÐÓ¦´ð¡±
    * Èë¿Ú²ÎÊý£º Ó¦´ðλ±êÖ¾£¬1£­¶Á³ö1¸ö×Ö½Úºó·¢ËÍ¡°ÓÐÓ¦´ð¡±
    * ³ö¿Ú²ÎÊý£º ³É¹¦£¨0£©/ʧ°Ü£¨1£©±êÖ¾
    * ±¸×¢£º
    ************************************************** *****************************************/
    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_();
    _nop_();
    _nop_();
    SHT15_SCK=0;
    SHT15_SDA=1;
    return val;
    }

    /************************************************** *****************************************
    * º¯ÊýÃû£º SHT15_Trans_Start
    * ¹¦ÄÜÃèÊö£º Æô¶¯×ÜÏß´«Êä
    * Èë¿Ú²ÎÊý£º ÎÞ
    * ³ö¿Ú²ÎÊý£º ÎÞ
    * ±¸×¢£º
    * ʱÐò£º
    _____ ________
    DATA: |_______|
    ___ ___
    SCK : ___| |___| |______
    ************************************************** *****************************************/
    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;
    }

    /************************************************** *****************************************
    * º¯ÊýÃû£º SHT15_Connection_Reset
    * ¹¦ÄÜÃèÊö£º ͨÐÅÁ¬½ÓÇ¿ÖƸ´Î»
    * Èë¿Ú²ÎÊý£º ÎÞ
    * ³ö¿Ú²ÎÊý£º ÎÞ
    * ±¸×¢£º
    * ʱÐò£º
    __________________________________________________ ___ ________
    DATA: |_______|
    _ _ _ _ _ _ _ _ _ ___ ___
    SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
    ************************************************** *****************************************/
    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();
    }

    /************************************************** *****************************************
    * º¯ÊýÃû£º s_softreset
    * ¹¦ÄÜÃèÊö£º ´«¸ÐÆ÷Èí¸´Î»
    * Èë¿Ú²ÎÊý£º ÎÞ
    * ³ö¿Ú²ÎÊý£º ³É¹¦£¨0£©/ʧ°Ü£¨1£©±êÖ¾
    * ±¸×¢£º
    ************************************************** *****************************************/
    unsigned char s_softreset(void)
    {
    unsigned char error=0;
    SHT15_Connection_Reset(); //ͨÐÅÁ¬½ÓÇ¿ÖƸ´Î»
    error+=SHT15_Write_Byte(RESET); //·¢ËÍÈí¸´Î»ÃüÁî
    return error; //error=1 ±íʾ´«¸ÐÆ÷ûÓÐÏìÓ¦£¨Ïß·¹ÊÕÏ£©
    }

    /************************************************** *****************************************
    * º¯ÊýÃû£º SHT15_Read_Status_Reg
    * ¹¦ÄÜÃèÊö£º ¶ÁÈ¡´«¸ÐÆ÷¼Ä´æÆ÷״̬£¬ÒÔ¼°8-bitУÑéºÍ
    * Èë¿Ú²ÎÊý£º ¼Ä´æÆ÷״̬»º³åÇøÊ×Ö·£¬Ð£ÑéºÍ»º³åÇøÊ×Ö·
    * ³ö¿Ú²ÎÊý£º ³É¹¦£¨0£©/ʧ°Ü£¨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 ±íʾ´«¸ÐÆ÷ûÓÐÏìÓ¦£¨Ïß·¹ÊÕÏ£©
    //}

    /************************************************** *****************************************
    * º¯ÊýÃû£º SHT15_Write_Status_Reg
    * ¹¦ÄÜÃèÊö£º дÈë´«¸ÐÆ÷¼Ä´æÆ÷״̬
    * Èë¿Ú²ÎÊý£º ¼Ä´æÆ÷״̬»º³åÇøÊ×Ö·
    * ³ö¿Ú²ÎÊý£º ³É¹¦£¨0£©/ʧ°Ü£¨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 ±íʾ´«¸ÐÆ÷ûÓÐÏìÓ¦£¨Ïß·¹ÊÕÏ£©
    //}

    /************************************************** *****************************************
    * º¯ÊýÃû£º SHT15_Measure
    * ¹¦ÄÜÃèÊö£º ²âÁ¿1´Î£¨¿ÉÑ¡ÔñζÈ/ʪ¶È£©
    * Èë¿Ú²ÎÊý£º ²âÁ¿½á¹û»º³åÇøÊ×Ö·£¬Ð£ÑéºÍ»º³åÇøÊ×Ö·£¬²âÁ¿Ä£Ê½£¨Î ¶È/ʪ¶È£©
    * ³ö¿Ú²ÎÊý£º ³É¹¦£¨0£©/ʧ°Ü£¨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); //¶ÁµÚ1¸ö×Ö½Ú(MSB)
    *(p_value+1) = SHT15_Read_Byte(ACK); //¶ÁµÚ2¸ö×Ö½Ú(LSB)
    *p_checksum = SHT15_Read_Byte(noACK); //¶ÁȡУÑéºÍ
    return error;
    }

    /************************************************** *****************************************
    * º¯ÊýÃû£º Init_Uart
    * ¹¦ÄÜÃèÊö£º ÅäÖô®ÐпÚΪ9600²¨ÌØÂÊ
    * Èë¿Ú²ÎÊý£º ÎÞ
    * ³ö¿Ú²ÎÊý£º ÎÞ
    * ±¸×¢£º ¾§ÕñΪ11.0592MHz£»ÆäÓà¼û´úÂë×¢ÊÍ
    ************************************************** *****************************************/
    /*
    void Init_Uart()
    {
    SCON = 0x40; //´®Ðз½Ê½1£¬²»ÔÊÐí´®ÐнÓÊÕ
    TMOD = 0x20; //¶¨Ê±Æ÷T1,·½Ê½2
    //**** ¸ù¾Ýµ¥Æ¬»úʱÖÓģʽÐÞ¸ÄÏÂÃæµÄ´úÂë ****
    // TH1 = 0xFA; //6ʱÖÓģʽ
    TH1 = 0xFD; //12ʱÖÓģʽ
    //******************************************
    TR1 = 1; //Æô¶¯¶¨Ê±Æ÷
    TI = 1; //·¢Ë͵ÚÒ»¸ö×Ö·û
    }
    */
    /************************************************** *****************************************
    * º¯ÊýÃû£º SHT15_Calculate
    * ¹¦ÄÜÃèÊö£º ת»»Î¶ȺÍʪ¶ÈΪ³£Óøñʽ£¨·Ö±ðΪÉãÊ϶Ⱥͣ¥RH£©
    * Èë¿Ú²ÎÊý£º ʪ¶È»º³åÖ¸Õ루12 bit ADת»»Öµ£©£¬Î¶Ȼº³åÖ¸Õ루14 bit ADת»»Öµ£©
    * ³ö¿Ú²ÎÊý£º ÎÞ
    * ±¸×¢£º
    ************************************************** *****************************************/
    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]
    }

    /************************************************** *****************************************
    * º¯ÊýÃû£º SHT15_Calculate_Dew_Point
    * ¹¦ÄÜÃèÊö£º ¸ù¾ÝζȺÍʪ¶È£¬¼ÆË㶵㣨µ¥Î»ÊÇ£ºÉãÊ϶ȣ©
    * Èë¿Ú²ÎÊý£º ʪ¶È£¨£¥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 main(void)
    {
    /* TMOD=0x20;
    SCON=0x50;
    TH1=0xFD;
    TR1=1;
    TI=1;
    while(1)
    display(data1);
    if(RI)
    {
    c=SBUF;
    RI=0;
    } */

    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; //ÔÊÐí485·¢ËÍ
    // 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;//¹Ø±Õ·¢ËÍָʾµÆ
    }
    }
    Last edited by banhcay12345; 11-06-2014, 10:53.

Về tác giả

Collapse

banhcay12345 Tìm hiểu thêm về banhcay12345

Bài viết mới nhất

Collapse

Đang tải...
X