;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Sensirion SHT11 sensor interface ;; ;; ;; ;; Copyleft (L) P Pemberton ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PIC setup and toplevel includes LIST R=DEC INCLUDE sxdefs.inc INCLUDE sxregs.inc EXPAND ; Expand must be ON for ICD #DEFINE BANK0 BCF STATUS, PA0 #DEFINE BANK1 BSF STATUS, PA0 ; SX18AB DEVICE EQU PINS18+OSC4MHZ+PAGES2+BANKS2 RAMBase EQU 07h ;Start of RAM SEROUT MACRO __byte MOVLW __byte ; CALL SER_SEND ENDM CHKWZ MACRO __outz, __outnz IORLW .0 MOVLW __outz BTFSS STATUS, Z MOVLW __outnz ; CALL SER_SEND ENDM ; Pindefs #DEFINE SHT_SDA PORTB, 7 #DEFINE SHT_SCL PORTB, 6 ; Variables CBLOCK RAMBase TEMPERATURE:2 HUMIDITY:2 CHKSUM CHAHI, CHALO ENDC ; Reset vector ORG 0 GOTO START ; Interrupt vector ORG 4 GOTO START ; Start of code ORG 5 START: ; CALL SER_SETUP ; Set up the serial port CALL SHT_BUSINIT ; Init the SHT bus MOVLW .5 ; Read Humidity CALL SHT_SEND CALL SHT_WAITACK ; Wait for an ACK CHKWZ "A","!" CALL SHT_WAIT ; Wait for the data CHKWZ "W","!" CALL SHT_RECEIVE ; Get byte 1 MOVWF HUMIDITY+0 ; Store it CALL SHT_ACK ; Wait for an ACK CALL SHT_RECEIVE ; Get byte 2 MOVWF HUMIDITY+1 ; Store it CALL SHT_ACK ; Wait for an ACK CALL SHT_RECEIVE MOVWF CHKSUM CALL SHT_NACK MOVF HUMIDITY+0, W ; Send humidity to host CALL HEXOUT MOVF HUMIDITY+1, W CALL HEXOUT MOVF CHKSUM, W CALL HEXOUT SEROUT " " GOTO $ ; Crash! ;; Subroutines HEXOUT: MOVWF CHALO SWAPF CHALO, W ANDLW 0x0F MOVWF CHAHI MOVLW 0x0F ANDWF CHALO MOVF CHAHI, W CALL HEXTAB ; CALL SER_SEND MOVF CHALO, W CALL HEXTAB ; CALL SER_SEND RETURN HEXTAB: ADDWF PC, F DT "0123456789ABCDEF" ;; Final post-code includes #DEFINE SER_BAUD .19200 INCLUDE "sht11.inc" END