Sensirion SHT11 with a PIC16F628-20


View Poll Results: Have I included enough relevant material to make the problem clear?

Voters
1. You may not vote on this poll
  • YES. I see what you mean

    0 0%
  • COULD BE BETTER: Kind of understand what your trying to say

    1 100.00%
  • NO: What the heck are you talking about

    0 0%
Closed Thread
Results 1 to 27 of 27
  1. #1
    gbm_designs's Avatar
    gbm_designs Guest

    Default Sensirion SHT11 with a PIC16F628-20

    Greetings-

    I 've recently moved from the BS2 to the PIC Basic Compiler Pro software.


    I'm trying to attach a Sensirion SHT11 Temperature and Humidity sensor to the Pic 16F628-20 and then send the Calculated values in Celcius and Farenheit, as well as Relative Humidity %RH to the COM1 port on my windows machine.

    I have found many sample source code, but none seem to work for me when I compile. Either written for a separate compiler, or a different chip, so the #Include statements are wrong.

    Can anyone help me along here? I would really appreciate it.

    If any other information is required, please let me know.


    Thank you,

    Christian
    EPIC PROGRAMMER
    PIC BASIC COMPILER PRO
    16F628-20
    20-MHz External Xtal

  2. #2
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    attached is a working piece of code for the 16F628

    Code:
    ___________________
    
    ' Temperature, Relative Humidity and Dewpoint
    ' Processor : 16F628
    ' Sensor    : SHT1x / SHT7x
    ' Display   : 16x2
    
    '** Pin-Assingments
    
    dta     VAR PORTB.1     ' Sensor Data
    clk     VAR PORTB.0     ' Sensor Clock
    
    '** Constants
    
    cmdtr     CON %00000011    ' Command "read temperature"
    cmdhr     CON %00000101    ' Command "read humitity"
    
    '** Variables
    
    result     VAR WORD    ' RAW Data from Sensor
    chksum     VAR BYTE     ' Checksum
    cmd     VAR WORD    ' Sensor Command
    RHlin     VAR WORD    ' Rel. Humidity (RH) Linear (% *10)
    RHtc    VAR WORD    ' Rel. Humidity (RH) temp. compensated (% *10)
    Temp     VAR WORD    ' Temperature (°C *100)
    DP     VAR WORD    ' Dewpiont (°C *10)
    
    
    '** Temp variables used in div. calculations
    
    TempDP  VAR WORD
    Rchk     VAR BYTE 
    logEW     VAR WORD
    sign     VAR BIT
    wy        VAR WORD
    wz        VAR WORD
    wj        VAR WORD
    wx      VAR WORD
    i     VAR BYTE
    ix     VAR BYTE
    bt     VAR BIT
    
    
    Pause 500
    LCDOut $FE,1,"**   SHT 11   **"
    PAUSE 1000
    
    main:
    
    GoSub init
    cmd = cmdtr
    
    GoSub readsensor
    Temp=result-3995
    
    GoSub init
    cmd = cmdhr
    
    GoSub readsensor
    RHlin=(26542-(54722**result+result))**result-40  ' RH linear
    RHtc=655+(result*5)+(result**15917)                 
    RHtc=(RHtc**(Temp/10+2480))-(RHtc**2730)+RHlin   ' RH temp. compensated
    
    GoSub dewpoint
    
    LCDOut $FE,$80," ",$DF,"C","   ",$25,"RH","    DP "
    LCDOut $FE,$C0,DEC2 (TEMP/100),".",DEC1 (temp/10),"  ",DEC2 RHTC/10,".",DEC1 RHTC,"  ",DEC2 ABS DP/10,".",DEC1 ABS dp
    sleep 10
    GoTo main
    
    '** Init Sensor
    init:                                             
    High dta 
    Low clk 
    For i=1 to 10 
    High clk
    Pause 1 
    Low clk
    Pause 1 
    Next i 
    Call tstart 
    Return 
    
    '** start transfer
    tstart:
    High clk
    Pause 1 
    Low dta 
    Pause 1 
    Low clk
    Pause 1 
    High clk
    Pause 1 
    High dta 
    Pause 1 
    Low clk
    Return 
    
    '** get Data from Sensor
    readsensor:
    GoSub tstart
    GoSub WaitSensor
    
    ShiftOut dta,clk,1,[cmd\8]                  ' send command
    Input dta                      ' wait acknowledge 
    Low clk
    While dta=1 
    Wend
    PulsOut clk,10                      ' send ack 
    While dta=0 
    Wend
    While dta=1                      ' wait for conversion to complete
    Wend 
    Low clk
    ShiftIn dta,clk,0,[result.highbyte\8]          ' get first byte
    Low dta 
    PulsOut clk,10                      ' send ack 
    ShiftIn dta,clk,0,[result.lowbyte\8]             ' get second byte
    Low dta 
    PulsOut clk,10                      ' send ack 
    ShiftIn dta,clk,0,[chksum\8]                     ' get third byte (checksum)
    High dta 
    PulsOut clk,10                      ' send ack 
    Input dta                      ' End of Transmission
    Input clk                          
    
    Return
    
    '** Dewpoint Calculation
        ' See: SENSIRION Application Note "Dewpoint Calculation"
        ' logEW = (0.66077+7.5*T/(237.3+T)+(log(RH)-2)
          ' DP = ((0.66077-logEW)*237.3)/(logEW-8.16077)
    
    DewPoint:
      
      TempDP=Temp/10
      logEW=6608
      sign=TempDP.bit15
      wz=ABS TempDP
      wx=(7*wz)+(wz/2)                              
      wy=2373+wz
      wj=wx/wy
       For ix=15 to 0 step -1
        wx=(wx//wy)<<1
        wz.bit0(ix)=wx/wy
       Next
      wx=(-sign^((wj*10000)+(wz**10000)))+sign        
      logEW=wx+logEW
      wx=RHtc
      wj=(NCD wx) - 1
      wx=wx<<(15-wj)
      wz=0
      For ix=14 to 0 step -1
        wy=wx**wx
        wz.bit0(ix)=wy.bit15
        bt=~wy.bit15
        wx=(wy<<bt)+(bt && wx.bit15)
       Next
      
      wx=((wj*4000)**49321)+(wz**6021)
      logEW=wx+logEW-30000            
      sign=logEW.bit15
      logEW=(-sign^(((ABS logEW)+2)/4))+sign
      wx=1652-logEW
      sign=~wx.bit15
      wx=ABS wx
      wy=20402-logEW
      wj=wx/wy
      
       For ix=15 to 0 step -1
        wx=(wx//wy)<<1
        wz.bit0(ix)=wx/wy
       Next
      
      DP=((23730**wz)+5)/10
      DP=(-sign^DP)+sign
    
    Return
    
    WaitSensor:
      result=4096
    Loop:
      result=result-1
       IF dta && result.bit11 Then Loop
      Return
    End
    ___________________

    Regards

    Ralph
    Last edited by ScaleRobotics; - 6th April 2011 at 16:24. Reason: added code tags

  3. #3
    gbm_designs's Avatar
    gbm_designs Guest


    Did you find this post helpful? Yes | No

    Question Compile Error with PBCP

    Greetings Ralph-

    Thank you for your help.

    PROBLEMS:

    PROBLEM1.
    I get a COMPILE ERROR in this Section of Code:

    DewPoint:

    For ix=14 to 0 step -1
    wy=wx**wx
    wz.bit0(ix)=wy.bit15
    bt=~wy.bit15
    wx=(wy< Next <<<<---------THIS IS THE PROBLEM


    QUESTION: Can you let me know what's missing here?

    ************************
    PROBLEM2:
    After COMMENTING out the ERROR LINE is PROB1, I set up the circuit as described below ((SEE CIRCUIT SPECS)) and use Hyperterminal to view the Output of the Program by COMMENTING out the LCD output in your code and replacing it with the following:

    SerOut2 PORTB.2,86,[DEC2 (TEMP/100),".",DEC1 (temp/10)," ",DEC2 (TEMP/100),".",DEC1 (temp/10)," ",DEC2 RHTC/10,".",DEC1 RHTC," ",DEC2 ABS DP/10,".",DEC1 ABS dp,13,10]
    'LCDOut $FE,$C0,DEC2 (TEMP/100),".",DEC1 (temp/10)," ",DEC2 RHTC/10,".",DEC1 RHTC," ",DEC2 ABS DP/10,".",DEC1 ABS dp


    WHAT HAPPENS: I get the SAME Values over and over again

    ** SHT 11 **
    12.8 00.4 12.3
    12.8 12.8 00.4 12.3

    QUESTION: ANY IDEAS?

    ************************

    CIRCUIT SPECS:

    PIC16F628-20
    PINOUTS
    4-----MCLR---4.7kohm to 5vdc
    5-----VSS-----GND
    6-----RB0-----Pin3 of SHT11 (SCK)
    7-----RB1-----Pin2 of SHT11 (Data)
    8-----RB2-----Pin10 of MAX232 (T2IN)
    15---OSC2---XTAL (4Mhz)
    16---OSC1---XTAL (4Mhz)


    SHT11
    PINOUTS
    1----GND----GND
    2----DATA---Pin7 of PIC (RB1)
    3----SCK --- Pin6 of PIC (RB0)
    4----VDD----5VDC


    THANKS,

    I hope this description is complete enough

    CHRISTIAN

  4. #4
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    Christian,

    initially I tried to post the example using " [ code ] ... [ /code ] "

    when I viewed my posting I had to realize the code was truncated at some point.

    I have edited my posting and the full code is now there as plain ASCII.

    please reload the code.

    regards

    Ralph

  5. #5
    gbm_designs's Avatar
    gbm_designs Guest


    Did you find this post helpful? Yes | No

    Question Code looks the same

    Greetings Ralph-

    It appears that the source code is the same.

    I did a quick line comparison and the same number of characters are present.

    Could you e-mail it to me?

    Thank you

    mailto: [email protected]


    Christian

  6. #6
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default Re: Code looks the same

    . . .Could you e-mail it to me?
    DONE!

  7. #7
    gbm_designs's Avatar
    gbm_designs Guest


    Did you find this post helpful? Yes | No

    Default Got the E-mail

    Greetings Ralph-

    Thank you for the E-mail.

    I used the new code with my changes for the Serial Port, rather than LCD.

    However I still get the same value over and over.

    ** SHT 11 **
    RH DP •
    12.8 12.8 00.4 14.9


    RH DP •
    12.8 12.8 00.4 14.9


    Kind of odd.


    I re-tested this SHT11 Device with my BS2 Program and hardware and it works fine.

    I have GOT to be doing something WRONG.



    Christian

  8. #8
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    Christian,

    I'll have a look at it tomorrow morning when I'm back at my desk.

    regards

    Ralph

  9. #9
    gbm_designs's Avatar
    gbm_designs Guest


    Did you find this post helpful? Yes | No

    Default THANKS!

    I'll keep banging away at it tonight also.



    I'll let you know what I find.


    Christian

  10. #10
    Original's Avatar
    Original Guest


    Did you find this post helpful? Yes | No

    Default

    Look at this:
    Code:
    '*******************************[ SHT11 Sample Code ]********************************
    ' File...... SHT11.bas
    ' Purpose... SHT11 digital humidity sensor sample code
    ' MCU....... Microchip PIC16F877
    ' Software.. Converted to Proton+ 2.0 from PBPro ver 2.4
    ' Author.... Brandon Tarr
    ' E-Mail.... [email protected]
    ' Web....... http://www.spectrumedix.com
    ' Started... May 15, 2002
    ' Updated... 
    ' Version... 1.0                                               
    '**************************************************************************
    
     device 16f877
    
    DEFINE xtal 4
    Define  LCD_DREG        PORTD
    Define  LCD_DBIT        4
    Define  LCD_RSREG       PORTE
    Define  LCD_RSBIT       0
    Define  LCD_EREG        PORTE
    Define  LCD_EBIT        1
    
    TEMP            con     1
    HUMI            con     2
                                            ' adr   command r/w
    Symbol statusregw      =     $06             ' 000   0011    0
    Symbol statusregr      =     $07             ' 000   0011    1
    Symbol measuretemp     =     $03             ' 000   0001    1
    Symbol measurehumi     =     $05             ' 000   0010    1
    Symbol resett           =     $1e             ' 000   1111    0
    
    ' -----[ Variables ]---------------------------------------------------------------------
    Dim SCK         as     PORTC.3             ' SHT11 SCK
    Dim SDATA       as     PORTC.4             ' SHT11 DATA
    
    Dim i           as     byte
    Dim f           as     byte
    Dim value       as     byte
    Dim errorr       as     bit
    Dim mode        as     byte
    Dim ack         as     bit
    
    Dim vall         as     byte
    Dim pktvalue    as     word
    Dim pktchksum   as     byte
    
    Dim humival     as     word
    Dim tempval     as     word
    Dim dewpoint    as     byte
    Dim checksum    as     byte
    
    ' -----[ Initialization ]----------------------------------------------------------------
    ADCON1 = 7              ' Set PORTA and PORTE to digital
    Low PORTE.2             ' LCD R/W line low (W)
    Pause 100               ' Wait for LCD to start
    lcdout $fe, 1
    
    ' -----[ Main Code ]---------------------------------------------------------------------
    usetheSHT11:
    gosub connectionreset
    value = measurehumi
    gosub measure                                        ' measure humidity
    humival = pktvalue * 1000
    humival = DIV32 4095
    lcdout $fe, 2, "                    ", $fe, 2, "Humidity: ", dec humival / 10, ".", dec humival // 10, "%"
    value = measuretemp
    gosub measure                                        ' measure temperature
    tempval = pktvalue * 1000
    tempval = DIV32 16383
    lcdout $fe, $c0, "                    ", $fe, $c0, "Temperature: ", dec tempval / 10, ".", dec tempval // 10, 223, "C"
    pause 100                                            ' decrease sample time
    goto usetheSHT11
    
    ' -----[ Subroutines ]-------------------------------------------------------------------
    connectionreset:            
    ' communication reset: DATA line is high and at least 9 SCK cycles followed by transstart
    '         ________________________________________       _____
    ' SDATA:                                          |_____|
    '           _   _   _   _   _   _   _   _   _     __    __
    '   SCK:  _| |_| |_| |_| |_| |_| |_| |_| |_| |___|  |__|  |___
    high SDATA
    low SCK                                 ' set initial state
    for i = 1 to 9                          ' 9 SCK cycles
        high SCK
        low SCK
    next
    gosub transstart                       ' transmission start
    return
    
    transstart:                             
    ' generates a transmission start 
    '         ___       _____
    ' SDATA:     |_____|
    '            __    __
    '   SCK:  __|  |__|  |___
    
    high SDATA
    low SCK                                 ' set initial state
    high SCK
    low SDATA
    low SCK
    high SCK
    high SDATA
    low SCK
    return
    
    measure:
    ' makes a measurement (humidity/temperature) with checksum
    errorr = 0
    gosub transstart                        ' transmission start
    gosub writebyte
    for i = 1 to 255                        ' wait until sensor has finished the measurement
        if SDATA = 0 then
            goto meascomplete
        endif
        pause 1
    next 
    meascomplete:
    if SDATA != 0 then                      ' or timeout (~2 sec.) is reached
        errorr = 1
    endif
    ack = 1
    gosub readbyte                          ' read the first byte (MSB)
    pktvalue.highbyte = vall
    gosub readbyte                          ' read the second byte (LSB)
    pktvalue.lowbyte = vall
    ack = 0
    gosub readbyte                          ' read checksum (no acknowledge to put sensor in "sleep" mode)
    pktchksum = vall
    return
    
    writebyte:
    ' writes a byte on the Sensibus and checks the acknowledge
    errorr = 0
    i = $80
    while i > 0
        if (i & value) != 0 then              ' masking value with i
            high SDATA                        ' write to SENSI-BUS
        else
            low SDATA
        endif
        high SCK                            ' clk for SENSI-BUS
        low SCK
        i = i / 2                           ' shift bit for masking
    wend
    high SDATA                              
    TRISC.4 = 1                             ' release SDATA-line
    high SCK                                ' clk #9 for ack
    errorr = SDATA                           ' check ack (SDATA will be pulled down by SHTXX)
    low SCK
    return
    
    readbyte:
    ' reads a byte from the Sensibus and gives an acknowledge in case of "ack=1"
    vall = 0
    high SDATA                              
    TRISC.4 = 1                             ' release SDATA-line
    i = $80
    while i > 0
        high SCK                            ' clk for SENSI-BUS
        if SDATA = 1 then                   ' read bit
            vall = vall | i
        endif
        low SCK
        i = i / 2
    wend
    TRISC.4 = 0                             ' grab SDATA-line
    SDATA = ack                            ' in case of "ack", pull down DATA-Line
    high SCK                                ' clk #9 for ack
    low SCK
    high SDATA                              
    TRISC.4 = 1                             ' release SDATA-line
    return
    
    softreset:
    ' resets the sensor by a softreset
    errorr = 0
    GOSUB connectionreset                   ' reset communication
    value = resett
    gosub writebyte                         ' send RESET-command to sensor
    return
    Last edited by ScaleRobotics; - 6th April 2011 at 16:26. Reason: added code tags

  11. #11
    Join Date
    Nov 2004
    Location
    malawi
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Need help also

    hi! iam also working SHT11 project with PIC16F877 display with LCD, itry Ralph source code but, only **SHT 11** display no sensor reading ,can someone email source code to me? [email protected]
    thanks

  12. #12
    Join Date
    Aug 2005
    Location
    Ontario, Canada
    Posts
    5


    Did you find this post helpful? Yes | No

    Thumbs up Missing part of code SHT11

    For anyone trying to get this first off, add wx=wy<< bt+(bt & wx.bit15)
    instead of the truncated part Christian had indicated.
    (nice work on the code Ralph)
    This works fine on a PIC16F876A 20mhz using portc.3 and portc.4
    I can see why Ralph had problems with it not posting properly. I had to
    put in a space before the first bt to see it properly as without the space
    it was truncating my reply too. Anyone else have a problem posting code
    like this?
    (helps to look at the preview)
    Hope this helps someone.
    Ron

  13. #13
    Join Date
    Mar 2007
    Posts
    5


    Did you find this post helpful? Yes | No

    Unhappy Please help me ??????

    I have been trying to get this code to work for the last week,A friend and i have both recived samples and have been unable to get the project going after overcoming the first compile error it seems that the device wont respond and it just hangs in the readsensor routine, Could yoiu please post the working picbasic pro or email it to me at [email protected].
    it seems that if i force data line to go low by shorting to ground the code execution continues and outputs repative numbers.
    There is a misplaced next could you please clarafy where this is ment to go

    regards Evolution

  14. #14
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,611


    Did you find this post helpful? Yes | No

    Angry Sometimes I feel tired ... but why ????

    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  15. #15
    Join Date
    Mar 2007
    Posts
    5


    Did you find this post helpful? Yes | No

    Angry Gee Thanx

    Why bother running the forum im sure all of us have no worries translating the cryptic data presented in these sheets and great bs2 code????? pbp

  16. #16
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,611


    Did you find this post helpful? Yes | No

    Question

    Quote Originally Posted by Evolution View Post
    Why bother running the forum im sure all of us have no worries translating the cryptic data presented in these sheets and great bs2 code????? pbp
    Hi,

    Sorry ... but could you be more explicit ... difficulties to fully understand your answer.

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  17. #17
    Join Date
    Mar 2007
    Posts
    5


    Did you find this post helpful? Yes | No

    Red face The SHT11 Issue

    First of all i had the code error where the example was missing (wx=(wy< Next <<<<---------) after replacing this with the example above it seems that code execution halts on waiting for initalisation command(data pulled low) and nothing will happen untill i manualy ground the pin temproraly oncei do this it outputs repeating numbers in an unordly fashion
    i am using picbasic pro 2.44 but can also code in assembly the selected mcu is a pic16f628 a running at 4mhz internal osc
    Would greatly appreciate any help you could provide i had a look at the bs2 code you pointed out and will try to convert it to picbasic

  18. #18


    Did you find this post helpful? Yes | No

    Unhappy sht11 vs. 16f84A

    I'm using the above code with a 16f84a and getting a consistent number, which is clearly wrong. I'm getting humvalue 16003 and tempval 4000. And since I haven't burst into flames this seems unlikely to be accurate.

    I also tried using a modified Keunyoung Oh's code for the 16f877 (found http://itp.nyu.edu/physcomp/sensors/Code/SHT11 ) but received similar results (consistent & wildly inaccurate numbers), though with different numbers.

    Any idea why this might be?

    Also, as a possible explanation: I'm using the DIP sht11 which has a 117 on the sensor. I'm assuming I should use the pin diagram which goes with sensirion sht11 sensor module #28018, but all the other pin diagrams are different.

    any suggestions would be helpful.

    thanks,
    Adam

  19. #19


    Did you find this post helpful? Yes | No

    Post sht11 v 16f84a follow up

    here's the full code I'm currently (trying to use) everything compiles and publishes in PBP, but still no good data. is my code messed up?
    Code:
    '*******************************[ SHT11 Sample Code ]********************************
    ' File...... SHT11.bas
    ' Purpose... SHT11 digital humidity sensor sample code
    ' MCU....... Microchip PIC16F877
    ' Software.. Converted to Proton+ 2.0 from PBPro ver 2.4
    ' Author.... Brandon Tarr
    ' E-Mail.... [email protected]
    ' Web....... http://www.spectrumedix.com
    ' Started... May 15, 2002
    ' Updated...
    ' Version... 1.0
    '************************************************* *************************
    
    'device 16f884a
    
    DEFINE xtal 4
    Define LCD_DREG PORTD
    Define LCD_DBIT 4
    Define LCD_RSREG PORTE
    Define LCD_RSBIT 0
    Define LCD_EREG PORTE
    Define LCD_EBIT 1
    
    TEMP con 1
    HUMI con 2
    ' adr command r/w
    Symbol statusregw = $06 ' 000 0011 0
    Symbol statusregr = $07 ' 000 0011 1
    Symbol measuretemp = $03 ' 000 0001 1
    Symbol measurehumi = $05 ' 000 0010 1
    Symbol resett = $1e ' 000 1111 0
    
    ' -----[ Variables ]---------------------------------------------------------------------
    ledPin             var     PORTB.0
    ledVar var byte
    
    rxPin              var     PORTB.3
    txPin              var     PORTB.2
    SCK var PORTA.4 ' SHT11 SCK
    SDATA var PORTB.1 ' SHT11 DATA
    
    i  var byte
    f var byte
    value var byte
    errorr var bit
    mode var byte
    ack var bit
    
    vall var byte
    pktvalue var word
    pktchksum var byte
    
    humival var word
    tempval var word
    dewpoint var byte
    checksum var byte
    
    ' -----[ Initialization ]----------------------------------------------------------------
    'ADCON1 = 7 ' Set PORTA and PORTE to digital
    'Low PORTE.2 ' LCD R/W line low (W)
    Pause 500 ' Wait for LCD to start
    'lcdout $fe, 1
    
    ' -----[ Main Code ]---------------------------------------------------------------------
    usetheSHT11:
        SerOut2 txPin, 16468, [" ", 10, 13]
        High ledPin          ' Turn on LED connected to PORTB.0
        Pause 500       ' Delay for .5 seconds
        Low ledPin           ' Turn off LED connected to PORTB.0
    
        gosub connectionreset
        value = measurehumi
        gosub measure ' measure humidity
        humival = pktvalue * 1000
        humival = DIV32 4095
        'lcdout $fe, 2, " ", $fe, 2, "Humidity: ", dec humival / 10, ".", dec humival // 10, "%"
        value = measuretemp
        gosub measure ' measure temperature
        tempval = pktvalue * 1000
        tempval = DIV32 16383
        'lcdout $fe, $c0, " ", $fe, $c0, "Temperature: ", dec tempval / 10, ".", dec tempval // 10, 223, "C"
        SerOut2 txPin, 16468, ["humival  ", DEC humival, 10, 13]
        SerOut2 txPin, 16468, ["tempval  ", DEC tempval, 10, 13]
    
        pause 500 '100 decrease sample time
    goto usetheSHT11
    
    ' -----[ Subroutines ]-------------------------------------------------------------------
    connectionreset:
    ' communication reset: DATA line is high and at least 9 SCK cycles followed by transstart
    ' ________________________________________ _____
    ' SDATA: |_____|
    ' _ _ _ _ _ _ _ _ _ __ __
    ' SCK: _| |_| |_| |_| |_| |_| |_| |_| |_| |___| |__| |___
        input SDATA
        high SDATA
        low SCK ' set initial state
        for i = 1 to 9 ' 9 SCK cycles
        high SCK
        low SCK
        next
        gosub transstart ' transmission start
    return
    
    transstart:
    ' generates a transmission start
    ' ___ _____
    ' SDATA: |_____|
    ' __ __
    ' SCK: __| |__| |___
    
        high SDATA
        low SCK ' set initial state
        high SCK
        low SDATA
        low SCK
        high SCK
        high SDATA
        low SCK
    return
    
    measure:
    ' makes a measurement (humidity/temperature) with checksum
        errorr = 0
        gosub transstart ' transmission start
        gosub writebyte
        for i = 1 to 255 ' wait until sensor has finished the measurement
            if SDATA = 0 then
                goto meascomplete
        endif
        pause 1
        next
    meascomplete:
        if SDATA != 0 then ' or timeout (~2 sec.) is reached
            errorr = 1
        endif
        ack = 1
        gosub readbyte ' read the first byte (MSB)
        pktvalue.highbyte = vall
        gosub readbyte ' read the second byte (LSB)
        pktvalue.lowbyte = vall
        ack = 0
        gosub readbyte ' read checksum (no acknowledge to put sensor in "sleep" mode)
        pktchksum = vall
    return
    
    writebyte:
        ' writes a byte on the Sensibus and checks the acknowledge
        errorr = 0
        i = $80
        while i > 0
            if (i & value) != 0 then ' masking value with i
                high SDATA ' write to SENSI-BUS
            else
                low SDATA
            endif
            high SCK ' clk for SENSI-BUS
            low SCK
            i = i / 2 ' shift bit for masking
        wend
        high SDATA
        TRISB.1 = 1 ' release SDATA-line
        high SCK ' clk #9 for ack
        errorr = SDATA ' check ack (SDATA will be pulled down by SHTXX)
        low SCK
    return
    
    readbyte:
    ' reads a byte from the Sensibus and gives an acknowledge in case of "ack=1"
        vall = 0
        high SDATA
        TRISB.1 = 1 ' release SDATA-line
        i = $80
        while i > 0
            high SCK ' clk for SENSI-BUS
            if SDATA = 1 then ' read bit
                vall = vall | i
            endif
            low SCK
            i = i / 2
        wend
        TRISB.1 = 0 ' grab SDATA-line
        SDATA = ack ' in case of "ack", pull down DATA-Line
        high SCK ' clk #9 for ack
        low SCK
        high SDATA
        TRISB.1 = 1 ' release SDATA-line
    return
    
    softreset:
    ' resets the sensor by a softreset
        errorr = 0
        GOSUB connectionreset ' reset communication
        value = resett
        gosub writebyte ' send RESET-command to sensor
    return
    Last edited by ScaleRobotics; - 6th April 2011 at 16:23. Reason: added code brackets for code tags

  20. #20
    Join Date
    Jan 2008
    Location
    Dublin, Ireland
    Posts
    33


    Did you find this post helpful? Yes | No

    Default

    Hmmmm. This was a valid interesting topic.... pity it ends in the middle of nowhere without a solution!

  21. #21
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by tonyfelloni View Post
    Hmmmm. This was a valid interesting topic.... pity it ends in the middle of nowhere without a solution!
    I think there are a couple of other working examples somewhere here on the forums.
    And heck, if the original poster doesn't care if the problem is solved, why should anyone else?

  22. #22
    Join Date
    Jan 2008
    Location
    Dublin, Ireland
    Posts
    33


    Did you find this post helpful? Yes | No

    Default

    Hey Skimask

    I care because its relevant to something I am working on now.... To find the perfect thread....only to discover you have been led up the garden path, haha. Not to worry, I did see a few other relevant posts elsewhere.

    Cheers
    Tony

  23. #23
    Join Date
    Aug 2005
    Posts
    4


    Did you find this post helpful? Yes | No

    Default addition to NavMicroSystems example

    Hi!

    I know this is an old thread but this might still help someone.

    NavMicroSystems' example works fine, if you place wx=wy<< bt+(bt & wx.bit15)
    instead of wx=(wy< Next.

    It has some issues with negative temperatures but ok...

    It's important that you don't forget 10k pullup on DTA line.

    There's one more thing: you should replace all HIGH dta
    commands with INPUT dta.

    Datahseet SHT1x @ page 5.

    "To avoid signall contention the microcontroller must only
    drive DATA low".

    When you have appropriate (10k) pullup on dta the line
    goes high if you set the dta pin to input. I tested it and
    it works perfectly fine.

    Also: if this is your first attempt to communicate with SHT1x maybe
    you should consider omitting dewpoint calculations and just read
    raw data.

    For room ambient (i had about 22°C and 35 %RH when testing) you should
    get readings like 0x1855 for 14bit temperature and 0x03EE for 12bit humidity).

    Best regards,

    Rok
    Last edited by psenek; - 26th January 2011 at 11:47.

  24. #24
    Join Date
    Apr 2011
    Posts
    2


    Did you find this post helpful? Yes | No

    Talking Hepl me please............

    I want to program SHT11, with language PBP or Pbasic
    want to know the temperature and humidity with SHT11.
    please help me ....

  25. #25
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: Sensirion SHT11 with a PIC16F628-20

    I have successfully used the code below with an 18F2321.
    It should work the same with a 16F part.

    I found it somewhere here on the forum, and it works great.


    Code:
    '------------------ Read SHT71 Temperature / Humidity Sensor -------------------
            
    ReadSHT71:
        SHTCommand = ReadTemp
        gosub Initialize
        GOSUB TransmissionStart
        GOSUB IssueCMD
        gosub ConvertToCelsius
        
        
        SHTCommand = ReadHumidity
        gosub Initialize
        GOSUB TransmissionStart
        GOSUB IssueCmd
        gosub ConvertToRH
    
        TEMPC = (TempC/100) - 2 
        TempC.7 = SignC      
        RHTempComp = RHTempComp/100
        RETURN
       
        
    
    '--------------------- Calculate Relative Humidity -----------------------------
    
    ConvertToRH:
    ' 12 Bit    RHLinear = -4 + 0.0405 * DataOutput -0.0000028 * DataOutput^2
        w = 405 * RawData
        w = div32 100
        x = RawData * RawData
        x = div32 1000
        x = x * 28
        x = div32 100
        RHLinear = w - x - 400
    
    '---------- Calculate Relative Humidity with Temperature Compensation ----------
    
        w = (8 * RawData + 1000) / 10
          IF SignC = 0 THEN      ' "+"  
            if tempC > 2500 then
                x = (tempC - 2500) * w
                x = div32 100
                RHTempComp = RHLinear + x / 100
                else
                x = (2500 - tempc) * w
                x = div32 100
                RHTempComp = RHLinear - x / 100
            endif
            else
            x = (2500 + tempC) * w
            x = div32 10000
            RHTempComp = RHLinear - x
        endif
        return
    
    '---------------------------- Initialize the Sensor ----------------------------
    
    Initialize:
        high DataPin                   ; Start condition
        low clk
        for i = 1 to 10
            high clk
            pause 1
            low clk
            pause 1
        next i                          ; Leaves clock low
    
        return
    
    '---------------------------- Get Data from Sensor -----------------------------
    
    IssueCmd:
        Pause 2
        low Clk
        shiftout DataPin,clk,1,[SHTCommand\8]       ' Send command byte
        input DataPin                                                            
    
    WaitForAck:
         x = 0
    WaitForAck2:
        IF DataPin = 1 THEN
          x = x + 1
          if x = 0 then goto NoAck
          Pauseus 2
          GotoWaitForAck2
        ENDIF  
          
        High Clk
        pause 10                                    ' Issue ACK from PIC
        low clk
        
        pause 10                                    ' Make certain we don't move forward on a glitch
    
        WHILE DataPin:WEND
                                         
       
        shiftin DataPin,clk,0,[RawData.byte1\8]     ' Get the first byte, 8 bits
        low DataPin
        
        HIGH Clk
        PAUSE 10
        LOW Clk
        pulsout clk,10                              ' Send NACK
        shiftin DataPin,clk,0,[RawData.byte0\8]     ' Get the second byte, 8 bits
    
    
        low DataPin
        pulsout clk,10                              ' Send acknowledge
        shiftin DataPin,clk,0,[crc\8]               ' Get third byte, 8 bits, CRC
        high DataPin
        pulsout clk,10                              ' Send acknowledge
        input DataPin                               ' End of Transmission
        input clk
    
        ERR = 0
        return
    
    NoAck:
        ERR = 1
        RETURN
    
    '---------------------------- Start Transfer -----------------------------------
    
    TransmissionStart:
        HIGH DataPin      ; Make sure it is high
        Pause 1
        high clk
        pause 1
        low DataPin
        pause 1
        low clk
        pause 1
        high clk
        pause 1
        high DataPin
        pause 1
        low clk
        return
    
    '----------------------------- Raw Data to Degrees C ---------------------------
    
    
    ConvertToCelsius:
    ' 14 Bit    Temperature = -40.00 + 0.01 * DataOutout
        if RawData => 4000 then
            tempC = RawData - 4000
            SignC = 0
                   
            else
            tempC = 4000 - RawData         ; Negative Temps
            SignC = 1
    
        endif
        return
    Charles Linquist

  26. #26
    Join Date
    Apr 2011
    Posts
    2


    Did you find this post helpful? Yes | No

    Default Re: Sensirion SHT11 with a PIC16F628-20

    what software you use as a compiler?
    pardon my beginner programing and
    program how to display the sensor readings of temperature and humidity SHT11 to 16bit and two-line LCD ....
    thanks very much
    Last edited by yohanfx; - 11th April 2011 at 03:54.

  27. #27
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326


    Did you find this post helpful? Yes | No

    Default Re: addition to NavMicroSystems example

    Hi ,
    I am going to implemant the dew point computation with this sensor and with a PIC baděsic Pro.
    I agree that there is an errot in DP calculation when the temperature is under 0°C.
    Any help to solve the problem? Or any updated code ?
    Thanks for helping.
    Regards,
    Ambrogio



    Quote Originally Posted by psenek View Post
    Hi!

    I know this is an old thread but this might still help someone.

    NavMicroSystems' example works fine, if you place wx=wy<< bt+(bt & wx.bit15)
    instead of wx=(wy< Next.

    It has some issues with negative temperatures but ok...

    It's important that you don't forget 10k pullup on DTA line.

    There's one more thing: you should replace all HIGH dta
    commands with INPUT dta.

    Datahseet SHT1x @ page 5.

    "To avoid signall contention the microcontroller must only
    drive DATA low".

    When you have appropriate (10k) pullup on dta the line
    goes high if you set the dta pin to input. I tested it and
    it works perfectly fine.

    Also: if this is your first attempt to communicate with SHT1x maybe
    you should consider omitting dewpoint calculations and just read
    raw data.

    For room ambient (i had about 22°C and 35 %RH when testing) you should
    get readings like 0x1855 for 14bit temperature and 0x03EE for 12bit humidity).

    Best regards,

    Rok

Similar Threads

  1. Watchdog Timers
    By Squibcakes in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 27th August 2014, 19:03
  2. INT2 anomaly in DT_INTS-18??
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 17th February 2010, 21:07
  3. capture/repeat data ?
    By Sam in forum Serial
    Replies: 44
    Last Post: - 27th November 2006, 04:19
  4. A little DTMF help
    By Travin77 in forum mel PIC BASIC Pro
    Replies: 48
    Last Post: - 30th May 2006, 02:31
  5. Serout PIC16F628 to PC R18iXL Board
    By Spindle in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 19th June 2005, 01:29

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