PDA

View Full Version : Device Compatibility: Sensirion Parallax sensor



embdfreak
- 5th April 2006, 10:52
Hi all,
Im new to this forum and dont have much experience with PBP. Im working on a Sensirion SHT11 Parallax Sensor for temperature/Humidity acq. The problem is, the code gives perfect results when ran on PIC16f84a but gives weird readings if same code is run on a PIC16f877a.
Following is the code. Please give suggestion to get over this problem.
Ive modified the code actually presented with the Application note.




INCLUDE "modedefs.bas"

DEFINE OSC 4
' ------------------------------------------------------------------------------
' I/O Definitions
' -------------------------------------------------------------------------

ShtData VAR PORTd.0
Clock VAR PORTd.1
' ------------------------------------------------------------------------------
' 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 (wait 11 ms after)

Ack CON 0
NoAck CON 1

No CON 0
Yes CON 1

MoveTo CON 2 ' for DEBUG control
ClrRt CON 11 ' clear DEBUG line to right

DegSym CON 186 ' degrees symbol for DEBUG

' ------------------------------------------------------------------------------
' Variables
' ------------------------------------------------------------------------------

ioByte VAR Byte ' data from/to SHT1x
ackBit VAR Bit ' ack/nak from/to SHT1x
toDelay VAR Byte ' timeout delay timer
timeOut VAR Bit ' timeout status

soT VAR Word ' temp counts from SHT1x
tC VAR Word ' temp - celcius
tF VAR Word ' temp - fahrenheit

soRH VAR Word ' humidity counts from SHT1x
rhLin VAR Word ' humidity; linearized
rhTrue VAR Word ' humidity; temp compensated

PWMvalue VAR WORD


' -------------------------------------------------------------------------'Initialization
' -------------------------------------------------------------------------

Initialize:
GOSUB SHT_Connection_Reset ' reset device connection
PAUSE 250 ' let DEBUG window open

GOTO Main ' skip heater demo

' ------------------------------------------------------------------------------
' Program Code
' ------------------------------------------------------------------------------

Main:
SEROUT2 PORTA.0,16572,["SHT11 DEMO",13,10] ' 16572 = value for 4800bps

Main2:
GOSUB SHT_Measure_Temp
SEROUT2 PORTA.0,16572,["Temp(C)=",DEC tc/10,".",DEC tC//10," "]
GOSUB SHT_Measure_Humidity
SEROUT2 PORTA.0,16572,["Hum(Lin)=",DEC rhLin/10,"%"," "]

rhTrue=rhTrue/10
SEROUT2 PORTA.0,16572,["Hum(True)=",DEC rhTrue,"%",10,13]

GOTO Main2

END


' ------------------------------------------------------------------------------
' Subroutines
' ------------------------------------------------------------------------------

' connection reset: 9 clock cyles with ShtData high, then start sequence
'
SHT_Connection_Reset:
SHIFTOUT ShtData, Clock, LSBFirst, [$FFF\9]

' generates SHT1x "start" sequence
' _____ _____
' ShtData |_______|
' ___ ___
' Clock ___| |___| |___
'
SHT_Start:
INPUT ShtData ' let pull-up take line high
LOW Clock
HIGH Clock
LOW ShtData
LOW Clock
HIGH Clock
INPUT ShtData
LOW Clock
RETURN


' measure temperature
' -- celcius = soT * 0.01 - 40
' -- fahrenheit = soT * 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 until measurement done
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 / 10 - 400 ' convert to tenths C
SEROUT2 PORTA.0,16572,["Temp Form(C)=",DEC tc,10,13]
tC = soT **$1999 - 400

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 until measurement done
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


rhLin= (soRH ** $67AE) - (soRH ** $83 * soRH ** $5B) - 40
rhTrue = (tC-250) * (soRH ** $34) + rhLin

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



SHT_Wait:
INPUT Shtdata
timeout = NO
For toDelay = 1 TO 250
if (ShtData = 0) THEN RETURN
PAUSE 1
NEXT
IF ( toDelay = 250 ) THEN timeout = Yes
RETURN

Acetronics2
- 5th April 2006, 12:03
Hi, freak

You just have to configure the extra registers in your 16F87x ... i.e. choose for digital inputs instead of analog, etc. etc.

Everything in the chip Databook.

Alain

embdfreak
- 5th April 2006, 16:06
well, i dont see anything like analog being used here. or rather i havent understood your answer properly. Can u please explain a bit further on. I truly dont know what to set cuz i have only two lines (data & clock ) to communicate with the sensor and nothing analog. plz have a look at the code and reply

sougata
- 5th April 2006, 16:56
Hi,

PICs which feature ADC have multiplexed inputs. That is either they go to the ADC or the digital I/O. By default (POR) the inputs are analogue. There are registers to set this according to your app. For your PIC it is the ADCON1

ADCON1 = %xxxx0110 ' where the x is your own setting turns of the ADC inputs and configures all as Digital I/O.

Read page 129-130 of your datasheet.

Acetronics2
- 5th April 2006, 18:15
Hi, SOUGATA

My inputs work well ... ( set to 7F ... as datasheet tells )

only the EEPROM is running bad.

Alain