News:

;) This forum is the property of Proton software developers

Main Menu

DHT22 Humidity Sensor Example Code

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

Previous topic - Next topic

Bob (G8GFA)

Some simple code for the DHT22 sensor.





'****************************************************************
'*  Name    : DHT22 (AM2302) Test.BAS                           *
'*  Author  : Bob Marshall                                      *
'*  Date    : 5/20/2013                                         *
'*  Version : 1.0                                               *
'****************************************************************


;**** 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 HumFract As Byte
 Dim HumData As Word
 Dim TempData As Word
 Dim TempFract As Byte
 Dim DataIn As Byte
 Dim ChkSum As Byte
 Dim Index As Byte
 Dim MinusFlag As Byte
 Dim CalcChkSum As Byte
 
 Symbol HumSenLn = PORTA.2

 '***** Special LCD Character Definition - oC. *****
 Print $FE,$40,$1C,$14,$1C,$00,$07,$04,$04,$07
 
 GoTo Main
 
 '*****************************************************************************
  GetHum:
 
    HumData=0:TempData=0:ChkSum=0:MinusFlag = 0
    Low HumSenLn:DelayMS 18
    High HumSenLn:DelayUS 30
    DataIn = PulsIn HumSenLn,1
   
    If DataIn < 30 Then
        Print At 1,1, "Sensor not ready"
        DelayMS 2000
        Cls
    Else   
        For Index = 15 To 0 Step -1
            DataIn = PulsIn HumSenLn,High
            If DataIn > 18 Then  LoadBit HumData,Index,2
        Next Index
       
        For Index = 15 To 0 Step -1
            DataIn = PulsIn HumSenLn,High                                                                             
            If DataIn > 18 Then  LoadBit TempData,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
       
        CalcChkSum = HumData.HighByte + HumData.LowByte + TempData.HighByte + TempData.LowByte
       
        If ChkSum <> CalcChkSum Then   
            Print At 2,1,"Check Sum Error!"
            DelayMS 5000
            Cls
        Else   
            HumFract = HumData//10
            HumData = HumData/10
           
            If  TempData.15 = 1 Then MinusFlag = 1
            TempData = TempData &  $7FFF
           
            TempFract = TempData//10
            TempData = TempData/10
             
            Print At 1,3,"     "
            Print At 1,1,"H:",Dec HumData ,".",Dec HumFract,"%"
            Print At 1,12,"    "
            If MinusFlag = 1 Then
                Print At 1,9,"T:-"
            Else
                Print At 1,9, " T:"
            EndIf       
            Print Dec TempData,".",Dec TempFract,0 
        EndIf
    EndIf
Return

'******************************************************************************
Main:
 Cls
 High HumSenLn
 Print At 1,4, "DHT22 Test"
 DelayMS 2000
 Cls
 While
    GoSub GetHum
    DelayMS 5000
 Wend
 End