Wednesday, December 27, 2023

PIC16F887 DHT-11 LCD Example Using XC8

DHT11 converts the surrounding temperature and humidity to serial digital data for any micro-processor. It uses only one serial data wire. It able to convert between 0 and 50 degree Celsius in temperature, and 0 to 90RH for humidity. It's suitable for air conditioning system, agricultural application, etc.

PIC16F887 DHT-11 LCD Example Using XC8
DHT-11 Sensor

PIC16F887 DHT-11 LCD Example Using XC8
Prototyping Board

It contains 5 bytes (40 bits) of digital data. They are 1 byte of humidity, 1 byte of temperature, and 1 byte of check sum. For more information about decoding its data byte please check this post.

PIC16F887 DHT-11 LCD Example Using XC8
DTH-11 Sensor

 
PIC16F887 DHT-11 LCD Example Using XC8
TC1604A-01 16x4 Character LCD Module

In this example, I use an 8-bit PIC micro-controller PIC16F887, a DHT-11 sensor module, and a 16x2 character LCD to read and display the environmental data. I use the Microchip MPLABX IDE v6.15 and its XC8 C compiler v2.36 (free version).

  1. /*
  2.  * File: main.c
  3.  * Author: Admin
  4.  *
  5.  * Created on December 26, 2023, 9:03 PM
  6.  * PIC16F887 DHT-11 Sensor and LCD Example
  7.  * MPLABX IDE v6.15 XC8 v2.36
  8.  */
  9.  
  10. #include <xc.h>
  11. #include "config.h"
  12. #include "LCD4Bits.h"
  13. #include <stdio.h>
  14.  
  15. #define _XTAL_FREQ 8000000UL
  16.  
  17. #define DATA_PIN RD0
  18. #define DATA_DIR TRISD0
  19.  
  20. uint8_t data[5];
  21. void readDHT11(void){
  22. for(uint8_t i=0;i<5;i++) data[i]=0;
  23. DATA_DIR=0;
  24. DATA_PIN=1;
  25. __delay_ms(10);
  26. DATA_PIN=0;
  27. __delay_ms(18);
  28. DATA_PIN=1;
  29. __delay_us(30);
  30. DATA_PIN=0;
  31. DATA_DIR=1;
  32. __delay_us(10);
  33. while(DATA_PIN==0);
  34. __delay_us(10);
  35. while(DATA_PIN==1);
  36. __delay_us(10);
  37.  
  38. //Start of Transmission
  39. for(uint8_t i=0;i<5;i++) {
  40. for(uint8_t j=0;j<8;j++){
  41. __delay_us(5);
  42. while(DATA_PIN==0);
  43. __delay_us(50);
  44. if(DATA_PIN==1) { data[i]|=(1<<7-j); while(DATA_PIN==1);}
  45. }
  46. __delay_us(10);
  47. }
  48. /*CRC Calculation - the last byte data[4] is check sum*/
  49. uint8_t crc = data[0]+data[1]+data[2]+data[3];
  50. if(crc!=data[4]) {for(uint8_t i=0;i<4;i++) data[i]=0; return;}
  51. __delay_us(10);
  52. }
  53.  
  54.  
  55. void main(void) {
  56. OSCCONbits.IRCF=7;
  57. char msg[8];
  58. PORTA=0;
  59. TRISA=0;
  60. lcdInit();
  61. lcdString("Temperature:");
  62. __delay_ms(1000);
  63. while(1){
  64. readDHT11();
  65. sprintf(msg,"%2dRH",data[0]);
  66. lcdXY(1,2); lcdString(msg);
  67. sprintf(msg,"%2d%cC",data[2],223);
  68. lcdXY(8,2); lcdString(msg);
  69. __delay_ms(500);
  70. }
  71. return;
  72. }
  73.  

I use its 8MHz internal oscillator clock source leaving the XTAL1 and XTAL2 pin opened.

PIC16F887 DHT-11 LCD Example Using XC8

Dashboard

PIC16F887 DHT-11 LCD Example Using XC8

Running Program



PIC16F887 DHT-11 LCD Example Using XC8

Running Program

 

PIC16F887 DHT-11 LCD Example Using XC8

Simulating Program


Proteus VSM can be used for circuit design and simulation. Click here to download its source file.



No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (56) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (47) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (71) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (3) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (31) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)