Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

DHT22 Routinen

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
Insanity

Bascom Member



Joined: 25 Apr 2021
Posts: 8

germany.gif
PostPosted: Mon Apr 26, 2021 10:39 am    Post subject: DHT22 Routinen Reply with quote

Hello everybody.
I'll set my routines for the DHT22 sensor here.
Parts of the code are from here and from Bascomforum.de, they have been changed and expanded.

Read out multiple sensors.
Calculate absolute humidity.
Calculate relative humidity.
Temperature also with minus evaluation

I use it to control a ventilation system.

greeting
Insanity

Code:


'
' Sensor DHT22
' Insanity
'

  $regfile = "m328pdef.dat"
  $hwstack = 64
  $swstack = 64
  $framesize = 128

' Mit internen Takt sind die Functionen
' und Subs zu langsam
  $crystal = 18432000

  Const Ein = 1
  Const Aus = 0
  Const Ausgang = 1
  Const Eingang = 0

' zum Berechnen ob Ent- Belüftung sinnvoll ist
' T = Temperatur          Integer
' H = Relative Feuchte    Integer
' AH = Absolute Feuchte   Single
  Declare Function Absolut_Feuchte(Byval T as Integer, Byval H As Integer ) As Single
  Declare Function Relativ_Feuchte(Byval T as Integer, Byval AH As Single ) As Integer

' zum Lesen mehrerer Sensoren mit gleicher Sub
' die Schreib und Lesefunktionen in verschiedene Subs und Function ausgelagert
  Declare Sub DHT22(T as Integer,H as Integer,Byval Sensor as Byte)   ' Hauptroutine
  Declare Sub DHT_P(Byval Zustand as Byte,Byval Sensor as Byte)       ' Pin definieren
  Declare Sub DHT(Byval Status As Byte,Byval Sensor as Byte)          ' Pin schreiben
  Declare Function DHT_L(Byval Sensor as Byte) as Byte                ' Pin lesen

' LCD Pinbelegung definieren
  Config Lcd = 16 * 2
  Config Lcdpin = Pin , Db4 = Portd.3 , Db5 = Portd.2 , Db6 = Portd.1 , Db7 = Portd.0 , E = Portd.5 , Rs = Portd.6
  Config Lcdbus = 4                                         ' 4 bit
  Cursor Off

'LCD Beleuchtung
  Config Portd.7 = Output
  Led Alias Portd.7

' Beleuchtung Ein
  Led = Ein

' Variablen Declarieren

  Dim Hygro(5) as Integer
  Dim Temp(5) as Integer
  Dim Absolut_Hygro(5) as Single
  Dim Relativ_Hygro(5) as Integer
  Dim Sensor as Byte

  Dim Text as String * 6
  Dim Flag as Byte
  Dim Dis as Byte

  Cls
  Locate 1,1  :Lcd "   DHT22 Test   "
  Locate 2,1  :Lcd "Ver:1.0 Insanity"

  Wait 2

' Sonderzeichen laden
  Deflcdchar 1,32,32,32,31,32,32,32,32'
  Deflcdchar 2,32,16,8,4,2,1,32,32'
  Deflcdchar 3,32,4,4,4,4,4,32,32'
  Deflcdchar 4,32,1,2,4,8,16,32,32'

  Deflcdchar 7,2,5,2,32,12,16,16,12 ' c°

' Sekundentakt
  Config Timer1 = Timer , Prescale = 256
  On Timer1 Takt
  Enable Timer1

' Einmal aufrufen
  Gosub Takt

' Alle Interrups freigeben
  Enable Interrupts


' Hauptschleife

  Cls
  Flag=0

  Do

  ' Damit die Anzeige nicht wirre sachen Anzeigt
    Disable Interrupts

' Externer Kombisensor
' Durch die Sensorvorgabe können verschiedene
' Hardwarepins ausgewählt werden.

    If Flag = 0 Then

      Sensor = 2
      Call DHT22(Temp(Sensor),Hygro(Sensor),Sensor)

      locate 2,11
      Lcd Sensor

' Sensordaten
      Locate 1,1  : LCD "Temp"
      Locate 1,7 : LCD Temp(Sensor);Chr(7);" "
      Locate 2,1  : LCD "Hygro"
      Locate 2,7 : LCD Hygro(Sensor);"%";" "

 ' Umrechenen in Absolute Feuchte in g/m3
      Absolut_Hygro(Sensor) = Absolut_Feuchte(Temp(Sensor),Hygro(Sensor))

 ' und wieder zurück in Relative Feuchte %
     Relativ_Hygro(Sensor) = Relativ_Feuchte(Temp(Sensor),Absolut_Hygro(Sensor))

' Absolute Feuchte berechnet
      Locate 2,12
      Text = Fusing (Absolut_Hygro(Sensor),"#.##" )
      Text = "   " + Text
      Text = Right(Text , 5)
      LCD Text

' Relative Feuchte zurückgerechnet
      Locate 1,12
      Lcd Relativ_Hygro(Sensor);"%"

      Flag = 1

    Else

      Sensor = 5
      Call DHT22(Temp(Sensor),Hygro(Sensor),Sensor)

      locate 2,11
      Lcd Sensor

' Sensordaten
      Locate 1,1  : LCD "Temp"
      Locate 1,7 : LCD Temp(Sensor);Chr(7);" "
      Locate 2,1  : LCD "Hygro"
      Locate 2,7 : LCD Hygro(Sensor);"%";" "

 ' Umrechenen in Absolute Feuchte in g/m3
      Absolut_Hygro(Sensor) = Absolut_Feuchte(Temp(Sensor),Hygro(Sensor))

 ' und wieder zurück in Relative Feuchte %
      Relativ_Hygro(Sensor) = Relativ_Feuchte(Temp(Sensor),Absolut_Hygro(Sensor))

' Absolute Feuchte berechnet
      Locate 2,12
      Text = Fusing (Absolut_Hygro(Sensor),"#.##" )
      Text = "   " + Text
      Text = Right(Text , 5)
      LCD Text

' Relative Feuchte zurückgerechnet
      Locate 1,12
      Lcd Relativ_Hygro(Sensor);"%"

      Flag=0

    End If

    Enable Interrupts

'  DHT22 max alle 0,5 Sek aufrufen
    wait 2

  Loop

End



Takt:

  Load Timer1 , 10000

' kleine spielerei
  incr Dis
  Locate 1,16
  Lcd Chr(Dis)
  if Dis => 4 then Dis=0

Return



' Verschiedene Ports zuweisen
' für Verschiedene Sensoren
' Eingang oder Ausgang
'
Sub DHT_P(Zustand,Sensor)

  Select Case Sensor
    Case 0
      If Zustand = Ausgang Then
        Config Portc.0 = Output
      Else
        Config Portc.0 = Input
      End IF
    Case 1
      If Zustand = Ausgang Then
        Config Portc.1 = Output
      Else
        Config Portc.1 = Input
      End IF
    Case 2
      If Zustand = Ausgang Then
        Config Portc.2 = Output
      Else
        Config Portc.2 = Input
      End IF
    Case 3
      If Zustand = Ausgang Then
        Config Portc.3 = Output
      Else
        Config Portc.3 = Input
      End IF
    Case 4
      If Zustand = Ausgang Then
        Config Portc.4 = Output
      Else
        Config Portc.4 = Input
      End IF
    Case 5
      If Zustand = Ausgang Then
        Config Portc.5 = Output
      Else
        Config Portc.5 = Input
      End IF
  End Select

End Sub

'
' Verschiedene Ports Schreiben
' für Verschiedene Sensoren
'
Sub DHT(Status,Sensor)

  Select Case Sensor
     Case 0
      If Status = Ein Then
        Portc.0 = Ein
      Else
        Portc.0 = Aus
      End IF
    Case 1
      If Status = Ein Then
        Portc.1 = Ein
      Else
        Portc.1 = Aus
      End IF
    Case 2
      If Status = Ein Then
        Portc.2 = Ein
      Else
        Portc.2 = Aus
      End IF
    Case 3
      If Status = Ein Then
        Portc.3 = Ein
      Else
        Portc.3 = Aus
      End IF
    Case 4
      If Status = Ein Then
        Portc.4 = Ein
      Else
        Portc.4 = Aus
      End IF
    Case 5
      If Status = Ein Then
        Portc.5 = Ein
      Else
        Portc.5 = Aus
      End IF
   End Select

End Sub

'
' Verschiedene Ports Lesen
' für Verschiedene Sensoren
'
Function DHT_L(Sensor)

  Select Case Sensor
    Case 0
      DHT_L = Pinc.0
    Case 1
      DHT_L = Pinc.1
    Case 2
      DHT_L = Pinc.2
    Case 3
      DHT_L = Pinc.3
    Case 4
      DHT_L = Pinc.4
    Case 5
      DHT_L = Pinc.5
  End Select

End Function

'
' Lese Sub
' für Temp und Hygro
' DHT22 Sensor
'
Sub DHT22(T,H,Sensor)

  Local Chksum As Byte
  Local Crc As Byte
  Local Sensor_data As String * 40
  Local Count As Byte
  Local Te As integer
  Local Hy As integer
  Local Data_temp As String * 16
  local Sicherheit as Word

  Count = 0
  Sensor_data = ""

' Start Messung
  Call DHT_P(Ausgang,Sensor)
  Call DHT(Aus,Sensor)        ' Pegel auf 0
  Waitms 2

' Umschalten auf Eingang und auf Antwort warten
  Call DHT(Aus,Sensor)
  Waitus 20
  Call DHT_P(Eingang,Sensor)
  Call DHT(Ein,Sensor)        ' Pullup Widerstand

' Sensor reagiert nicht - Fehler und Exit
  Waitus 40
  If DHT_L(Sensor) = 1 Then
    T = 1000        ' Fehlercode
    H = 1000
    Exit Sub
  End If

' Sensor reagiert nicht - Fehler und Exit
  Waitus 80
  If DHT_L(Sensor) = 0 Then
    T = 2000        ' Fehlercode
    H = 2000
    Exit Sub
  End If

' Daten Einlesen
  Sicherheit=0
  While DHT_L(Sensor) = 1
    Incr Sicherheit
    If Sicherheit > 1000 Then
      T = 3000      ' Fehlercode
      H = 3000
      Exit Sub
    End If
  Wend

  Do
    While DHT_L(Sensor) = 0 : Wend
    Waitus 30
    If DHT_L(Sensor) = 1 Then
      Sensor_data = Sensor_data + "1"
      While DHT_L(Sensor) = 1
        Incr Sicherheit
        If Sicherheit > 1000 Then
          T = 4000  ' Fehlercode
          H = 4000
          Exit Sub
        End If
      Wend
    Else
      Sensor_data = Sensor_data + "0"
    End If
    Incr Count
  Loop Until Count = 40

' Messung fertig - Sensor abschalten
  Call DHT_P(Ausgang,Sensor)
  Call DHT(Ein,Sensor)

' Daten auswerten
  Data_temp = Left(sensor_data , 16)
  Hy = Binval(Data_temp)

  Data_temp = Mid(sensor_data , 17 , 16)
  Te = Binval(Data_temp)

' Checksumme
  Data_temp = Right(sensor_data , 8)
  Crc = Binval(Data_temp)

  Chksum = Low(Hy ) + High(Hy )
  Chksum = Chksum + Low(Te)
  Chksum = Chksum + High(Te)

  ' Minus Grade
  If Te.15 = 1 Then
    Te=Te + 32767
    Te=TE * -1
  End If

  If Chksum = Crc Then
    H=Hy/10         ' Werte werden in Zentel ausgegeben
    T=Te/10         ' darum hier durch 10 teilen
  Else
    T = 5000        ' Fehlercode
    H = 5000
  End If

End Sub


Function Absolut_Feuchte(T,H)
  Local T1 as Single
  Local T2 as Single
  Local AF as Single
  T1 = 237 + T
  T2 = 7.5 * T
  T1 = T2 / T1
  AF = 10 ^ T1
  T1 = 273.16 + T
  AF = AF / T1
  AF= AF * H
  AF= 13.233 * AF
  Absolut_Feuchte = AF
End Function

Function Relativ_Feuchte(T,AH)
  Local T1 as Single
  Local T2 as Single
  Local AF as Single
  Local TF as Single
  Local RF as Integer
  T1 = 237 + T
  T2 = 7.5 * T
  T1 = T2 / T1
  AF = 10 ^ T1
  T1 = 273.16 + T
  TF = AF / T1
  AF = AH / 13.233
  AF = AF/TF
  RF = Round(AF)
  Relativ_Feuchte = RF
End Function
 
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum