User Tools

Site Tools


en:pfw:dht22-msp430g2553-noforth

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:pfw:dht22-msp430g2553-noforth [2025-04-25 01:11] mkaen:pfw:dht22-msp430g2553-noforth [2025-06-26 12:41] (current) – [Sorce Code] mka
Line 1: Line 1:
 +===== Read DHT22 data using noForth on TI's Launchpad.  (bit bang) =====
 +
 +[[en:pfw:dht22#implementation_for_ti_s_launchpad|go back]]
 +==== Pseudocode ====
 +
 +<code>
 +Function: Pio    ( -- )  \ set port I/O function. SDA --> P1.0 
 +Function: wakeup ( -- )  \ notify sensor to prepare the data
 +Function: @data  ( -- sun moon hum tmp chk chksum )  \ get sensor data
 +  get response time of sensor, store in register 'sun' (just for testing)
 +  get startpulse duration, store in register 'moon'
 +  40 loop 
 +     read bit using 'moon' based delay 
 +     lshift bit into array xx yy zz
 +  08 loop       
 +     lshift array  \ adjust xx yy zz --> hum tmp chk 
 +  calculate chksum
 +Function: (dht22)  ( -- sun moon hum tmp chk chksum ) \ wake up and read sensor
 +Function:  dht22   ( -- )  \ print temperatur and humidity
 +Function:  test    ( n -- ) \ multiple readings  
 +    n loop  dht22
 +</code>
 +
 +==== Sorce Code ====
 +
 <code> <code>
-\ Read DHT22  (bitbang)+\ Read DHT22 data.  (bit bang)
 \ TI MSP430G2553 Launchpad with noForth mv 2553 240101  \ TI MSP430G2553 Launchpad with noForth mv 2553 240101 
  
 (*  (* 
-To do: timeout +To do: monitor timeout 
  
 History History
-V0065 - Temperature in tenths of a degreemk 20250424 +v0066 - Display negative temperatures correctly. 
-V0064 - Loops named with ( A ) ... ( D ). Variants of loops B and D studied. This loop variation of B and D work well. Comments revised. mk 20250423 05:25+V0065 - Represent tenths of degrees of temperatures
 +V0064 - Loops named with ( A ) ... ( D ). Variants of loops B and D studied. These loop variations of B and D work well. Comments revised. mk 20250423 05:25
 V0063 - Cleaned up the code. Data output reduced to the essentials. mk 20250421 02:04 V0063 - Cleaned up the code. Data output reduced to the essentials. mk 20250421 02:04
 V0062 - Works well, output rounded to whole digits. mk 20250419 21:51 V0062 - Works well, output rounded to whole digits. mk 20250419 21:51
Line 28: Line 54:
 026  P1SEL   Port Select 026  P1SEL   Port Select
 041  P1SEL2  Port Select2 041  P1SEL2  Port Select2
-027  P1REN   Resistor Enable +027  P1REN   Resistor Enable
 *) *)
  
Line 36: Line 62:
 hex hex
  
-helper+tools
 code cls ( -- ) s0 # sp mov  next end-code  \ clear stack code cls ( -- ) s0 # sp mov  next end-code  \ clear stack
  
 code p1H     #1 021 & .b bis   next end-code \ set lines code p1H     #1 021 & .b bis   next end-code \ set lines
-code p1L     #1 021 & .b bic   next end-code +code p1L     #1 021 & .b bic   next end-code  
-code p6H   40 # 021 & .b bis   next end-code +code p6H   40 # 021 & .b bis   next end-code  
-code p6L   40 # 021 & .b bic   next end-code+code p6L   40 # 021 & .b bic   next end-code 
  
  
Line 57: Line 83:
   pio p1H p6H 1 ms  p1L 10 ms  ( p6L ) p1H  ; \ 1 ms == 100┬Ás !   pio p1H p6H 1 ms  p1L 10 ms  ( p6L ) p1H  ; \ 1 ms == 100┬Ás !
  
-Code @data ( -- sun moon hum tmp chk chksum )  \ read sensor data+Code @data ( -- sun moon  xx  yy  zz chksum )  \ read sensor data 
 +\  i.e     ( -- sun moon hum tmp chk chksum )
  \ get response time   \ get response time 
   #1 022 & .b bic \ p1,0 IN   #1 022 & .b bic \ p1,0 IN
Line 154: Line 181:
   ;   ;
  
-: .tmp ( tmp -- )   + 
-  10 /mod 3 .r [char] emit .+: .tmp ( tmp -- ) \ Print temperature with sign, one decimal place and degree symbol  
 +  dup hx 8000 and hx 8000 = 
 +      if [char] - else [char] + then emit 
 +  hx 7fff and ( tmp -- +tmp )   
 +  10 /mod 3 .r [char] emit .
   [char] f decemit    [char] f decemit 
   [char] C emit space   [char] C emit space
-  ;  \ ja, geht. +  ;  
      
-: .hum ( hum -- )  \ rounded to whole percent+: .hum ( hum -- )  \ Print the relative humidity rounded to whole digits
   10 / . ." %rel" space    10 / . ." %rel" space 
   ;     ;  
   
-: dht22 ( -- ) \ print temperatur and humidity+: dht22 ( -- ) \ get temperatur and humidity
   cr (dht22)   ( 2 + ) \ add 2 for 'else' part   cr (dht22)   ( 2 + ) \ add 2 for 'else' part
   = if ." chk  " else ." chksum error" cls exit then   = if ." chk  " else ." chksum error" cls exit then
Line 194: Line 224:
 ( finis) ( finis)
 </code> </code>
 +
 +==== Numerical representation of the measured values ====
 +Note((This is already included in the source code.))
 +
 +
 +=== Humidity ===
 +
 +The first 16 bits coming from the DHT22 are the relative humidity in tenths of a percent. They can be processed directly with the 16-bit noForth and correspond to a positive integer. Rounded to whole digits for display purposes.
 +Here is an excerpt from the noforth source code:
 +
 +<code>
 +: .hum ( hum -- )   10 / . ." %rel" space  ;  
 +</code>
 +
 +=== Temperature ===
 +
 +The next 16 bits are the temperature in tenths of a degree Celsius. Temperatures from zero degrees and higher can be processed directly using the 16-bit noForth; they correspond to a positive integer.
 +
 +Temperatures below zero degrees are represented by the DHT22 as follows: The MSB of the 16-bits is set, but otherwise the temperature is specified as a positive integer. This number format //does not correspond// to the two's complement of noForth. Here, the MSB must first be evaluated to determine the sign. The sign is prepended for output. The measured value can then be further processed as a positive integer.  Whole degrees are displayed to the left of the decimal point, tenths to the right. The unit °C is specified.
 +<code>
 +: .tmp ( tmp -- )  
 +  dup hx 8000 and hx 8000 =  \ check MSB
 +      if [char] - 
 +      else [char] + 
 +      then emit
 +  hx 7fff and ( tmp -- +tmp )    \ reset MSB
 +  10 /mod 3 .r [char] . emit .    \ --> TTT.T
 +  [char] f decemit  [char] C emit  \ print °C
 +  space
 +  ;  
 + </code>
 +
 +
 +
en/pfw/dht22-msp430g2553-noforth.1745536286.txt.gz · Last modified: 2025-04-25 01:11 by mka