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




Bookmarks