Sample Code

Windows Driver Samples/ WPD Basic Hardware Sample Driver (UMDF Version 1)/ C++/ firmware/ temp_humidity_wpd_enabled.bs2/

' Temp_humidity_wpd_enabled.BS2
' This program demonstrates the interface and conversion of SHT11/15 data
' to usable program values. This program uses advanced math features of
' PBASIC, specifically the ** operator.
'
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
' ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
' THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
' PARTICULAR PURPOSE.
'
' Copyright (c) Microsoft Corporation. All rights reserved
'
' {$STAMP BS2}
' {$PBASIC 2.5}

'
' =========================================================================
' -------------------------------------------------------------------------
' I/O Definitions
' -------------------------------------------------------------------------
ShtData PIN 1 ' bi-directional data
Clock PIN 0
' -------------------------------------------------------------------------
' Constants
' -------------------------------------------------------------------------
ShtTemp CON %00011 ' read temperature
ShtHumi CON %00101 ' read humidity
ShtStatW CON %00110 ' status register write
ShtStatR CON %00111 ' status register read
ShtReset CON %11110 ' soft reset
Ack CON 0
NoAck CON 1
No CON 0
Yes CON 1
DegSym CON 186 ' degrees symbol for DEBUG
' -------------------------------------------------------------------------
' Variables
' -------------------------------------------------------------------------
ioByte VAR Byte ' data from/to SHT11
ackBit VAR Bit ' ack/nak from/to SHT11
toDelay VAR Byte ' timeout delay timer
timeOut VAR Bit ' timeout status
soT VAR Word ' temp counts from SHT11
tC VAR Word ' temp - Celcius
tF VAR Word ' temp - Fahrenheit
soRH VAR Word ' humidity counts
rhLin VAR Word ' humidity; linearized
rhTrue VAR Word ' humidity; compensated
status VAR Byte ' status byte

'-------------------------------------------------------------------------
' WPD Variables
'-------------------------------------------------------------------------
SensorID  VAR   Byte  'Sensor identifier = 2 for Sensiron Temp/Humidity
ElementSize VAR Byte  'Size (in bytes) of each element
ElementCount  VAR   Byte  'Count of elements in packet
Padding VAR Byte      'Padding for the 8-byte element

SensorID = 2
ElementSize = 1
ElementCount = 7      '4-bytes for temp, 3-bytes for relative humidity

NewInterval VAR  Word  'New interval requested by user
Interval  VAR   Word  'Interval value utlized by firmware

LFD       CON $10      'Linefeed character

Interval = 2000
NewInterval = 2000

' -------------------------------------------------------------------------
' EEPROM Data
' -------------------------------------------------------------------------
' -------------------------------------------------------------------------
' Initialization
' -------------------------------------------------------------------------
Initialize:
GOSUB SHT_Connection_Reset ' reset device connection
' -------------------------------------------------------------------------
' Program Code
' -------------------------------------------------------------------------
Main:
  GOSUB SHT_Measure_Temp  'Retrieve temperature
  GOSUB SHT_Measure_Humidity   'Retrieve humidity
  GOSUB RetrieveInterval    'Retrieve interval data

SendData:
  SEROUT 16, 16468, [DEC1 SensorID, DEC1 ElementSize, DEC1 ElementCount, DEC4 (tF), DEC3 (rhTrue), DEC5 Interval, LFD]
 GOTO Main

RetrieveInterval:
   SERIN 16, 16468, Interval, SendData, [DEC NewInterval]   'Retrieve interval
   IF NewInterval >= 10 AND NewInterval <= 60000 THEN
     Interval = NewInterval
   ENDIF
RETURN

' -------------------------------------------------------------------------
' Subroutines
' -------------------------------------------------------------------------
' connection reset: 9 clock cyles with ShtData high, then start sequence
'
SHT_Connection_Reset:
SHIFTOUT ShtData, Clock, LSBFIRST, [$FFF\9]
' generates SHT11 "start" sequence
' _____ _____
' ShtData |_______|
' ___ ___
' Clock ___| |___| |___
'
SHT_Start:
INPUT ShtData ' let pull-up take high
LOW Clock
HIGH Clock
LOW ShtData
LOW Clock
HIGH Clock
INPUT ShtData
LOW Clock
RETURN
' measure temperature
' -- celcius = raw * 0.01 - 40
' -- fahrenheit = raw * 0.018 - 40
'
SHT_Measure_Temp:
GOSUB SHT_Start ' alert device
ioByte = ShtTemp ' temperature command
GOSUB SHT_Write_Byte ' send command
GOSUB SHT_Wait ' wait for measurement
ackBit = Ack ' another read follows
GOSUB SHT_Read_Byte ' get MSB
soT.HIGHBYTE = ioByte
ackBit = NoAck ' last read
GOSUB SHT_Read_Byte ' get LSB
soT.LOWBYTE = ioByte
' Note: Conversion factors are multiplied by 10 to return the
' temperature values in tenths of degrees
tC = soT ** $1999 - 400 ' convert to tenths C
tF = soT ** $2E14 - 400 ' convert to tenths F
RETURN
' measure humidity
'
SHT_Measure_Humidity:
GOSUB SHT_Start ' alert device
ioByte = ShtHumi ' humidity command
GOSUB SHT_Write_Byte ' send command
GOSUB SHT_Wait ' wait for measurement
ackBit = Ack ' another read follows
GOSUB SHT_Read_Byte ' get MSB
soRH.HIGHBYTE = ioByte
ackBit = NoAck ' last read
GOSUB SHT_Read_Byte ' get LSB
soRH.LOWBYTE = ioByte
' linearize humidity
' rhLin = (soRH * 0.0405) - (soRH^2 * 0.0000028) - 4
'
' for the BASIC Stamp:
' rhLin = (soRH * 0.0405) - (soRH * 0.002 * soRH * 0.0014) - 4
'
' Conversion factors are multiplied by 10 to return tenths
'
rhLin = (soRH ** $67AE) - (soRH ** $83 * soRH ** $5B) - 40
' temperature compensated humidity
' rhTrue = (tc - 25) * (soRH * 0.00008 + 0.01) + rhLin
'
' Conversion factors are multiplied by 10 to return tenths
' -- simplified
'
rhTrue = (tC - 250) * (soRH ** $34) + rhLin
RETURN
' sends "status"
'
SHT_Write_Status:
GOSUB SHT_Start ' alert device
ioByte = ShtStatW ' write to status reg cmd
GOSUB SHT_Write_Byte ' send command
ioByte = status
GOSUB SHT_Write_Byte
RETURN
' returns "status"
'
SHT_Read_Status:
GOSUB SHT_Start ' alert device
ioByte = ShtStatW ' write to status reg cmd
GOSUB SHT_Read_Byte ' send command
ackBit = NoAck ' only one byte to read
GOSUB SHT_Read_Byte
RETURN
' sends "ioByte"
' returns "ackBit"
'
SHT_Write_Byte:
SHIFTOUT ShtData, Clock, MSBFIRST, [ioByte] ' send byte
SHIFTIN ShtData, Clock, LSBPRE, [ackBit\1] ' get ack bit
RETURN
' returns "ioByte"
' sends "ackBit"
'
SHT_Read_Byte:
SHIFTIN ShtData, Clock, MSBPRE, [ioByte] ' get byte
SHIFTOUT ShtData, Clock, LSBFIRST, [ackBit\1] ' send ack bit
INPUT ShtData ' release data line
RETURN
' wait for device to finish measurement (pulls data line low)
' -- timeout after ~1/4 second
'
SHT_Wait:
INPUT ShtData ' data line is input
timeOut = No ' assume no timeout
FOR toDelay = 1 TO 250 ' wait ~1/4 second
IF (ShtData = 0) THEN EXIT
PAUSE 1
NEXT
IF (toDelay = 250) THEN timeOut = Yes ' loop completed = timeout
RETURN

' reset SHT11/15 with soft reset
'
SHT_Soft_Reset:
GOSUB SHT_Connection_Reset ' reset the connection
ioByte = ShtReset ' reset command
ackBit = NoAck ' only one byte to send
GOSUB SHT_Write_Byte ' send it
PAUSE 11 ' wait at least 11 ms
RETURN

Our Services

  • What our customers say about us?

© 2011-2024 All Rights Reserved. Joya Systems. 4425 South Mopac Building II Suite 101 Austin, TX 78735 Tel: 800-DEV-KERNEL

Privacy Policy. Terms of use. Valid XHTML & CSS