Autor Tema: libreria para el modulo DHT22 o el AM2302  (Leído 2931 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado tithanae

  • PIC16
  • ***
  • Mensajes: 109
libreria para el modulo DHT22 o el AM2302
« en: 17 de Enero de 2017, 17:51:43 »
Realice una recopilación de algunas paginas que contenían códigos realizados para este modulo.
Lo que hice  fue porque algunos códigos tenían variables globales y otros no me daban una función sencilla para utilizar.

Link de las paginas utilzadas
https://www.ccsinfo.com/forum/viewtopic.php?t=51358&postdays=0&postorder=asc&highlight=dht&start=0
http://playground.arduino.cc/Main/DHTLib
http://ccspicc.blogspot.com/2016/05/pic16f877a-dht22-am2302-rht03-humidity-temperature-sensor-proteus-simulation-ccs-picc.html

Resumiendo solo necesitan utilizar la función mostrada mas abajo, la cual devuelve una variable tipo int entre 1 y 4 lo que nos da a conocer el estado de nuestra conexión con el modulo y carga en dos variables tipo float la Temperatura y la Humedad Relativa.
Código: [Seleccionar]
int get_data_sensor_dht22(float *temp_f, float *rh_f)
Les dejo un código para realizar una prueba básica realizada con un PIC 18F25K20
Código: [Seleccionar]
#include "flex_lcd.c"
#include "dht22.c"

void main()
{
   float temp=0;
   float rh=0;
   
   setup_oscillator(OSC_64MHZ);
   lcd_init();
   
   lcd_putc('\f');
   
   printf(lcd_putc,"INCIO OK");
   delay_ms(2000);
   lcd_putc('\f');
   
   while(TRUE)
   { 
      switch(get_data_sensor_dht22(&temp, &rh))
      {
         case DATA_OK:           
               lcd_gotoxy (1, 1) ;
               printf (lcd_putc, "HR %0.1f", rh);
               printf (lcd_putc, "\nTEMP %0.1f", temp);
               break;
         case CHECK_SUM_ERROR:   
               printf (lcd_putc, "\fCHECK SUM \nERROR");
               delay_ms(1000);
               break;
         case TIME_OUT_ERROR:   
               printf (lcd_putc, "\fTIME OUT \nERROR");
               delay_ms(1000);
               break;
         case UNRESPONSIVE_SENSOR:   
               printf (lcd_putc, "\fUNRESPONSIVE \nERROR");
               delay_ms(1000);
               break;
      }
      delay_ms(1000);
   }
}
Aca la libreria
Código: [Seleccionar]
// Librery for module TH22 OR AM2302

#Byte    TRISB = 0XF93                 // Pin mapped to PORTB
#define  DHT_IO   PIN_B1
#define  UNRESPONSIVE_SENSOR  4
#define  TIME_OUT_ERROR       3
#define  CHECK_SUM_ERROR      2
#define  DATA_OK              1



void start_signal()
{
   Output_low (DHT_IO);
   delay_ms (25);
   Output_high (DHT_IO);
   delay_us (30);
   bit_set (TRISB, 1); // Configure connection pin as input
}

short check_response()
{
   delay_us (40);

   if ( ! input (DHT_IO) )
   {
      // Read and test IF connection pin is low
      delay_us (80);

      if (input (DHT_IO) )
      {
         // Read and test IF connection pin is high
         delay_us (50);
         return 1;
      }
      else
          return 0;
   }
}

unsigned INT8 Read_Data(unsigned int8 *data)
{
     unsigned int8 i, k = 0; // k is used to count 1 bit reading duration

     for (i = 0; i < 8; i++)
     {
          k = 0;     
          while ( !input (DHT_IO))
          {
               if (++k > 100)      // Wait until pin goes high
                    return TIME_OUT_ERROR;
               delay_us (1);
          }

          delay_us (30);

          if ( ! input (DHT_IO) )
          bit_clear (*data, (7 - i) ); // Clear bit (7 - i)
          else
          {
               bit_set (*data, (7 - i) ); // Set bit (7 - i)
               while (input (DHT_IO))
               {
                    if (++k > 100)      // Wait until pin goes low
                         return TIME_OUT_ERROR;
                    delay_us (1);
               }
          }
     }
     return DATA_OK;
}

////////////////////////////////////////////////////////////////////////////////
// funcion que retorna lo siguiente:
//      * Temperatura TEMP(FLOAT)
//      * Humedad Relativa HR(FLOAT)
// define de la respuesta del sensor
//       UNRESPONSIVE_SENSOR  4
//       TIME_OUT_ERROR       3
//       CHECK_SUM_ERROR      2
//       DATA_OK              1
// datos recividos
//       RH_byte1  reciver_byte_sensor[0]
//       RH_byte2  reciver_byte_sensor[1]
//       T_byte1   reciver_byte_sensor[2]
//       T_byte2   reciver_byte_sensor[3]
//       Checksum  reciver_byte_sensor[4]
int get_data_sensor_dht22(float *temp_f, float *rh_f)
{
     unsigned int16 Temp, RH;
     unsigned int8 reciver_byte_sensor[5];                 //creamos la variable para recivir los datos
     int8 n;
     Start_signal ();
     
     if (check_response () )       // si hay respuesta del sensor
     {
          for(n=0; n<5; n++)
          {
               if(Read_Data(&reciver_byte_sensor[n]) == TIME_OUT_ERROR)
                    return TIME_OUT_ERROR;
          }     
          if (reciver_byte_sensor[4] == ((reciver_byte_sensor[0] + reciver_byte_sensor[1] + reciver_byte_sensor[2] + reciver_byte_sensor[3])&0xFF))    // verificamos CHECK SUM ERROR
          {           
               RH = make16 (reciver_byte_sensor[0], reciver_byte_sensor[1]);
               Temp = make16 (reciver_byte_sensor[2], reciver_byte_sensor[3]);
               *rh_f = (FLOAT) RH * 0.1;           // guardamos el valor en la dir apuntada por rh_f
               *temp_f = (FLOAT) Temp * 0.1;       // guardamos el valor en la dir apuntada por temp_f         
               if(Temp > 0X8000)
                    temp_f = temp_f * -1;      // si es temperatura negativa (Temp = Temp&0X7FFF;)         
               return DATA_OK;
          }
          else
               return CHECK_SUM_ERROR;       // si hay error de check sum retormamos con dicha vandera
     }
     else                         
          return UNRESPONSIVE_SENSOR;            // no hay respuesta del sensor
}

una foto del resultado




Devolviendo a la comunidad lo que nos ha dado
« Última modificación: 17 de Febrero de 2017, 23:44:42 por tithanae »

Desconectado Jeremy

  • PIC10
  • *
  • Mensajes: 1
Re:libreria para el modulo DHT22 o el AM2302
« Respuesta #1 en: 09 de Octubre de 2020, 01:49:37 »
Lo intenté poner en el ccs c compiler, pero al compilarlo me dice manda a el "#Byte TRISB = 0XF93" y me dice "A #DEVICE required before this line" Alguien sabe por qué me marca esto? estoy trabajando con un pic16f877A