The Outdoor Unit Picaxe Code | |
The Outdoor Unit - Picaxe CodeThis is the latest code but is subject to change. [October 2013]Corrected error in negative temperature maths and replaced faulty Humidity Sensor [08/11/2011]
Variable Assignment Table
; ================================================================== ; Main 18M2 code for the Picaxe Weather Station Outdoor (Transmitter) Unit ; Decimal precision Humidity & Temperature routines, ; copyright, Peter H Anderson, Baltimore, MD, Jan, '04 ; ; ================================================================== #Picaxe 18M2 Symbol HValue = w0 Symbol HighWord = w1 Symbol LowWord = w2 Symbol RH10 = w3 Symbol HQuotient = b0 Symbol HFract = b1 Symbol X = b0 Symbol aDig = b1 Symbol TFactor = b2 Symbol Tc = b3 Symbol SignBit = b4 Symbol TValue = w4 Symbol TQuotient = b10 Symbol TFract = b11 Symbol TempC_100 = w6 Symbol MagDir = w7 Symbol MagDirLo = b14 Symbol MagDirHi = b15 Symbol WindSpeed = w8 Symbol WindSpeedLo = b16 Symbol WindSpeedHi = b17 Symbol ThisHour = b18 Symbol LastHour = b19 Symbol RainRequest = b20 ; Hardware Symbol HumidRaw = B.7 Symbol TempRaw = B.6 Symbol DirRaw = B.3 Symbol Speed = B.0 do ; Read Humidity ReadADC10 HumidRaw, HValue ;Get Humidity (HValue) HighWord = 1613 ** HValue ; calculate RH LowWord = 1613 * HValue RH10 = LowWord / 1024 LowWord = Highword * 64 RH10 = RH10 + LowWord RH10 = RH10 - 258 pause 100 ; Read temperature Readtemp12 TempRaw, TValue ; Get temperature SignBit = TValue / 256 / 128 if SignBit = 0 then positive ; It's negative so TValue = TValue ^ $ffff + 1 ; take twos comp positive: TempC_100 = TValue * 6 ; TC = value * 0.0625 TValue = TValue * 25 / 100 TempC_100 = TempC_100 + TValue TQuotient = TempC_100 / 100 TFract = TempC_100 % 100 / 10 X = TQuotient / 10 ; Calculate temperature correction factor for Humidity if SignBit = 0 then SignBit = " " else SignBit = "-" endif if SignBit = "-" then X = 4 - X else X = X + 4 endif GoSub TempCorrection ; compensate RH HQuotient = RH10 / 10 ; Calculate RH Quotient and... HFract = RH10 % 10 ; ...decimal place. if HQuotient > 99 then ; Over range HQuotient = 99 HFract = 9 endif if HQuotient > 127 then ; Under range HQuotient = 0 HFract = 0 endif ; Read AS540 magnetic encoder for wind direction readadc10 DirRaw, MagDir ; Read from AS5040 magnetic bearing pause 100 ; Read rpm from windspeed counter count Speed, 1000, WindSpeed ; Every 30th cycle (approx 1 minute), request rain gauge data from 08M inc RainRequest if RainRequest >= 30 then high C.1 serin [500], C.0, N2400, ("r"), LastHour, ThisHour ; Rain counters low C.1 RainRequest = 0 endif ; Send data to Indoor Unit in 8 byte blocks ; First group needs no calibration so calculations are done here first. ; Second group will need "tweaking" - more easily done at indoor end. serout C.2, N2400, ("t", SignBit, TQuotient, TFract, HQuotient, HFract, "A", "B") pause 100 serout C.2, N2400, ("m", MagDirHi, MagDirLo, WindSpeedHi, WindSpeedLo, LastHour, ThisHour, "C") loop TempCorrection: Lookup X, (87, 89, 91, 93, 95, 97, 99, 101, 103, 106, 108, 110, 113, 116, 119, 122, 126), TFactor ' -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 if TFactor < 100 then aDig = TFactor / 10 RH10 = RH10 * aDig / 10 TFactor = TFactor % 10 aDig = TFactor RH10 = RH10 * aDig / 100 + RH10 else TFactor = TFactor % 100 aDig = TFactor / 10 RH10 = RH10 * aDig / 10 + RH10 TFactor = TFactor % 10 aDig = TFactor RH10 = RH10 * aDig / 100 + RH10 endif ; [October 2013] The humidity has consistently returned too high a value, frequently above 99.9%. ; To get a feel for how high the reading is, I've added the following two lines to reduce the ; returned value by 20%. So far, the new value is agreeing well with a local commercial ; weather station. aDig = RH10 * 20 / 100 RH10 = RH10 - aDig return {Memory used = 295 bytes out of 2048}
|