Autor Tema: DHT22  (Leído 1635 veces)

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

Desconectado picmex

  • PIC10
  • *
  • Mensajes: 3
DHT22
« en: 18 de Abril de 2016, 23:34:39 »
.Buenas les dejo un codigo para el sensor dht22...se basa en las librerias de pic trance y zerovlc del tema http://www.todopic.com.ar/foros/index.php?topic=35680.0 falta pulirlo pero teniendo en cuenta de que soy nuevo en esto, pues creo está bien,utilizo un cristal de 48M ..

Código: [Seleccionar]
#include <18f4550.h>           //PIC utilizado
#device adc=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG
#use delay(clock=20M)
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48M)
#build (reset=0x1000,interrupt=0x1008)
#org 0x0000,0x0FFF{}
//#use i2c(Master,slow,sda=PIN_A4,scl=PIN_A5)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "dht22.c"
#include <LCD420.c>

void main(){
   int16 lux;
   float dhthum, dthtemp;
   setup_adc_ports(AN0|VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL);
   EXT_INT_EDGE(H_TO_L);
   lcd_init();
lcd_putc("\fDHT22");
   delay_ms(500);
printf(lcd_putc,"\f");
lcd_gotoxy(1,1);
printf(lcd_putc,"Control");
lcd_gotoxy(1,2);
printf(lcd_putc,"Temperatura");
lcd_gotoxy(1,3);
printf(lcd_putc,"y Humedad");
   dht_init();
 
   delay_ms(300);
   lcd_putc("\f");
while(true){
   set_adc_channel(0);
   delay_ms(1);
   lux=read_adc();
   delay_ms(10);
   leer_dht22(dhthum, dthtemp);
   delay_ms(10);
   delay_ms(1);
lcd_gotoxy(1,1);
printf(lcd_putc,"Temp= %f %cC", dthtemp,223);
lcd_gotoxy(1,4);
printf(lcd_putc,"Hum= %f %%",dhthum);
 delay_ms(2000);
« Última modificación: 20 de Abril de 2016, 00:22:05 por picmex »

Desconectado KILLERJC

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 8242
Re:Ayuda DHT22...no marca valores
« Respuesta #1 en: 19 de Abril de 2016, 06:00:56 »
Por si las dudas alguien quiere darte una mano.. Aca dejo el codigo identado y para el creador del post, identar hace mas facil la lectura, y tambien no deberias incluir .c en el main.c, para eso estan los archivos .h

dht.c
Código: C
  1. #define dht22 PIN_A2
  2. #bit dht_io = 0xf92.2
  3.  
  4. byte dht22_dat[5];
  5. byte leer_datos_dht();
  6.  
  7. Void dht_init (void)
  8. {
  9.         dht_io=0;
  10.         delay_ms(1);
  11.         output_high(dht22);
  12. }
  13.  
  14. byte leer_datos_dht()
  15. {
  16.         byte i = 0;
  17.         byte result=0;
  18.         for (i=0; i< 8; i++)
  19.         {
  20.                 //We enter this during the first start bit (low for 50uS) of the byte
  21.                 //Next: wait until pin goes high
  22.                 while(input(dht22)==0);
  23.                 delay_us(30);
  24.                 if (input(dht22)==1)
  25.                 {
  26.                         result |=(1<<(7-i));
  27.                 }
  28.                 while (input(dht22)==1);
  29.         }//end of "for.."
  30.         return result;
  31. }
  32.  
  33.  
  34. Void leer_dht22 (float &dhthum,float &dthtemp)
  35. {
  36.         byte dht22_in, i, dht22_checksum;
  37.         int16 temperatura, humedad;
  38.         float temp,hum;
  39.         dht_io=0; // configurar el pin como salida
  40.         output_high(dht22);
  41.         delay_us(20);
  42.         output_low(dht22);
  43.         delay_ms(18);
  44.         output_high(dht22);
  45.         delay_us(22);
  46.         dht_io=1;// configurar el pin como entrada
  47.         delay_us(5);
  48.         dht22_in=input(dht22);
  49.         if(dht22_in)
  50.         {
  51.                 printf("\r\ndht condicion 1 de inicio no encontrada");
  52.                 return;
  53.         }
  54.         delay_us(80);
  55.         dht22_in=input(dht22);
  56.         if(!dht22_in)
  57.         {
  58.                 printf("\r\ndht condicion 2 de inicio no encontrada");
  59.                 return;
  60.         }
  61.         delay_us(80);
  62.         for (i=0; i<5; i++)
  63.         {
  64.                 dht22_dat[ i ] = leer_datos_dht(); // capturando datos
  65.         }
  66.  
  67.         dht_io=0;              
  68.         delay_us(10);
  69.         output_high(dht22);
  70.  
  71.         dht22_checksum = dht22_dat[0]+dht22_dat[1]+dht22_dat[2]+dht22_dat[3];
  72.         if(dht22_dat[4]!=dht22_checksum)
  73.         {
  74.                 printf("\r\nDHT checksum error");
  75.         }
  76.  
  77.         humedad = make16(dht22_dat[0],dht22_dat[1]);
  78.         temperatura = make16(dht22_dat[2],dht22_dat[3]);
  79.         hum = humedad;
  80.         temp = temperatura;
  81.         dhthum = (hum)/10;
  82.         dthtemp = (temp)/10;
  83. }//Fin Driver

main.c
Código: C
  1. #include <18f4550.h>
  2.  
  3. #device adc=10 //Usa resolución de 10 bits
  4. #fuses HS,NOWDT,NOPUT,NOLVP,NOBROWNOUT,NOWRT,NOPROTECT
  5. #use delay(clock=8000000)
  6. #include "lcd420.C" // cambiar comillas
  7. #include "dht.C" // cambiar comillas
  8.  
  9. void main()
  10. {
  11.         float dhthum, dthtemp;
  12.         delay_ms(300);
  13.         while(true)
  14.         {
  15.                 leer_dht22(dhthum, dthtemp);
  16.                 delay_ms(10);
  17.                 printf(lcd_putc,"\fHum= %f %%",dhthum);
  18.                 printf(lcd_putc,"\nTemp= %f %cC", dthtemp,223);
  19.                 printf("\r\n%f",dthtemp);
  20.                 delay_ms(2000);
  21.         }
  22. }

Una cosa mas,, probaste ponelo en FAST_IO(A) ?, los pines estan en digital?, por que le puertoA viene analogico en el reset.
Si no sabes como pasarlo a analogico podrias usar un pin del puerto B que no posee la opcion de analogico.
Le pusiste la resistencia del Pull-up en el pin ?, asi mirando rapido el codigo solo veo un error de concepto que es esto:

Código: C
  1. dht_io=0;              
  2.         delay_us(10);
  3.         output_high(dht22);

Y que se deberia quitar. Pero no creo que te afecte tanto.

Desconectado picmex

  • PIC10
  • *
  • Mensajes: 3
Re:Ayuda DHT22...no marca valores
« Respuesta #2 en: 20 de Abril de 2016, 00:23:27 »
Gracias por el aporte, ya he logrado echarlo a andar, y he hecho las correcciones en el tema, gracias por los consejos, soy nuevo programando en ccs, siempre habia utilizado basic y esto es nuevo para mi.

Por si las dudas alguien quiere darte una mano.. Aca dejo el codigo identado y para el creador del post, identar hace mas facil la lectura, y tambien no deberias incluir .c en el main.c, para eso estan los archivos .h

dht.c
Código: C
  1. #define dht22 PIN_A2
  2. #bit dht_io = 0xf92.2
  3.  
  4. byte dht22_dat[5];
  5. byte leer_datos_dht();
  6.  
  7. Void dht_init (void)
  8. {
  9.         dht_io=0;
  10.         delay_ms(1);
  11.         output_high(dht22);
  12. }
  13.  
  14. byte leer_datos_dht()
  15. {
  16.         byte i = 0;
  17.         byte result=0;
  18.         for (i=0; i< 8; i++)
  19.         {
  20.                 //We enter this during the first start bit (low for 50uS) of the byte
  21.                 //Next: wait until pin goes high
  22.                 while(input(dht22)==0);
  23.                 delay_us(30);
  24.                 if (input(dht22)==1)
  25.                 {
  26.                         result |=(1<<(7-i));
  27.                 }
  28.                 while (input(dht22)==1);
  29.         }//end of "for.."
  30.         return result;
  31. }
  32.  
  33.  
  34. Void leer_dht22 (float &dhthum,float &dthtemp)
  35. {
  36.         byte dht22_in, i, dht22_checksum;
  37.         int16 temperatura, humedad;
  38.         float temp,hum;
  39.         dht_io=0; // configurar el pin como salida
  40.         output_high(dht22);
  41.         delay_us(20);
  42.         output_low(dht22);
  43.         delay_ms(18);
  44.         output_high(dht22);
  45.         delay_us(22);
  46.         dht_io=1;// configurar el pin como entrada
  47.         delay_us(5);
  48.         dht22_in=input(dht22);
  49.         if(dht22_in)
  50.         {
  51.                 printf("\r\ndht condicion 1 de inicio no encontrada");
  52.                 return;
  53.         }
  54.         delay_us(80);
  55.         dht22_in=input(dht22);
  56.         if(!dht22_in)
  57.         {
  58.                 printf("\r\ndht condicion 2 de inicio no encontrada");
  59.                 return;
  60.         }
  61.         delay_us(80);
  62.         for (i=0; i<5; i++)
  63.         {
  64.                 dht22_dat[ i ] = leer_datos_dht(); // capturando datos
  65.         }
  66.  
  67.         dht_io=0;              
  68.         delay_us(10);
  69.         output_high(dht22);
  70.  
  71.         dht22_checksum = dht22_dat[0]+dht22_dat[1]+dht22_dat[2]+dht22_dat[3];
  72.         if(dht22_dat[4]!=dht22_checksum)
  73.         {
  74.                 printf("\r\nDHT checksum error");
  75.         }
  76.  
  77.         humedad = make16(dht22_dat[0],dht22_dat[1]);
  78.         temperatura = make16(dht22_dat[2],dht22_dat[3]);
  79.         hum = humedad;
  80.         temp = temperatura;
  81.         dhthum = (hum)/10;
  82.         dthtemp = (temp)/10;
  83. }//Fin Driver

main.c
Código: C
  1. #include <18f4550.h>
  2.  
  3. #device adc=10 //Usa resolución de 10 bits
  4. #fuses HS,NOWDT,NOPUT,NOLVP,NOBROWNOUT,NOWRT,NOPROTECT
  5. #use delay(clock=8000000)
  6. #include "lcd420.C" // cambiar comillas
  7. #include "dht.C" // cambiar comillas
  8.  
  9. void main()
  10. {
  11.         float dhthum, dthtemp;
  12.         delay_ms(300);
  13.         while(true)
  14.         {
  15.                 leer_dht22(dhthum, dthtemp);
  16.                 delay_ms(10);
  17.                 printf(lcd_putc,"\fHum= %f %%",dhthum);
  18.                 printf(lcd_putc,"\nTemp= %f %cC", dthtemp,223);
  19.                 printf("\r\n%f",dthtemp);
  20.                 delay_ms(2000);
  21.         }
  22. }

Una cosa mas,, probaste ponelo en FAST_IO(A) ?, los pines estan en digital?, por que le puertoA viene analogico en el reset.
Si no sabes como pasarlo a analogico podrias usar un pin del puerto B que no posee la opcion de analogico.
Le pusiste la resistencia del Pull-up en el pin ?, asi mirando rapido el codigo solo veo un error de concepto que es esto:

Código: C
  1. dht_io=0;              
  2.         delay_us(10);
  3.         output_high(dht22);

Y que se deberia quitar. Pero no creo que te afecte tanto.


 

anything