اطلاعیه

Collapse
No announcement yet.

کار نکردن یه برنامه درس در avr studio

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    کار نکردن یه برنامه درس در avr studio

    سلام :rolleyes:
    من یه برنامه واسه سنسور sht11 نوشتم تو کدویژن درسه ولی با avr studio مشکل داره محاباتش رو درس انجام نمیده
    Avr studio با متغیرهای float مشکل داره؟
    فک میکنم مشکلش تو این مورد باشه :read:

    #2
    پاسخ : کار نکردن یه برنامه درس در avr studio

    نه مگر اینکه خوئتون یه جایی اشتباه کرده باشین...
    فقط SHT تا دلت بخواد چیز مسخره ایه :cry2:
    چرا ناراحتی پاتریک!
    + امروز ی بچه دیدم سرچهارراه گل میفروخت
    - از دیدنش ناراحت شدی؟
    + نه
    پس چی ناراحتت کرده
    + اینکه دیدن اینجور بچه ها انقدر واسم عادی شده که دیگه ناراحتم نمیکنه
    ------------------------------------------------------------------------------------------------------
    تاحالا فکر کردی بیسواد کیه؟- بی سواد یعنی کسی که نمیتونه ذهنیاتش رو عملی کنه!
    ------------------------------------------------------------------------------------------------------
    محصولات

    دیدگاه


      #3
      پاسخ : کار نکردن یه برنامه درس در avr studio

      سلام مهندس
      با کد ویژن راش انداختم...حالا کلا میخوام با avr studio کار کنم
      دقیقا همون برنامست...
      فقط به جای مثلا porta.3 =1 مینویسم setbit(porta,3) ...یه فرقای جزیی بین دوتا کامپایلر....مگه میشه اخه خیلی عجیبه ...
      یه چیز دیگه این ای وی ار استودیو ftoa نداره :mrgreen:
      Sprintf هم با float مشکل داره...حالا من با float کار ندارم قسمت int دما و رطوبت و میگیرم ولی نمیشه
      برنامه های دیگه درس کار میکنه نمیدونم چرا نتونسم sht باش درس کنم...
      پروژه دانشگامه تمومش کنم بره...
      نباید یه نکته خاصی رو انجام بدم ؟
      چون برنامش چیز خاصی نداره
      مرسی

      دیدگاه


        #4
        پاسخ : کار نکردن یه برنامه درس در avr studio

        کد برنامه و میزارم لطفا یکی بررسی کنه :cry:
        Header
        کد:
        /*
         * Shtxx.h
         *
         * Created: 6/18/2014 2:47:58 AM
         * Author: K1
         */ 
        
        
        #ifndef SHTXX_H_
        #define SHTXX_H_
        
        
        #define SHT_DATA_PIN    PINA
        #define SHT_DATA_BIT    3
        #define SHT_DATA_DDR    DDRA //
        #define SHT_DATA_PORT    PORTA
        
        #define SHT_DATA_IN      (SHT_DATA_PIN & (1<<SHT_DATA_BIT))
        #define SHT_DATA_OUT(x)   (x ? (SHT_DATA_DDR |= (1<<SHT_DATA_BIT)) : (SHT_DATA_DDR &= ~(1<<SHT_DATA_BIT))  )
        #define SHT_DATA(x)      (x ? (SHT_DATA_PORT |= (1<<SHT_DATA_BIT)) : (SHT_DATA_PORT &= ~(1<<SHT_DATA_BIT)) ) 
        
        #define SHT_SCK_BIT      4
        #define SHT_SCK_DDR      DDRA
        #define SHT_SCK_PORT    PORTA // 
        #define SHT_SCK(x)      (x ? (SHT_SCK_PORT |= (1<<SHT_SCK_BIT)) : (SHT_SCK_PORT &= ~(1<<SHT_SCK_BIT)) ) 
        #define SHT_SCK_OUT(x)   (x ? (SHT_SCK_DDR |= (1<<SHT_SCK_BIT)) : (SHT_SCK_DDR &= ~(1<<SHT_SCK_BIT)) ) 
        
        typedef union
        { unsigned int i; float f;} value;
        
        
        enum {TEMP,HUMI};
        
        #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
        
        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
        
        //SHT Functions
        char SHT_WriteByte(unsigned char valuep);
        char SHT_ReadByte(unsigned char ack);
        void s_transstart(void);
        void s_connectionreset(void);
        char s_softreset(void);
        char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);
        void calc_sth11(float *p_humidity ,float *p_temperature);
        float calc_dewpoint(float h,float t);
        
        unsigned char key=16,uch_TempTime_Number=0;
        int intTemp1=0,intTime1=0,intTemp2=0,intTime2=0,intTemp3=0,intTime3=0;
        int intTemp4=0,intTime4=0,intTemp5=0,intTime5=0,intTemp6=0,intTime6=0;
        
        float sht11_temp(void);
        float sht11_humy(void);
        void Sht11_Init(void);
        
        #endif /* SHTXX_H_ */
        فایل .c
        کد:
        
        
        /*
         * Shtxx.c
         *
         * Created: 6/18/2014 2:49:58 AM
         * Author: K1
         */ 
        #include "Shtxx.h"
        
        float sht11_temp()
        {
        value humi_val, temp_val;
        unsigned char error, checksum, status;
        float dew_point;
        error=0;
        error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);
        error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);
        if(error!=0) s_connectionreset();
        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
        }  
        
        
        //temp_val.f+=10;
        return  temp_val.f ;
        }  
        
        float sht11_humy()
        { 
        value humi_val, temp_val;
        unsigned char error, checksum, status;
        float dew_point;
        error=0;
        error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);
        error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);
        if(error!=0) s_connectionreset();
        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
        }  
        return   humi_val.f;
        }
        
        
        
        void Sht11_Init()
        {
        // Setup Sensibus Pins
        SHT_SCK(0); // ClockLow
        SHT_SCK_DDR(1); // SCK is an output
        SHT_DATAO(0); // Always Zero
        s_softreset();
        
        }
        
        
        //----------------------------------------------------------------------------------
        // tulis byte ke SHT
        //----------------------------------------------------------------------------------
        char SHT_WriteByte(unsigned char valuep)
        {
        
        // Declare your global variables here
        unsigned char i,error=0;
        for (i=0x80;i>0;i/=2)
        {
        if (i & valuep) SHT_DATA_DDR(0);
        else SHT_DATA_DDR(1);
        
        SHT_SCK(1);
        _delay_us(5); //pulswith approx. 5 us
        SHT_SCK(0);
        
        }
        SHT_DATA_DDR(0); //release DATA-line
        SHT_SCK(1); //clk #9 for ack
        error = SHT_DATA ? 1 : 0 ;//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_DDR(0); //release DATA-line
        for (i=0x80;i>0;i/=2)
        {
        SHT_SCK(1);
        if (SHT_DATA) val=(val | i); //read bit
        SHT_SCK(0);
        }
        SHT_DATA_DDR(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_DDR(0); //release DATA-line
        return val;
        }
        
        //----------------------------------------------------------------------------------
        // kondisi start
        // _____ ________
        // DATA: |_______|
        // ___ ___
        // SCK : ___| |___| |______
        //----------------------------------------------------------------------------------
        void s_transstart(void)
        {
        SHT_DATA_DDR(0);
        SHT_SCK(0); //Initial state
        _delay_us(1);
        SHT_SCK(1);
        _delay_us(1);
        SHT_DATA_DDR(1);
        _delay_us(1);
        SHT_SCK(0);
        _delay_us(5);
        SHT_SCK(1);
        _delay_us(1);
        SHT_DATA_DDR(0);
        _delay_us(1);
        SHT_SCK(0);
        }
        
        //----------------------------------------------------------------------------------
        // reset koneksi, 9 clock diikuti dengan kondisi start
        // _____________________________________________________ ________
        // DATA: |_______|
        // _ _ _ _ _ _ _ _ _ ___ ___
        // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
        //----------------------------------------------------------------------------------
        
        void s_connectionreset(void)
        {
        unsigned char i;
        SHT_DATA_DDR(0); SHT_SCK(0); //Initial state
        for(i=0;i<9;i++) //9 SCK cycles
        {
        SHT_SCK(1);
        _delay_us(1);
        SHT_SCK(0);
        }
        s_transstart(); //transmission start
        }
        
        //----------------------------------------------------------------------------------
        // reset SHT-nya !!
        //----------------------------------------------------------------------------------
        
        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
        }
        
        //----------------------------------------------------------------------------------
        // pengukuran data
        //----------------------------------------------------------------------------------
        char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
        {
        unsigned error=0;
        long int i;
        
        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<131070;i++) if(!SHT_DATA) break; //wait until sensor has finished the measurement
        if(SHT_DATA) 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;
        }
        
        //----------------------------------------------------------------------------------------
        // hitung temperature [°C] and humidity [%RH]
        // input : humi [Ticks] (12 bit)
        // temp [Ticks] (14 bit)
        // output: humi [%RH]
        // temp [°C]
        //----------------------------------------------------------------------------------------
        
        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 [°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;
        }
        مرسی

        دیدگاه


          #5
          پاسخ : کار نکردن برنامه درست سنسور sht11(کدویژن) در avr studio

          برنامه کدویژن هم اینه که به راحتی کار میکنه :
          کد:
          
          
          #include "app_config.h"
          
          
          #define SHT_DATA_PIN    PINA
          #define SHT_DATA_BIT    3
          #define SHT_DATA_DDR    DDRA //
          #define SHT_DATA_PORT    PORTA
          
          #define SHT_DATA_IN      (SHT_DATA_PIN & (1<<SHT_DATA_BIT))
          #define SHT_DATA_OUT(x)   (x ? (SHT_DATA_DDR |= (1<<SHT_DATA_BIT)) : (SHT_DATA_DDR &= ~(1<<SHT_DATA_BIT))  )
          #define SHT_DATA(x)      (x ? (SHT_DATA_PORT |= (1<<SHT_DATA_BIT)) : (SHT_DATA_PORT &= ~(1<<SHT_DATA_BIT)) ) 
          
          #define SHT_SCK_BIT      4
          #define SHT_SCK_DDR      DDRA
          #define SHT_SCK_PORT    PORTA // 
          #define SHT_SCK(x)      (x ? (SHT_SCK_PORT |= (1<<SHT_SCK_BIT)) : (SHT_SCK_PORT &= ~(1<<SHT_SCK_BIT)) ) 
          #define SHT_SCK_OUT(x)   (x ? (SHT_SCK_DDR |= (1<<SHT_SCK_BIT)) : (SHT_SCK_DDR &= ~(1<<SHT_SCK_BIT)) ) 
          
          typedef union
          { unsigned int i; float f;} value;
          
          
          enum {TEMP,HUMI};
          
          #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
          
          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
          
          //SHT Functions
          char SHT_WriteByte(unsigned char valuep);
          char SHT_ReadByte(unsigned char ack);
          void s_transstart(void);
          void s_connectionreset(void);
          char s_softreset(void);
          char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);
          void calc_sth11(float *p_humidity ,float *p_temperature);
          float calc_dewpoint(float h,float t);
          
          
          float sht11_temp(void);
          float sht11_humy(void);
          void Sht11_Init(void);
          
          float sht11_temp()
          {
          value humi_val, temp_val;
          unsigned char error, checksum, status;
          float dew_point;
          error=0;
          error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);
          error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);
          if(error!=0) s_connectionreset();
          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
          }  
          
          
          //temp_val.f+=10;
          return  temp_val.f ;
          }  
          
          float sht11_humy()
          { 
          value humi_val, temp_val;
          unsigned char error, checksum, status;
          float dew_point;
          error=0;
          error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);
          error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);
          if(error!=0) s_connectionreset();
          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
          }  
          
          
          return   humi_val.f;
          }
          
          
          
          void Sht11_Init()
          {
          // Setup Sensibus Pins
          SHT_SCK(0); // ClockLow
          SHT_SCK_OUT(1); // SCK is an output
          SHT_DATAO(0); // Always Zero
          s_softreset();
          
          }
          
          
          //----------------------------------------------------------------------------------
          // tulis byte ke SHT
          //----------------------------------------------------------------------------------
          char SHT_WriteByte(unsigned char valuep)
          {
          
          // Declare your global variables here
          unsigned char i,error=0;
          for (i=0x80;i>0;i/=2)
          {
          if (i & valuep) 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;
          }
          
          //----------------------------------------------------------------------------------
          // kondisi start
          // _____ ________
          // DATA: |_______|
          // ___ ___
          // SCK : ___| |___| |______
          //----------------------------------------------------------------------------------
          void s_transstart(void)
          {
          SHT_DATA_OUT(0);
          SHT_SCK(0); //Initial state
          _delay_us(1);
          SHT_SCK(1);
          _delay_us(1);
          SHT_DATA_OUT(1);
          _delay_us(1);
          SHT_SCK(0);
          _delay_us(5);
          SHT_SCK(1);
          _delay_us(1);
          SHT_DATA_OUT(0);
          _delay_us(1);
          SHT_SCK(0);
          }
          
          //----------------------------------------------------------------------------------
          // reset koneksi, 9 clock diikuti dengan kondisi start
          // _____________________________________________________ ________
          // DATA: |_______|
          // _ _ _ _ _ _ _ _ _ ___ ___
          // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
          //----------------------------------------------------------------------------------
          
          void s_connectionreset(void)
          {
          unsigned char i;
          SHT_DATA_OUT(0); SHT_SCK(0); //Initial state
          for(i=0;i<9;i++) //9 SCK cycles
          {
          SHT_SCK(1);
          _delay_us(1);
          SHT_SCK(0);
          }
          s_transstart(); //transmission start
          }
          
          //----------------------------------------------------------------------------------
          // reset SHT-nya !!
          //----------------------------------------------------------------------------------
          
          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
          }
          
          //----------------------------------------------------------------------------------
          // pengukuran data
          //----------------------------------------------------------------------------------
          char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
          {
          unsigned error=0;
          long int i;
          
          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<131070;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;
          }
          
          //----------------------------------------------------------------------------------------
          // hitung temperature [°C] and humidity [%RH]
          // input : humi [Ticks] (12 bit)
          // temp [Ticks] (14 bit)
          // output: humi [%RH]
          // temp [°C]
          //----------------------------------------------------------------------------------------
          
          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 [°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;
          }
          مقدار خروجی همش یه عدد مثل 2576 هست...
          ولی با کدویژن درسته...چرا ؟! برنامه که یکیه کدش ولی تو یه کامپایلر دیگه درست کار نمیکنه ؟
          یکی زاه نمایی میکنه ؟

          دیدگاه


            #6
            پاسخ : کار نکردن یه برنامه درس در avr studio

            آقایون درس شد !!!!!!!!!!!

            فقط من دوتا فایل داشتم یکی هدر بود یکی فایل c ...
            فایل هدر و فایل c رو یکی کردم ... یعنی همه تابع هامو آوردم تو فایل .h حالا درس کار میکنه....
            مشکل کار من چی بوده ؟

            ابنم کد برنامه sht11 با avr studio :

            کد:
            /*
             * Shtxx.h
             *
             * Created: 6/18/2014 2:47:58 AM
             * Author: K1
             */ 
            
            
            #ifndef SHTXX_H_
            #define SHTXX_H_
            
            
            #include "app_config.h"
            
            
            //#define SHT_DATA_OUT DDRA.3
            //#define SHT_DATA_IN PINA.3
            //#define SHT_SCK PORTA.4
            #define SHT_DATA_PIN		PINA
            #define SHT_DATA_BIT		3
            #define SHT_DATA_DDR		DDRA //
            #define SHT_DATA_PORT		PORTA
            
            #define SHT_DATA_IN			(SHT_DATA_PIN & (1<<SHT_DATA_BIT))
            #define SHT_DATA_OUT(x)   (x ? (SHT_DATA_DDR |= (1<<SHT_DATA_BIT)) : (SHT_DATA_DDR &= ~(1<<SHT_DATA_BIT))  )
            #define SHT_DATA(x)			(x ? (SHT_DATA_PORT |= (1<<SHT_DATA_BIT)) : (SHT_DATA_PORT &= ~(1<<SHT_DATA_BIT)) ) 
            
            #define SHT_SCK_BIT			4
            #define SHT_SCK_DDR			DDRA
            #define SHT_SCK_PORT		PORTA // 
            #define SHT_SCK(x)			(x ? (SHT_SCK_PORT |= (1<<SHT_SCK_BIT)) : (SHT_SCK_PORT &= ~(1<<SHT_SCK_BIT)) ) 
            #define SHT_SCK_OUT(x)   (x ? (SHT_SCK_DDR |= (1<<SHT_SCK_BIT)) : (SHT_SCK_DDR &= ~(1<<SHT_SCK_BIT)) ) 
            
            typedef struct
            { unsigned int i; float f;} value;
            
            
            enum {TEMP,HUMI};
            
            #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
            
            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
            
            //SHT Functions
            char SHT_WriteByte(unsigned char valuep);
            char SHT_ReadByte(unsigned char ack);
            void s_transstart(void);
            void s_connectionreset(void);
            char s_softreset(void);
            char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);
            void calc_sth11(float *p_humidity ,float *p_temperature);
            float calc_dewpoint(float h,float t);
            
            
            float sht11_temp(void);
            float sht11_humy(void);
            void Sht11_Init(void);
            float sht11_temp()
            {
            value humi_val, temp_val;
            unsigned char error, checksum, status;
            float dew_point;
            error=0;
            error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);
            error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);
            if(error!=0) s_connectionreset();
            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
            }  
            
            
            //temp_val.f+=10;
            return  temp_val.f ;
            }  
            
            float sht11_humy()
            { 
            value humi_val, temp_val;
            unsigned char error, checksum, status;
            float dew_point;
            error=0;
            error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);
            error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);
            if(error!=0) s_connectionreset();
            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
            }  
            
            
            return   humi_val.f;
            }
            
            
            
            void Sht11_Init()
            {
            // Setup Sensibus Pins
            SHT_SCK(0); // ClockLow
            SHT_SCK_OUT(1); // SCK is an output
            SHT_DATA(0); // Always Zero
            s_softreset();
            
            }
            
            
            //----------------------------------------------------------------------------------
            // tulis byte ke SHT
            //----------------------------------------------------------------------------------
            char SHT_WriteByte(unsigned char valuep)
            {
            
            // Declare your global variables here
            unsigned char i,error=0;
            for (i=0x80;i>0;i/=2)
            {
            if (i & valuep) 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;
            }
            
            //----------------------------------------------------------------------------------
            // kondisi start
            // _____ ________
            // DATA: |_______|
            // ___ ___
            // SCK : ___| |___| |______
            //----------------------------------------------------------------------------------
            void s_transstart(void)
            {
            SHT_DATA_OUT(0);
            SHT_SCK(0); //Initial state
            _delay_us(1);
            SHT_SCK(1);
            _delay_us(1);
            SHT_DATA_OUT(1);
            _delay_us(1);
            SHT_SCK(0);
            _delay_us(5);
            SHT_SCK(1);
            _delay_us(1);
            SHT_DATA_OUT(0);
            _delay_us(1);
            SHT_SCK(0);
            }
            
            //----------------------------------------------------------------------------------
            // reset koneksi, 9 clock diikuti dengan kondisi start
            // _____________________________________________________ ________
            // DATA: |_______|
            // _ _ _ _ _ _ _ _ _ ___ ___
            // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
            //----------------------------------------------------------------------------------
            
            void s_connectionreset(void)
            {
            unsigned char i;
            SHT_DATA_OUT(0); SHT_SCK(0); //Initial state
            for(i=0;i<9;i++) //9 SCK cycles
            {
            SHT_SCK(1);
            _delay_us(1);
            SHT_SCK(0);
            }
            s_transstart(); //transmission start
            }
            
            //----------------------------------------------------------------------------------
            // reset SHT-nya !!
            //----------------------------------------------------------------------------------
            
            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
            }
            
            //----------------------------------------------------------------------------------
            // pengukuran data
            //----------------------------------------------------------------------------------
            char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
            {
            unsigned error=0;
            long int i;
            
            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<131070;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;
            }
            
            //----------------------------------------------------------------------------------------
            // hitung temperature [°C] and humidity [%RH]
            // input : humi [Ticks] (12 bit)
            // temp [Ticks] (14 bit)
            // output: humi [%RH]
            // temp [°C]
            //----------------------------------------------------------------------------------------
            
            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 [°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;
            }
            
            #endif /* SHTXX_H_ */
            مشکل کار من چیه ؟
            چطور باید یه فیال .h رو به .c وصل کرد ؟
            یکی یه بررسی میکنه ؟
            تو فایل هدر باید فقط و فقط
            #define هارو با امضا توایع بزاریم ؟ متغیر هارو نمیشه گذاشت ؟ تازه من فقط چندتا const داشتم متغییر هم نداشتم...
            عجیبه واسم

            دیدگاه

            لطفا صبر کنید...
            X