首页 > 解决方案 > 多传感器 DHT11 与 pic 877A 在 Ccs

问题描述

我用ccs,proteus。

我有 4 个 dht11 连接到 4 针(、、、、PIN_D0PIN_D1PIN_D21PIN_D3个 LCD 以显示 DHT 的温度。

当我跑步时,3个传感器连接到PIN_D0, PIN_D1, PIN_D2,它显示温度。但是传感器连接到PIN_D3,所以错误校验和。

代码CSS:

#include <Thucodecss.h>
#define LCD_RS_PIN PIN_B0
#define LCD_RW_PIN PIN_B1
#define LCD_ENABLE_PIN PIN_B2
#define LCD_DATA4 PIN_B3
#define LCD_DATA5 PIN_B4
#define LCD_DATA6 PIN_B5
#define LCD_DATA7 PIN_B6
//End LCD module connections
 
#fuses HS,NOWDT,NOPROTECT,NOLVP
#include <lcd.c>
// Connection pin between PIC16F877A and DHT11 (RHT01) sensor
//#BIT Data_Pin = 0x08.0                       // Pin mapped to PORTD.0
//#BIT Data_Pin_Direction = 0x88.0             // Pin direction mapped to TRISD.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 ;
void start_signal(pin){
  output_drive(pin);             // Configure connection pin as output
  output_bit(pin,0);                        // Connection pin output low
  delay_ms(25);
  output_bit(pin,1);                        // Connection pin output high
  delay_us(30);
  output_drive(pin);              // Configure connection pin as inpuoutput_drive
}
short check_response(pin){
  delay_us(40);
  if(!input(pin)){                     // Read and test if connection pin is low
    delay_us(80);
    if(input(pin)){                    // Read and test if connection pin is high
      delay_us(50);
      return 1;}
    }
}
unsigned int8 Read_Data(pin){
  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(!input(pin)){                          // Wait until pin goes high
      k++;
      if (k > 100) {Time_out = 1; break;}
      delay_us(1);}
    delay_us(30);
    if(!input(pin))
      bit_clear(_data, (7 - i));               // Clear bit (7 - i)
    else{
      bit_set(_data, (7 - i));                 // Set bit (7 - i)
      while(input(pin)){                         // Wait until pin goes low
      k++;
      if (k > 100) {Time_out = 1; break;}
      delay_us(1);}
    }
  }
  return _data;
}

void XykyDHT(pin){
 
    Time_out = 0;
    Start_signal(pin);
    if(check_response(pin)){                    // If there is response from sensor
      RH_byte1 = Read_Data(pin);                 // read RH byte1
      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("RH_byte1 bi");
        delay_ms(1000);
      }
      RH_byte2 = Read_Data(pin);                 // read RH byte2
      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("RH_byte2 bi");
        delay_ms(1000);
      }
      T_byte1 = Read_Data(pin);                  // read T byte1
      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("T_byte1 bi");
        delay_ms(1000);
      }
      T_byte2 = Read_Data(pin);                  // read T byte2
      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("T_byte2 bi");
        delay_ms(1000);
      }
      Checksum = Read_Data(pin);                 // read checksum
      if(Time_out){                           // If reading takes long time
        lcd_putc('\f');                       // LCD clear
        lcd_gotoxy(1, 1);                     // Go to column 5 row 1
        lcd_putc("Checksum bi");
        lcd_gotoxy(1, 2); 
        printf(lcd_putc, "%d %d %d %d %d" ,RH_byte1,RH_byte2,T_byte1,T_byte2,Checksum); 
        delay_ms(3000);
      }
      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)){
        message1[7]  = T_Byte1/10  + 48;
        message1[8]  = T_Byte1%10  + 48;
        message1[10] = T_Byte2/10  + 48;
        message2[7]  = RH_Byte1/10 + 48;
        message2[8]  = RH_Byte1%10 + 48;
        message2[10] = RH_Byte2/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");
    }
    
}
void main(){
  lcd_init();                                 // Initialize LCD module
  lcd_putc('f');// LCD clear
  int i = 0;
  
  while(TRUE){
  set_tris_D(0);
  
   if(i ==0){
      XykyDHT(PIN_D0);
   }
   if(i == 1){
   XykyDHT(PIN_D1);
   }
   if(i == 2)
   {
   XykyDHT(PIN_D2);
   }
   if(i == 3){
   XykyDHT(PIN_D3);
   }
   
   i++;
   
   if(i > 3)
   i = 0;
   
   delay_ms(2000);
  }
}

我有一个问题

pic 877a的多少限制dht11?

为什么 dht11 pin_d0pin_d1pin_d2success 但pin_d3错误是校验和?

标签: carduino

解决方案


推荐阅读