; #Picaxe 20M2 #Terminal 4800 ; SHT31 Connection Diagram ; ; V+ ----------.----.---.----.-------------- V+ ; | .|. .|. | ; PICAXE-20M2 | | | | | | SHT31 ; .---------. | |_| |_| | .-------. ; | V+ |--' | | `---| VIN | ; | SDA B.5 |-------^---|--------| SDA | ; | B.6 |-----------|--------| RST | ; | SCL B.7 |-----------^--------| SCL | ; | 0V |--. 2 x 4K7 .---| ADR | ; `---------' | Internal | -| ALR | ; | }---| GND | ; | | `-------' ; 0V ----------^-------------^-------------- 0V Symbol SDA = B.5 ; I2C SDA Symbol SCL = B.7 ; I2C SCK Symbol SHT_RST = B.6 ; SHT31 RST ; .-----------------------------------------------------------------------------. ; | Constants | ; `-----------------------------------------------------------------------------' Symbol SHT31_DEVICE_ADDR = $88 ; $88 or $8A ; .-----------------------------------------------------------------------------. ; | Variables | ; `-----------------------------------------------------------------------------' Symbol reserveW0 = w0 ; b1:b0 Symbol reserveW1 = w1 ; b3:b2 Symbol temperature = w2 ; b5:b4 Symbol temperature.lsb = b4 Symbol temperature.msb = b5 Symbol humidity = w3 ; b7:b6 Symbol humidity.lsb = b6 Symbol humidity.msb = b7 Symbol temperature.crc = b8 Symbol humidity.crc = b9 Symbol idx = b10 Symbol crc = b11 Symbol polynomial = b12 ; .-----------------------------------------------------------------------------. ; | Macros | ; `-----------------------------------------------------------------------------' #Define Negative(wvar) wvar >= $8000 #Define Positive(wvar) wvar < $8000 #Define Negate(wexpr) wexpr ^ $FFFF + 1 #Macro Display(text,wexpr,char) w1 = wexpr If Negative(w1) Then w1 = -w1 SerTxd( text, TAB, "-" ) Else SerTxd( text, TAB ) End If b0 = w1 / 10 // 10 b1 = w1 // 10 w1 = w1 / 100 SerTxd( #w1, ".", #b0, #b1, " ", char, CR, LF ) #EndMacro ; .-----------------------------------------------------------------------------. ; | Main Program | ; `-----------------------------------------------------------------------------' PowerOnReset: Gosub Sht31_Initialise MainLoop: Do Gosub Sht31_Read Display( "Temperature", temperature, "C" ) Display( "Humidity", humidity, "%" ) SerTxd( CR, LF ) Pause 2000 Loop ; .-----------------------------------------------------------------------------. ; | SHT31 Routines | ; `-----------------------------------------------------------------------------' Sht31_Initialise: ; I2C Interface Reset HI2cSetup OFF ; Force SHT31 reset Pause 100 Low SHT_RST Pause 50 High SHT_RST ; Shift out any locked bits (probably not required) High SDA Low SCL For b0 = 0 To 9 PulsOut SCL,1 Next Input SCL Input SDA HI2cSetup I2CMASTER, SHT31_DEVICE_ADDR, I2CFAST, I2CBYTE Return Sht31_SingleShot: HI2cOut ( $24, $00 ) Return Sht31_Read: Do Do Gosub Sht31_SingleShot Pause 50 HI2cIn ( temperature.msb, temperature.lsb, temperature.crc, humidity.msb, humidity.lsb, humidity.crc ) w0 = temperature Gosub CalculateCrc If crc <> temperature.crc Then Gosub Sht31_Initialise End If Loop until crc = temperature.crc w0 = humidity Gosub CalculateCrc If crc <> humidity.crc Then Pause 10 End If Loop until crc = humidity.crc temperature = temperature ** 17500 - 4500 humidity = humidity ** 10000 Return ; Calculate checksum of w0 CalculateCrc: crc = $FF For idx = 0 to 15 polynomial = b1 ^ crc & $80 If polynomial <> 0 Then polynomial = $31 End If crc = crc * 2 ^ polynomial w0 = w0 * 2 Next Return ; ******************************************************************************* ; * * ; * End of Program * ; * * ; *******************************************************************************