Shop OBEX P1 Docs P2 Docs Learn Events
Propeller and smt 16030 digital temp sensor - very basics — Parallax Forums

Propeller and smt 16030 digital temp sensor - very basics

dubbel_jdubbel_j Posts: 2
edited 2015-05-12 09:20 in Propeller 1
Hi

I just managed to get a Propeller1 and Smt16030 digital temperature sensor. I don't have previous experience about Propeller or microcontrollers or that temp sensor.
Could somebody point me to the correct direction for reading a temperature using that temp sensor and Propeller. I have looked some datasheets and currently I just need to print the temperature using Parallax Seriel Terminal.

Also I just blinked some leds using Propeller :).

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-05-11 11:26
    You caught me in a procrastinating mood.

    Here's what I came up with:
    DAT programName         byte "Smt16030", 0
    CON
    { 
      This program might show how to use read from a SMT16030 sensor.
      By Duane Degn
      11 May, 2015
      
    }  
    CON
    
      _clkmode = xtal1 + pll16x                           
      _xinfreq = 5_000_000
    
      CLK_FREQ = ((_clkmode - xtal1) >> 6) * _xinfreq
      MS_001   = CLK_FREQ / 1_000
      US_001   = CLK_FREQ / 1_000_000
    
      SCALED_MULTIPLIER = 10_000
      SCALED_DECIMAL_PLACES = 4
      SCALED_CONVERSION_DENOMINATOR = 47  ' = .0047 * 10_000
      SCALED_CONVERSION_SUBTRACTION = 3200  ' = .32 * 10_000
     
      SMT16030_PIN = 16  ' Connect to sensor with at least a 3K ohm resistor. 10K ohm should work fine.
      
    VAR
    
      long scaledTemperature ' make this global so it can be used from other methods
      
    OBJ
    
      Pst : "Parallax Serial Terminal"
      Format : "StrFmt"
       
    PUB Setup
    
      Pst.Start(115_200)
     
      repeat
        result := Pst.RxCount
        Pst.Str(string(11, 13, "Press any key to start program."))
        waitcnt(clkfreq / 2 + cnt)
      until result
      
      Pst.RxFlush
      Pst.Clear
      
      TestSensor 
    
    PUB TestSensor | startLow, startHigh, endTime, timeHigh, timeTotal, scaledDutyCycle
     
      repeat 
        waitpeq(|< SMT16030_PIN, |< SMT16030_PIN, 0) ' wait for pin to go high
        waitpeq(0, |< SMT16030_PIN, 0) ' wait for pin to go low
        startLow := cnt      ' record start of low time
        waitpeq(|< SMT16030_PIN, |< SMT16030_PIN, 0) ' wait for pin to go high
        startHigh := cnt     ' record start of high time 
        waitpeq(0, |< SMT16030_PIN, 0) ' wait for pin to go low again
        endTime := cnt      ' record start of low time
        timeTotal := endTime - startLow
        timeHigh := endTime - startHigh
    
        scaledDutyCycle := (timeHigh * SCALED_MULTIPLIER) / timeTotal
        scaledTemperature := ((scaledDutyCycle - SCALED_CONVERSION_SUBTRACTION) * SCALED_MULTIPLIER) / {
        } SCALED_CONVERSION_DENOMINATOR
        
        Pst.Home
        Pst.Str(string("    SMT16030 Temperture Sensor Demo", 11, 13)) 
        Pst.Str(string(11, 13, "timeTotal = "))
        ' 11 clears the end of the previous line, 13 moves to a new line
        
        Pst.Dec(timeTotal)
        Pst.Str(string(" clocks or "))  
        Pst.Dec(timeTotal / US_001)
        Pst.Str(string(" us"))  
        Pst.Str(string(11, 13, "timeHigh = ")) 
        Pst.Dec(timeHigh)
        Pst.Str(string(" clocks or "))  
        Pst.Dec(timeHigh / US_001)
        Pst.Str(string(" us"))  
        Pst.Str(string(11, 13, "duty cycle (out of 1.0) = "))
        DecPoint(scaledDutyCycle, SCALED_DECIMAL_PLACES)
        Pst.Str(string(11, 13, "temperature = "))
        DecPoint(scaledTemperature, SCALED_DECIMAL_PLACES)
        Pst.Str(string(" C"))  
        
    PUB DecPoint(value, decimalPlaces) | localBuffer[4]
    
      result := Format.FDec(@localBuffer, value, decimalPlaces + 3, decimalPlaces)
      byte[result] := 0
      Pst.str(@localBuffer)
    

    It compiles but it's untested. I don't have a sensor to test it with.

    Make sure and take a look at page 222 of the Propeller Manual to see how to use waitpeq. I think I used it correctly but I don't use waitpeq in Spin often.

    I used scaled integer math. I think all the values stay safely below the 32-bit overflow but I could be wrong.

    If you try the code, I hope you post the output. When posting output, it helps if you use code tags. Click "Reply With Quote" to see how I used code tags with the above code, and use the same tags when posting code or output from the PST.

    The code could be greatly improved by taking multiple readings and averaging the readings.

    Edit: I just noticed I left some comments from a previous project in the program. I deleted these comments from the forum code but the attached code still has these extra comments.
  • dubbel_jdubbel_j Posts: 2
    edited 2015-05-12 09:20
    Thanks!

    This is way more than I expected to get. I think I have time to test this on next weekend. I will let you know is this working or not!
Sign In or Register to comment.