#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);
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.cCódigo: C
#define dht22 PIN_A2 #bit dht_io = 0xf92.2 byte dht22_dat[5]; byte leer_datos_dht(); Void dht_init (void) { dht_io=0; delay_ms(1); output_high(dht22); } byte leer_datos_dht() { byte i = 0; byte result=0; for (i=0; i< 8; i++) { //We enter this during the first start bit (low for 50uS) of the byte //Next: wait until pin goes high while(input(dht22)==0); delay_us(30); if (input(dht22)==1) { result |=(1<<(7-i)); } while (input(dht22)==1); }//end of "for.." return result; } Void leer_dht22 (float &dhthum,float &dthtemp) { byte dht22_in, i, dht22_checksum; int16 temperatura, humedad; float temp,hum; dht_io=0; // configurar el pin como salida output_high(dht22); delay_us(20); output_low(dht22); delay_ms(18); output_high(dht22); delay_us(22); dht_io=1;// configurar el pin como entrada delay_us(5); dht22_in=input(dht22); if(dht22_in) { return; } delay_us(80); dht22_in=input(dht22); if(!dht22_in) { return; } delay_us(80); for (i=0; i<5; i++) { dht22_dat[ i ] = leer_datos_dht(); // capturando datos } dht_io=0; delay_us(10); output_high(dht22); dht22_checksum = dht22_dat[0]+dht22_dat[1]+dht22_dat[2]+dht22_dat[3]; if(dht22_dat[4]!=dht22_checksum) { } humedad = make16(dht22_dat[0],dht22_dat[1]); temperatura = make16(dht22_dat[2],dht22_dat[3]); hum = humedad; temp = temperatura; dhthum = (hum)/10; dthtemp = (temp)/10; }//Fin Driver
main.cCódigo: C
#include <18f4550.h> #device adc=10 //Usa resolución de 10 bits #fuses HS,NOWDT,NOPUT,NOLVP,NOBROWNOUT,NOWRT,NOPROTECT #use delay(clock=8000000) #include "lcd420.C" // cambiar comillas #include "dht.C" // cambiar comillas void main() { float dhthum, dthtemp; delay_ms(300); while(true) { leer_dht22(dhthum, dthtemp); delay_ms(10); delay_ms(2000); } }
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
dht_io=0; delay_us(10); output_high(dht22);
Y que se deberia quitar. Pero no creo que te afecte tanto.