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 code desired (RH and Temp sensor)
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Tue Nov 08, 2016 4:36 am     Reply with quote

Fair enough. I suspect he didn't bother, since there was a working driver. Smile
wided



Joined: 22 Jun 2017
Posts: 1

View user's profile Send private message Send e-mail

two dht22
PostPosted: Tue Jul 18, 2017 4:53 pm     Reply with quote

HELLO. I need your help with two DHT22 sensor and PIC18f4550.
when i use one dht22 it works but when i use two dht22 it doesn't. Can anyone help me??? Thanks anyway!!!

This is the code:

Code:

#include <18f4550.h>
#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2                                                                                                                                                         
#define LCD_DATA4 PIN_D3
#define LCD_DATA5 PIN_D4
#define LCD_DATA6 PIN_D5                                                                   
#define LCD_DATA7 PIN_D6
#fuses HS,NOWDT,NOLVP
#use delay(clock = 20000000)               
#include <lcd.c>
                       
#use fast_io(B)
#use fast_io(D)           
#BIT Data_Pin = 0xF81.0                       // Pin mapped to PORTB.0
#BIT Data_Pin_Direction = 0xF93.0             // Pin direction mapped to TRISB.0 
#BIT Data_Pin1 = 0xF83.0                       // Pin mapped to PORTB.0
#BIT Data_Pin_Direction1 = 0xF95.0             // Pin direction mapped to TRISB.0
char message1[] = "Temp = 00.0 C";
char message2[] = "RH   = 00.0 %";
short Time_out ;
unsigned int8 T_byte1, T_byte2, RH_byte1, RH_byte2, CheckSum ;
unsigned int16 Temp, RH; 
char message11[] = "Text = 00.0 C";
char message22[] = "R1   = 00.0 %";
short Time_out1 ;                                                                                           
unsigned int8 T_byte11, T_byte22, RH_byte11, RH_byte22, CheckSum1 ;
unsigned int16 Text, R1;
 
void start_signal(){
  Data_Pin_Direction = 0;              // Configure connection pin as output
  Data_Pin = 0;                        // Connection pin output low
  delay_ms(25);
  Data_Pin = 1;                        // Connection pin output high
  delay_us(30);
  Data_Pin_Direction = 1;              // Configure connection pin as input


short check_response(){
  delay_us(40);
  if(!Data_Pin){                     // Read and test if connection pin is low
    delay_us(80);
    if(Data_Pin){                    // Read and test if connection pin is high
      delay_us(50);
      return 1;}
    }
}

unsigned int8 Read_Data(){
  unsigned int8 i, k, _data = 0;     // k is used to count 1 bit reading duration
  if(Time_out)
    break;
  for(i = 0; i < 8; i++){
    k = 0;
    while(!Data_Pin){                          // Wait until pin goes high
      k++;
      if (k > 100) {Time_out = 1; break;}
      delay_us(1);}
    delay_us(30);
    if(!Data_Pin)
      bit_clear(_data, (7 - i));               // Clear bit (7 - i)
    else{
      bit_set(_data, (7 - i));                 // Set bit (7 - i)
      while(Data_Pin){                         // Wait until pin goes low
      k++;
      if (k > 100) {Time_out = 1; break;}
      delay_us(1);}
    }
  }
  return _data;
}

void start_signal1(){
  Data_Pin_Direction1 = 0;              // Configure connection pin as output
  Data_Pin1 = 0;                        // Connection pin output low
  delay_ms(25);
  Data_Pin1 = 1;                        // Connection pin output high
  delay_us(30);
  Data_Pin_Direction1 = 1;              // Configure connection pin as input


short check_response1(){
  delay_us(40);     
  if(!Data_Pin1){                     // Read and test if connection pin is low
    delay_us(80);
    if(Data_Pin1){                    // Read and test if connection pin is high
      delay_us(50);
      return 1;}
    }
}

unsigned int8 Read_Data1(){
  unsigned int8 i, k, _data1 = 0;     // k is used to count 1 bit reading duration
  if(Time_out1)             
    break;
  for(i = 0; i < 8; i++){
    k = 0;
    while(!Data_Pin1){                          // Wait until pin goes high
      k++;
      if (k > 100) {Time_out1 = 1; break;}
      delay_us(1);}
    delay_us(30);
    if(!Data_Pin1)           
      bit_clear(_data1, (7 - i));               // Clear bit (7 - i)
    else{
      bit_set(_data1, (7 - i));                 // Set bit (7 - i)
      while(Data_Pin1){                         // Wait until pin goes low
      k++;
      if (k > 100) {Time_out1 = 1; break;}
      delay_us(1);}
    }
  }
  return _data1;
}     
                     
void main(){       
  //setup_oscillator(OSC_8MHZ);                // Set internal oscillator to 8MHz
  //setup_adc_ports(NO_ANALOGS);                // Configure AN pins as digital
  lcd_init();                                 // Initialize LCD module
  lcd_putc('\f');                             // LCD clear
  while(TRUE){
    delay_ms(1000);
    Time_out = 0;
    Start_signal();
    if(check_response()){                    // If there is response from sensor
      RH_byte1 = Read_Data();                 // read RH byte1
      RH_byte2 = Read_Data();                 // read RH byte2
      T_byte1 = Read_Data();                  // read T byte1
      T_byte2 = Read_Data();                  // read T byte2
      Checksum = Read_Data();                 // read checksum
      if(Time_out){                           // If reading takes long time
        lcd_putc('\f');                       // LCD clear
        lcd_gotoxy(5, 1);                     // Go to column 5 row 1
        lcd_putc("Time out!");
      }
      else{
       if(CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF)){                                                                                                     
        RH = RH_byte1;
        RH = (RH << 8) | RH_byte2;
        Temp = T_byte1;
        Temp = (Temp << 8) | T_byte2;
        if (Temp > 0X8000){
         message1[6] = '-';
         Temp = Temp & 0X7FFF; }
        else
         message1[6] = ' ';
        message1[7]  = (Temp / 100) % 10  + 48;
        message1[8]  = (Temp / 10) % 10  + 48;
        message1[10] = Temp % 10  + 48;
        message2[7]  = (RH / 100) % 10 + 48;
        message2[8]  = (RH / 10) % 10 + 48;
        message2[10] = RH % 10 + 48;
        message1[11] = 223;                   // Degree symbol
        lcd_putc('\f');                       // LCD clear   
        lcd_gotoxy(1, 1);                     // Go to column 1 row 1
        printf(lcd_putc, message1);           // Display message1
        lcd_gotoxy(1, 2);                     // Go to column 1 row 2
        printf(lcd_putc, message2);           // Display message2
       }
        else {
          lcd_putc('\f');                     // LCD clear
          lcd_gotoxy(1, 1);                   // Go to column 1 row 1
          lcd_putc("Checksum Error!");
        }
      }
    }
    else {
      lcd_putc('\f');             // LCD clear
      lcd_gotoxy(3, 1);           // Go to column 3 row 1
      lcd_putc("No response");
      lcd_gotoxy(1, 2);           // Go to column 1 row 2
      lcd_putc("from the sensor");
    }   
   
     delay_ms(2000);
    Time_out1 = 0;
    Start_signal1(); 
    if(check_response1()){                    // If there is response from sensor
      RH_byte11 = Read_Data1();                 // read RH byte1
      RH_byte22 = Read_Data1();                 // read RH byte2
      T_byte11 = Read_Data1();                  // read T byte1
      T_byte22 = Read_Data1();                  // read T byte2
      Checksum1 = Read_Data1();                 // read checksum
      if(Time_out1){                           // If reading takes long time
        lcd_putc('\f');                       // LCD clear
        lcd_gotoxy(5, 1);                     // Go to column 5 row 1
        lcd_putc("Time out!");
      }
      else{
       if(CheckSum1 == ((RH_Byte11 + RH_Byte22 + T_Byte11 + T_Byte22) & 0xFF)){
        R1 = RH_byte11;
        R1 = (R1 << 8) | RH_byte22;
        Text = T_byte11;
        Text = (Text << 8) | T_byte22;
        if (Text > 0X8000){
         message11[6] = '-';
         Text = Text & 0X7FFF; }
        else
        message11[6] = ' ';
        message11[7]  = (Text / 100) % 10  + 48;
        message11[8]  = (Text / 10) % 10  + 48;
        message11[10] = Text % 10  + 48;
        message22[7]  = (R1 / 100) % 10 + 48;
        message22[8]  = (R1 / 10) % 10 + 48;
        message22[10] = R1 % 10 + 48;
        message11[11] = 223;                   // Degree symbol
        lcd_putc('\f');                       // LCD clear   
        lcd_gotoxy(1, 1);                     // Go to column 1 row 1
        printf(lcd_putc, message1);           // Display message1
        lcd_gotoxy(1, 2);                     // Go to column 1 row 2
        printf(lcd_putc, message2);           // Display message2
       }
        else {
          lcd_putc('\f');                     // LCD clear
          lcd_gotoxy(1, 1);                   // Go to column 1 row 1
          lcd_putc("Checksum Error!");
        }
      }
    }
    else {
      lcd_putc('\f');             // LCD clear
      lcd_gotoxy(3, 1);           // Go to column 3 row 1
      lcd_putc("No response");
      lcd_gotoxy(1, 2);           // Go to column 1 row 2
      lcd_putc("from the sensor");
    } 
   
  }
}                                                     
 
temtronic



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

View user's profile Send private message

PostPosted: Tue Jul 18, 2017 5:01 pm     Reply with quote

Simple to debug...

Using the single sensor program, confirm both DHT22 sensors work.

Copy the single sensor program, call it version2, and confirm both sensors work (obviously 1 at a time), using the 2nd I/O pin.

Now 'merge' both programs into one.. it should work fine.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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