RH/Temp Sensor Example Code


Results 1 to 8 of 8

Threaded View

  1. #1
    Join Date
    Jul 2003
    Posts
    2,405

    Default RH/Temp Sensor Example Code

    Someone asked me a few questions recently on using one of these new RH/Temp sensors, so,
    of course, I had to have one to mess with & grabbed one for testing.

    These are really nice little units for anyone looking to measure relative humidity & temperature.

    http://www.preconusa.com/humidity_mo...ew_sensors.htm

    I thought someone here may find some use for portions of a test routine I put together, so here it is.
    Code:
      ' MPASMWIN V5.02 assembler. PBP V2.46a 674 words.
        DEFINE  LOADER_USED 1
        DEFINE  OSC 4
        DEFINE  HSER_TXSTA 24H ' TX enabled, 8-bit, BRGH=1 for high-speed
        DEFINE  HSER_SPBRG 25  ' Set baud rate generator for 9600bps @4MHz
        DEFINE  HSER_RCSTA 90H ' USART enabled, 8-bit, continuous receive
        TEMP    VAR BYTE[12]   ' Array for sensor data
        SIGN    VAR TEMP(7)    ' Alias to TEMP(7). Sensors ± temp sign bit location
        RESULTF VAR WORD       ' For °C to °F conversion
        DEG     CON 176        ' ASCII ° symbol for degrees
        NegC    CON "-"        ' -°C is represented by the ASCII "-" symbol
        ' Note: Positive temp is represented by an ASCII space
        ' Sensor output = 9600bps ASCII format as: H xx.x T±xx.x CR
    
        ' Program serial output 9600bps ASCII format: RH=11.5% : TEMP=21.8°C : 71.24°F
    MAIN:
        PAUSE 1000  ' Let everything initialize & delay between display updates
        
        ' Synch with HS-200DD RH/Temp sensor ASCII H character output
        SERIN2 PORTB.0,84,[WAIT("H"),STR TEMP\12] ' HS-2000DD input on RB0
        
        ' Parse & display relative humidity reading from HS-200DD sensor
        HSEROUT ["RH=",TEMP(1),TEMP(2),TEMP(3),TEMP(4),"% : "]
        
        ' Parse & display temperature reading from HS-2000DD sensor
        HSEROUT ["TEMP=",TEMP(8),TEMP(9),TEMP(10),TEMP(11),DEG,"C : "]
        
        ' Parse & remove ASCII component & build whole integer number
        RESULTF = ((TEMP(8)-48)*100)+((TEMP(9)-48)*10)+(TEMP(11)-48)
        
        ' I.E. ASCII temp "24.9" now = 249d for a useable whole integer number.
    
        ' Now take ((resultf * 18)/100)+32 for whole number, and resultf // 100
        ' for the remainder.
        
        ' Example: 41.0°C = 105.8°F. ((410*18)/100)+32 = whole the number result.
        ' 7,380/100 = 73. We're dropping the .8 and adding 73 + 32 for 105.
        ' Then with RESULTF//100 we end up with 8 as the remainder for 105.8°F
         
        RESULTF = RESULTF * 18        ' °C * 9/5
        IF Sign = NegC THEN Negative  ' If -°C jump to -result handler
        
        ' This handles everything from 99.9°C (211.82°F) to 00.0°C (32.0°F)
        HSEROUT [DEC (RESULTF/100)+32,".",DEC2 RESULTF//100,DEG,"F",13,10]
        GOTO MAIN
              
    Negative:
        ' The first section handles everything from -00.1°C (31.82°F) to -17.7°C (0.14°F)
        ' which is still +°F.
        IF RESULTF < 3200 THEN
           HSEROUT [DEC (3200-RESULTF)/100,".",DEC2 (3200-RESULTF)//100,DEG,"F",13,10]
        ELSE
        ' The 2nd section handles everything from -17.8°C (-0.04°F) to -99.9°C (-147.82°F)
        ' Note how we reverse the subtraction here on -°F.
           HSEROUT ["-",DEC (RESULTF-3200)/100,".",DEC2 (RESULTF-3200)//100,DEG,"F",13,10]
        ENDIF
        GOTO MAIN
           
        END
    And if you don't have one of these nifty gadgets, but would like to run the conversion routines with a
    serial terminal emulator, here's a chopped version;
    Code:
        DEFINE LOADER_USED 1
        DEFINE OSC 4
        DEFINE  HSER_TXSTA 24H ' TX enabled, 8-bit, BRGH=1 for high-speed
        DEFINE  HSER_SPBRG 25  ' Set baud rate generator for 9600bps @4MHz
        DEFINE  HSER_RCSTA 90H ' USART enabled, 8-bit, continuous receive
        TEMP    VAR BYTE(5)    ' Array for simulated sensor temp input
        RESULTF VAR WORD       ' For °C to °F conversion
        DEG     CON 176        ' ASCII value for ° symbol
        SIGN    VAR TEMP(0)    ' TEMP(0) holds the sign indicator
        NEG     CON "-"        ' Negative °C indicator
                               ' Positive °C indicator = " " ASCII space
       
        HSEROUT ["Input H then +xx.x or -xx.x temp to convert",13,10]
        ' Enter H-19.7 for the conversion result of -19.7°C to °F
        
    Main:
        HSERIN [WAIT("H"),STR TEMP\5]
        HSEROUT [TEMP(0),TEMP(1),TEMP(2),TEMP(3),TEMP(4),DEG,"C = "]
        ' Temp input to convert starts at TEMP(1). TEMP(0) = sign indicator
        ' TEMP(3) is the decimal point
        RESULTF = ((TEMP(1)-48)*100)+((TEMP(2)-48)*10)+(TEMP(4)-48)
        GOSUB Test
        GOTO Main
    
    Test: 
        ' This handles everything from 00.0°C (32.0°F) to 99.9°C (211.82°F)
        RESULTF = RESULTF * 18  ' °F = (1.8*°C)+32
        IF Sign = NEG THEN Negative
        HSEROUT [DEC (RESULTF/100)+32,".",DEC2 RESULTF//100,DEG,"F",13,10]
        RETURN
              
    Negative:
        ' The first section handles everything from -00.1°C (31.82°F) to -17.7°C (0.14°F)
        IF RESULTF < 3200 THEN
           HSEROUT [DEC (3200-RESULTF)/100,".",DEC2 (3200-RESULTF)//100,DEG,"F",13,10]
        ELSE
        ' The 2nd section handles everything from -17.8°C (-0.04°F) to -99.9°C (-147.82°F)
           HSEROUT ["-",DEC (RESULTF-3200)/100,".",DEC2 (RESULTF-3200)//100,DEG,"F",13,10]
        ENDIF
        RETURN
        
        END
    Enter H+69.9 to view the °C to °F conversion for 69.9°C to °F.
    Enter H-69.9 to see the result of -69.9°C to °F.

    It should work for °C to °F conversion of values from 99.9°C to -99.9°C.
    Last edited by Bruce; - 17th March 2006 at 16:17.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

Similar Threads

  1. Reading in Manchester code
    By brid0030 in forum Code Examples
    Replies: 0
    Last Post: - 10th March 2009, 21:55
  2. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  3. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  4. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  5. Re-Writing IF-THEN-AND-ENDIF code?
    By jessey in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 18th August 2006, 17:23

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts