News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

DHT11 Humidity Sensor Code

Started by Bob (G8GFA), Feb 01, 2021, 11:22 AM

Previous topic - Next topic

Bob (G8GFA)

An example of simple code to run the DHT11





'****************************************************************
'*  Name    : DHT11_Test                                        *
'*  Author  : Bob Marshall                                      *
'*  Date    : 20/05/2013                                        *
'*  Version : 1.1                                               *
'*  Notes   : Compiler Version 3.5.5.6                          *
'****************************************************************

;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 16F628A

Config FOSC_HS, WDTE_OFF, PWRTE_ON, MCLRE_ON, BOREN_ON, LVP_OFF, CPD_OFF, CP_OFF

;**** End of Fuse Configurator Settings ****
 
 Xtal = 16
 Declare LCD_Interface = 4
 Declare LCD_DTPin PORTB.4
 Declare LCD_ENPin PORTB.3
 Declare LCD_RSPin PORTB.0
 Declare LCD_Lines = 2
 Declare LCD_Type = 0

 All_Digital = true
 
 Dim DataIn As Byte
 Dim ChkSum As Byte
 Dim Index As Byte
 Dim SenData As Dword
 Dim Loop1 As Byte

 Symbol HumSenLn = PORTA.2

' Special LCD Character Definitions
 Print $FE,$40,$07,$05,$05,$07,$00,$00,$00,$00 '0  - Degree

 
 GoTo Main
 '******************************************************************************
 
 Get_Hum:
   
    SenData=0:ChkSum=0
 
    High HumSenLn
    DelayMS 50
    Low HumSenLn:DelayMS 18
    High HumSenLn:DelayUS 30
    DataIn = PulsIn HumSenLn,1

    If DataIn < 33 Then
        Print At 1,1, "Sensor not ready"
        DelayMS 2000
        Cls
    Else
        For Index = 31 To 0 Step -1
            DataIn = PulsIn HumSenLn,High 'recieve pulses from sensor
            If DataIn > 18 Then  LoadBit SenData,Index,2
        Next Index
       
        For Index = 7 To 0 Step -1
            DataIn = PulsIn HumSenLn,High
            If DataIn > 18 Then LoadBit ChkSum,Index,2
        Next Index
       
        Print At 1,1,"H:",Dec SenData.Byte3,"%"
        Print At 1,13,"    "
        Print At 1,11,"T:",Dec SenData.Byte1,0,"C"
       
        If ChkSum <> (SenData.Byte1 + SenData.Byte3) Then
            Cls
            Print At 2,1,"Check Sum Error!"
            DelayMS 2000
        EndIf
   EndIf
Return
'******************************************************************************

Main:
 Cls
 Print At 1,4, "DHT11 Test"
 DelayMS 2000
 Cls

 While
    GoSub Get_Hum
    DelayMS 5000
 Wend
 End