CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

DHT22 - PIC 24FJ64GA002

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
asiklariskostas



Joined: 07 Jul 2013
Posts: 16
Location: Greece

View user's profile Send private message Send e-mail Visit poster's website

DHT22 - PIC 24FJ64GA002
PostPosted: Mon Nov 11, 2013 9:21 am     Reply with quote

Hello everyone. I need your help with DHT22 sensor and PIC24FJ64GA002. According to a previous topic, i tried to compile your code to my project, but my hyperterminal is blank - i don't receive any messages. Can anyone help me??? Thanks anyway!!!

I use PCWHD Compiler (version 5.008).
Here is the program that i've made:

Code:

#include <24fj64ga002.h>
#include <stdio.h>
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOJTAG                   //JTAG disabled
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOWRT                    //Program memory not write protected
//#FUSES DEBUG                    //Debug mode for use with ICD
#FUSES ICSP1                    //ICD uses PGC1/PGD1 pins
#FUSES NOWINDIS                 //Watch Dog Timer in Window mode
#FUSES WPOSTS16                 //Watch Dog Timer PostScalar 1:32768
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES PR                       //Primary Oscillator
#FUSES NOCKSFSM           
#FUSES NOOSCIO                  //OSC2 is clock output
#FUSES HS                       //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD                    //Internal External Switch Over mode disabled               
#FUSES WUT_DEFAULT           
#FUSES WPOSTS1                  //Watch Dog Timer PostScalar 1:1

#use delay(xtal=16000000)

#use rs232(baud=9600, xmit=PIN_A0, rcv=PIN_A1,bits=8)

int bExit;
int DHT_Data;
int iByteIndex;
int CRC;

void DHT_Start(void)
{

   // Here we send the 'start' sequence to the DHT sensor! We pull the DHT_IO pin low for 25mS, and
   // then high for 30mS. The DHT sensor then responds by pulling the DHT_IO pin low, and then pulling it
   // back high.
   
   Output_low(PIN_B3);
   delay_ms(25);
   Output_high(PIN_B3);
   delay_us(30);
   
   // Here we wait while the DHT_IO pin remains low.....
   bExit = False;
   set_timer2(255);
   while (!input(PIN_B3) && !bExit);

   // Here we wait while the DHT_IO pin remains high.....
   bExit = False;
   set_timer2(255);
   while (input(PIN_B3) && !bExit);
}

int8 DHT_ReadData(void)
{
   
   // This subroutine reads a byte of data (8 bits) from the DHT sensor...
   int8 iIndex;
   int8 iValue = 0;

   for(iIndex = 0; iIndex < 8 ; iIndex++)
   {
      bExit = False;
      set_timer2(255);
      while (!input(PIN_B3) && !bExit);
      delay_us(30);

      if (input(PIN_B3))
      {
         iValue = iValue |(1<<(7 - iIndex));
         bExit = False;
         set_timer2(255);
         while (input(PIN_B3) && !bExit);
      }
   }

   return iValue;   
}

void main()
{
 // Here we send the initialization sequence to "wake up" the DHT sensor, and put it into
      // data transmission mode.
      DHT_Start();
int i;
      // Here we read a total of 5 bytes of data (40 bits) from the DHT sensor. For the DHT-11 sensor,
      // the integer data is contained in the 1st (Humidity), and 3rd (Temperature) bytes transmitted,
      // and the 2nd and 4th decimal bytes are always zero (0). For the DHT-22 sensor, the integer data is 
      // is contained in the 1st (Humidity), and 3rd (Temperature) bytes transmitted, and the decimal
      // data is contained in the 2nd (Humidity), and 4th (Temperature) bytes transmitted. The 5th byte
      // contains the CRC byte for both sensor types.
      for (i = 0; i < 5 ; i++)
      {
         DHT_Data(i) = DHT_ReadData();
      }

      // Here we calculate the CRC by adding the 1st four bytes, and looking at only the lower 8 bits.
      // This value should match the 5th byte sent by the sensor which is the CRC byte!
      CRC = DHT_Data[0] + DHT_Data[1] + DHT_Data[2] + DHT_Data[3];
      CRC = CRC & 0xFF;

      if (CRC != DHT_Data[4] && !bExit)
         fprintf(PC, "CRC Error, expected: %Lu, and received: %u\n\r", CRC, DHT_Data[4]);

      if (bSensorType == DHT11)
      {
         Humidity = DHT_Data[0];
            Temperature = DHT_Data[2];
         //fprintf(PC, "Humidity: %ld%%\n\r", Humidity);
         //fprintf(PC, "Temperature: %ldC\n\r", Temperature);
      }
      else
      {
            Humidity = Make16(DHT_Data[0], DHT_Data[1]);
         Temperature = Make16(DHT_Data[2] & 0x7F, DHT_Data[3]);   //Strip off the sign bit first!
            //Temperature /= 10;
            if (DHT_Data[2] & 0x80)
            Temperature *= -1;
         //fprintf(PC, "Humidity: %3.1w%%\n\r", Humidity);
         //fprintf(PC, "Temperature: %3.1wC\n\r", Temperature);
      }
}
temtronic



Joined: 01 Jul 2010
Posts: 9162
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Nov 11, 2013 10:42 am     Reply with quote

Since you're not receiving any messages, it sounds like a communications problem.

Start with a simple 'hello world' program to get the serial stuff right.
Simple things like...
1) proper RS232 xcvr( like MAX232) connected right ?
2) proper connections to PC(pin 2 is rcv, 5 is ground if DE9 connector)
3) PIC running at correct speed? The 1Hz flashing LED program will confirm this...
4) disable any and all peripherals that may be connected to the I/O pins you're using.ADC,PWM,etc...must be turned off.
5) confirm that Hyprerterminal is running. simple wire from pin 3 to pin 2 creates a 'loopback.press a key it MUST be displayed on screen,if not,well there's a huge problem that must be solved.

You have to program in 'steps'.Confirm the simple things work BEFORE you cut your main code.

hth
jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group