728x90

728x90

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

320x50

Search This Blog

tyro-728x90