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




Bookmarks