Вы находитесь на странице: 1из 7

' ============================================================================== ' ' {$STAMP BS2e} ' ' ============================================================================== ' -----------------------------------------------------------------------------' I/O Definitions ' -----------------------------------------------------------------------------ShtData

Clock cnt VAR Word '-----Constant declarations-----'**************Variables for light sensor********************* S0 CON 0 'Sensitivity selection pins S1 CON 1 IN CON 2 'TSL230 output pin input to controller OUT CON 3 'Output to LED1 LED1 CON 4 'Output to LED2 LED2 CON 5 'Output To LED3 relay CON 7 'Connection to fan through relay relay1 CON 9 'relay for cooling equipment relay2 CON 12 'relay for humnidifier humid_fan CON 13 'relay for dehumidifier '*************Variables ShtTemp CON ShtHumi CON ShtStatW CON ShtStatR CON ShtReset CON Ack NoAck No Yes MoveTo ClrRt DegSym CON CON CON CON CON CON CON for tmp/hum sensor******************** %00011 ' read temperature %00101 ' read humidity %00110 ' status register write %00111 ' status register read %11110 ' soft reset (wait 11 ms after) 0 1 0 1 2 11 186 ' for DEBUG control ' clear DEBUG line to right ' degrees symbol for DEBUG CON CON 6 8 ' bi-directional data

' -----------------------------------------------------------------------------' Variables ' -----------------------------------------------------------------------------ioByte ackBit toDelay timeOut soT tC tF VAR VAR VAR VAR VAR VAR VAR Byte Bit Byte Bit Word Word Word ' ' ' ' data from/to SHT1x ack/nak from/to SHT1x timeout delay timer timeout status

' temp counts from SHT1x ' temp - celcius ' temp - fahrenheit

soRH rhLin rhTrue

VAR VAR VAR

Word Word Word

' humidity counts from SHT1x ' humidity; linearized ' humidity; temp compensated

status VAR Byte ' SHT1x status byte tempthresh VAR Word ' temp Threshold humiditythresh VAR Word ' Humidity Threshold lightthresh VAR Word ' Light Threshold counter VAR Nib ' -----------------------------------------------------------------------------' EXCEL VARIABLES ' -----------------------------------------------------------------------------sPin CON 16 'Serial Pin - P16, Programming por t Baud CON 84 'Baud mode for a rate of 9600, 8-N -1 ' -----------------------------------------------------------------------------' EEPROM Data ' -----------------------------------------------------------------------------' -----------------------------------------------------------------------------' Initialization ' -----------------------------------------------------------------------------LOW S0 'Set sensitivity pins (S0,S1) to x10 HIGH S1 Initialize: GOSUB SHT_Connection_Reset PAUSE 250 ' reset device connection ' let DEBUG window open

' -----------------------------------------------------------------------------' Program Code ' -----------------------------------------------------------------------------'SEROUT sPin,Baud,["CELL,SET,B4,",DEC cnt,CR] 'Send Current Light to Excel S heet MAIN: SEROUT sPin,Baud,[CR] 'Send a lone CR to ensure PLX-DAQ buffer is ready '******************************************LCD DISPLAY************************* ********************* SEROUT 10, 84, [22, 12, 17] 'PORT #10 SEROUT 10, 84, [128, "TEMP HUMID LGHT", 148, DEC (tC / 10), ".", DEC1 tC, 154 , DEC (rhTrue / 10), ".", DEC1 rhTrue, "%", 161, DEC cnt] '*************************************TEMPERATURE SENSOR*********************** ********************* GOSUB SHT_Measure_Temp DEBUG MoveTo, 0, 3 DEBUG "TEMPERATURE ", CR DEBUG "soT...... " DEBUG SDEC soT, ClrRt, CR DEBUG "tC....... " DEBUG SDEC (tC / 10), ".", SDEC1 tC, DegSym, ClrRt, CR SEROUT sPin,Baud,["CELL,SET,B2,",SDEC (tC / 10), ".", SDEC1 tC,CR] 'Send C urrent temprature (C) to Excel Sheet DEBUG "tF....... "

DEBUG SDEC (tF / 10), ".", SDEC1 tF, DegSym, ClrRt, CR SEROUT sPin,Baud,["CELL,GET,B1,",CR] ' Read Temprature Threshold from the Excel Database SERIN sPin, Baud,200,Timeout1,[SDEC tempthresh] ' Set Threshold Value t o the read value DEBUG "tempthresh...", tempthresh, CR, CR MAIN3: tempthresh = ((tempthresh*10) + 400)*10 SEROUT sPin,Baud,["CELL,SET,C7,",SDEC tempthresh,CR] 'Send Set tempratur e (C) to Excel Sheet for verification 'Comparing Temperature Threshold Values IF (soT < tempthresh) THEN high_hot_fan IF ((soT > tempthresh) & (soT < (tempthresh+100))) THEN low_cool_fan IF (soT > (tempthresh+100)) THEN high_cool_fan MAIN1: '**************************************HUMIDITY SENSOR************************ ********************* GOSUB SHT_Measure_Humidity 'humiditythresh = 400 DEBUG MoveTo, 0, 8, CR DEBUG "HUMIDITY ", CR DEBUG "soRH..... " DEBUG DEC soRH, ClrRt, CR DEBUG "rhLin.... " DEBUG DEC (rhLin / 10), ".", DEC1 rhLin, "%", ClrRt, CR DEBUG "rhTrue... " DEBUG DEC (rhTrue / 10), ".", DEC1 rhTrue, "%", ClrRt SEROUT sPin,Baud,["CELL,SET,B6,",DEC (rhTrue / 10), ".", DEC1 rhTrue,CR] ' Send Current Humidity to Excel Sheet SEROUT sPin,Baud,["CELL,GET,B5,",CR] ' Read Humidity Threshold from th e Excel Database SERIN sPin, Baud,200,Timeout2,[DEC humiditythresh] ' Set Threshold Valu e to the read value MAIN4: SEROUT sPin,Baud,["CELL,SET,C9,",DEC humiditythresh,CR] 'Send Current Hu midity to Excel Sheet SEROUT sPin,Baud,["CELL,SET,B6,",DEC (rhTrue / 10), ".", DEC1 rhTrue,CR] 'Send Current Humidity to Excel Sheet 'Comparing the humidity Threshold Values IF (rhTrue < (humiditythresh*10)) THEN HIGH_RELAY2 IF (rhTrue > ((humiditythresh*10)+20)) THEN LOW_RELAY2 MAIN2: '****************************************LIGHT SENSOR************************** ********************* DEBUG MoveTo, 0, 13, CR COUNT IN, 100, cnt 'COUNT on P2 FOR 100 ms, store value in cnt SEROUT sPin,Baud,["CELL,SET,B4,",DEC cnt,CR] 'Send Current Light to Excel Sheet SEROUT sPin,Baud,["CELL,GET,B3,",CR] ' Read Light Threshold from the Exce l Database SERIN sPin, Baud,200,Timeout3,[DEC lightthresh] ' Set Threshold Value t o the read value MAIN5: SEROUT sPin,Baud,["CELL,SET,C8,",DEC lightthresh,CR] 'Send Set light to Excel Sheet for verification

'Comparing the light Threshold Values IF (cnt < lightthresh) THEN LED_HIGH1 'If Intensity very low, turn on all L EDs IF y low, IF ensity IF ff all (cnt turn (cnt low, (cnt LEDs > lightthresh & cnt < (lightthresh+10)) THEN LED_HIGH2 'If Intensit on only one LED > (lightthresh+10) & cnt < (lightthresh+40)) THEN LED_HIGH3 'If Int turn on only one LED > (lightthresh+40)) THEN LED_LOW 'If Intensity really High, turn o

GOTO MAIN ' -----------------------------------------------------------------------------' 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 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 ioByte = ShtTemp GOSUB SHT_Write_Byte GOSUB SHT_Wait ackBit = Ack GOSUB SHT_Read_Byte soT.HIGHBYTE = ioByte ackBit = NoAck GOSUB SHT_Read_Byte soT.LOWBYTE = ioByte ' ' ' ' ' ' alert device temperature command send command wait until measurement done another read follows get MSB

' let pull-up take line high

' last read ' get LSB

' Note: Conversion factors are multiplied by 10 to return the ' temperature values in tenths of degrees

tC = soT / 10 - 400 tF = soT ** 11796 - 400 RETURN ' measure humidity ' SHT_Measure_Humidity: GOSUB SHT_Start ioByte = ShtHumi GOSUB SHT_Write_Byte GOSUB SHT_Wait ackBit = Ack GOSUB SHT_Read_Byte soRH.HIGHBYTE = ioByte ackBit = NoAck GOSUB SHT_Read_Byte soRH.LOWBYTE = ioByte

' convert to tenths C ' convert to tenths F

' ' ' ' ' '

alert device humidity command send command wait until measurement done another read follows get MSB

' last read ' get LSB

' Conversion factors are multiplied by 10 and then rounded to ' return tenths ' rhLin = (soRH ** 26542) rhLin = rhLin - ((soRH ** 3468) * (soRH ** 3468) + 50 / 100) rhLin = rhLin - 40 ' Conversion factors are multiplied by 100 to improve accuracy and then ' rounded off. ' rhTrue = ((tC / 10 - 25) * (soRH ** 524 + 1) + (rhLin * 10)) + 5 / 10 RETURN Timeout1: GOTO MAIN3 Timeout2: GOTO MAIN4 Timeout3: GOTO MAIN5 LED_HIGH1: DEBUG "Light Intensity Lowest...." DEBUG DEC5 cnt , ClrRt, CR HIGH OUT HIGH LED1 HIGH LED2 RETURN LED_HIGH2: DEBUG "Light Intensity Medium1...." DEBUG DEC5 cnt , ClrRt, CR LOW OUT HIGH LED1 HIGH LED2 RETURN LED_HIGH3: DEBUG "Light Intensity Medium2...." DEBUG DEC5 cnt , ClrRt, CR LOW OUT 'Turn ON all LEDs 'Display value of cnt, as a decimal

'Turn ON 2 LEDs 'Display value of cnt, as a decimal

'Turn ON 1 LEDs 'Display value of cnt, as a decimal

LOW LED1 HIGH LED2 RETURN LED_LOW: DEBUG "Light Intensity Highest...." DEBUG DEC5 cnt , ClrRt, CR LOW OUT LOW LED1 LOW LED2 RETURN high_cool_fan: DEBUG "Turning Cooler ON....", CR HIGH relay1 LOW relay GOTO MAIN1 low_cool_fan: DEBUG "Turning Fans OFF....", CR LOW relay LOW relay1 GOTO MAIN1 high_hot_fan: DEBUG "Turning Heater ON....", CR HIGH relay LOW relay1 GOTO MAIN1 HIGH_RELAY2: DEBUG "Turning on Humidifier....", CR HIGH relay2 LOW humid_fan GOTO MAIN2 LOW_RELAY2: DEBUG "Turning off Humidifier....", CR LOW relay2 HIGH humid_fan GOTO MAIN2 SHT_Write_Status: GOSUB SHT_Start ioByte = ShtStatW GOSUB SHT_Write_Byte ioByte = status GOSUB SHT_Write_Byte RETURN ' returns "status" ' SHT_Read_Status: GOSUB SHT_Start ioByte = ShtStatW GOSUB SHT_Read_Byte ackBit = NoAck GOSUB SHT_Read_Byte 'Turn OFF all LEDs 'Display value of cnt, as a decimal

' alert device ' write to status reg command ' send command

' ' ' '

alert device write to status reg command send command only one byte to read

RETURN ' sends "ioByte" ' returns "ackBit" ' SHT_Write_Byte: SHIFTOUT ShtData, Clock, MSBFIRST, [ioByte] SHIFTIN ShtData, Clock, LSBPRE, [ackBit\1] RETURN

' send byte ' get ack bit

' 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 ' wait for device to finish measurement (pulls ' -- timeout after ~1/4 second ' SHT_Wait: INPUT ShtData FOR toDelay = 1 TO 250 timeOut = INS.LOWBIT(ShtData) IF (timeOut = No) THEN SHT_Wait_Done PAUSE 1 NEXT SHT_Wait_Done: RETURN ' reset SHT1x with soft reset ' SHT_Soft_Reset: GOSUB SHT_Connection_Reset ioByte = ShtReset ackBit = NoAck GOSUB SHT_Write_Byte PAUSE 11 RETURN data line low)

' ' ' '

data line is input give ~1/4 second to finish scan data line if low, we're done

' ' ' ' '

reset the connection reset command only one byte to send send it wait at least 11 ms

Вам также может понравиться