วันจันทร์ที่ 20 สิงหาคม พ.ศ. 2555

Interface DHT21 with PIC


Code: Select all
/***********************************************************
 * Description:
     AM2301 Humility & Tempreture seosor.
 * Test configuration:
     MCU:             PIC16F688
     dev.board:       F688 LCD board
     Oscillator:      internal RC , 4.0000 MHz
     Ext. Modules:    Character LCD 2x16
     SW:              mikroC PRO for PIC 4.15

 * NOTES:
 
************************************************************/

typedef unsigned char  U8;  /* defined for unsigned 8-bits integer variable */

//----------------Definition zone---------------//
unsigned char  U8FLAG,U8comdata;
unsigned char  U8checkdata,U8checkdata_temp,U8temp;
unsigned char  U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L;
unsigned char  U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp;
unsigned int   U16RH_data,U16T_data;
//-----------------------------------------------//


//---------------Defination for sensor IO interface--------//
sbit iobit at RA5_bit;
sbit iobit_Direction at TRISA5_bit;
//-----------------------------------------------//


void rd8bit(void){
    U8 i;
    for(i=0;i<8;i++){
        U8FLAG=2;
        while((!iobit) && U8FLAG++);
        Delay_10us();
        Delay_10us();
        Delay_10us();
        U8temp=0;
        if(iobit) U8temp=1;     // bit rd is 1, set U8temp=1
        U8FLAG=2;
        while((iobit) && U8FLAG++);
        if(U8FLAG==1)break;
        U8comdata<<=1;
        U8comdata|=U8temp;
    }
}

void rdbus(void){
iobit_Direction=0;
iobit=0;
Delay_us(500);
iobit=1;
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
iobit=1;
iobit_Direction=1;            // TRISA5_bit = 1;  //set RA5 pin as input
    if(!iobit){
        U8FLAG=2;
        while((!iobit)&&U8FLAG++);
        U8FLAG=2;
        while((iobit)&&U8FLAG++);
        rd8bit();
        U8RH_data_H_temp=U8comdata;
        rd8bit();
        U8RH_data_L_temp=U8comdata;
        rd8bit();
        U8T_data_H_temp=U8comdata;
        rd8bit();
        U8T_data_L_temp=U8comdata;
        rd8bit();
        U8checkdata_temp=U8comdata;
        iobit=1;
        U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
          if(U8temp==U8checkdata_temp){
             U8RH_data_H=U8RH_data_H_temp;
             U8RH_data_L=U8RH_data_L_temp;
             U8T_data_H=U8T_data_H_temp;
             U8T_data_L=U8T_data_L_temp;
             U8checkdata=U8checkdata_temp;

             U16RH_data<<=8;
             U16RH_data|=U8RH_data_H;
             U16RH_data<<=8;
             U16RH_data|=U8RH_data_L;
             U16T_data<<=8;
             U16T_data|=U8T_data_H;
             U16T_data<<=8;
             U16T_data|=U8T_data_L;
          }//CRC checked

    }//read data-bus.
}//rdbus-end

2 ความคิดเห็น:

  1. Version for CCS C Compiler:

    /***********************************************************
    * Descrição:
    AM2301 Sensor de Humidade e Temperatura.
    * Configuration de teste:
    MCU: PIC16F873A
    Oscilador: XT, 4.0000 MHz
    SoftWare: CCS C Compiler 5.012

    * NOTAS:
    Baseado na versão de Siwakorn Longsomboon feito em mikroC PRO for PIC 4.15
    http://siwakorn-lsb.blogspot.com.br/2012/08/interface-dht21-with-pic.html
    ************************************************************/

    typedef unsigned char U8; /* defined for unsigned 8-bits integer variable */

    //----------------Definition zone---------------//
    unsigned char U8FLAG,U8comdata;
    unsigned char U8checkdata,U8checkdata_temp,U8temp;
    unsigned char U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L;
    unsigned char U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp;
    unsigned int16 U16RH_data,U16T_data;
    //-----------------------------------------------//


    //---------------Defination for sensor IO interface--------//
    #define DHT PIN_C6

    //-----------------------------------------------//

    void rd8bit(void){
    U8 i;
    for(i=0;i<8;i++){
    U8FLAG=2;
    while(!input(DHT) && U8FLAG++);
    Delay_us(30);
    U8temp=0;
    if(input(DHT)) U8temp=1; // bit rd is 1, set U8temp=1
    U8FLAG=2;
    while(input(DHT) && U8FLAG++);
    if(U8FLAG==1)break;
    U8comdata<<=1;
    U8comdata|=U8temp;
    }
    }

    void rdbus(void){
    Delay_ms(10);
    output_low(DHT);
    Delay_ms(1);
    output_high(DHT);
    Delay_us(40);
    if(!input(DHT)){
    U8FLAG=2;
    while(!input(DHT)&&U8FLAG++);
    U8FLAG=2;
    while(input(DHT)&&U8FLAG++);
    rd8bit();
    U8RH_data_H_temp=U8comdata;
    rd8bit();
    U8RH_data_L_temp=U8comdata;
    rd8bit();
    U8T_data_H_temp=U8comdata;
    rd8bit();
    U8T_data_L_temp=U8comdata;
    rd8bit();
    U8checkdata_temp=U8comdata;
    U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
    if(U8temp==U8checkdata_temp){
    U8RH_data_H=U8RH_data_H_temp;
    U8RH_data_L=U8RH_data_L_temp;
    U8T_data_H=U8T_data_H_temp;
    U8T_data_L=U8T_data_L_temp;
    U8checkdata=U8checkdata_temp;
    U16RH_data<<=16;
    U16RH_data|=U8RH_data_H;
    U16RH_data<<=8;
    U16RH_data|=U8RH_data_L;
    U16T_data<<=8;
    U16T_data|=U8T_data_H;
    U16T_data<<=8;
    U16T_data|=U8T_data_L;
    }//CRC checked

    }//read data-bus.

    }//rdbus-end

    ตอบลบ
  2. Hello Sir
    I saw your blog about on DHT21 and the ı examined and trid your code in CCS PIC but ıt didnt work ? Can you help me how to fix it ?

    ตอบลบ