'*********************** 'Eelctricity Meter Monitor Rx '433MHz Receiver 'Picaxe 08M2 'LCD Display with I2c port expander 'Phillip Webb 2012 'Ver 5 '*********************** 'Clock setfreq M16 'Variables symbol outbyte = b0 symbol rs = b1 ' bit 0 of port expander symbol counter1 = b2 'second counter needed as lcd uses one symbol nblcount = b3 symbol counter = w4 symbol Temp = w5 'b10 b11 symbol Watts =w6 'b12 b13 data symbol Secscount = w7'b14,b15 symbol Thous = b16' w8 symbol Hunds = b17' w8 symbol Tens = b18' w9 symbol Units = b19'w9 symbol kWHr = w10 symbol Durationms = w11 'Constants symbol lcddata =1 symbol lcdinst =0 'Pins symbol dataLED = c.4 symbol RxPin = c.3 '***************** 'initilise I2C hi2csetup i2cmaster, %01001110 , i2cslow,i2cbyte ' %0-1-0-0-A2-A1-A0-0 is 8bit port xpander address hi2cout $00,(%00000000) '$00 is inout set address, 0 sets GPIO to output, 1 sets to input '****************** 'Initialise LCD rs = lcdinst for counter = 0 to 5 lookup counter, ($33,$32,$28,$0C,$01,$06),outbyte gosub lcdout next counter '******************* gosub display1stline '******************* Main: low dataled serin RxPin, N2400_16,("MM1"), thous, hunds, tens,units high dataled kwhr = kwhr +1 gosub display1stline gosub display2ndline goto main '******************* 'Send to LCD lcdout: for nblcount = 1 to 2 temp = outbyte & $F0 |2|rs 'high 4 bits + Clock Hi + RS hi2cout $09, (temp) ' $09 address is the GPIO ports temp = temp & %11111101 'clock low ' don't change high order bits hi2cout $09, (temp) ' $09 address is the GPIO ports pause 4 outbyte = outbyte * 16 'shift left next nblcount return '******************* Display1stline: 'display message rs = lcdinst outbyte = $80 ' first line gosub lcdout rs = lcddata for counter1 = 0 to 5 lookup counter1 , ("Meter:"), outbyte gosub lcdout next counter1 for counter1 =1 to 10 'require word variable for >255 select case counter1 case 1 temp = kwhr/10000 ' 10 case 2 temp = kwhr/1000 '1 temp = temp//10 case 3 temp = 46 - $30 '. case 4 temp = kwhr/100 '0.1 temp = temp //10 case 5 temp = kwhr/10 '0.01 temp = temp//10 case 6 temp = kwhr//10 '0.001 case 7 temp = 107 - $30 'k case 8 temp = 87 - $30 'W case 9 temp = 72 - $30 'H case 10 temp = 114 - $30 'r end select outbyte = temp + $30 gosub lcdout next counter1 return Display2ndline: 'display data setup for second line rs = lcdinst 'Second Line outbyte = $C0 gosub lcdout rs = lcddata 'Calculate duration from received data temp= thous * 1000 temp = hunds * 100 + temp temp = tens * 10 + temp durationms = units + temp ' time in ms ' Power calc is 360000/duration in 10's of ms. ' Use 60000 then multiply by 6 to keep within word variable size watts = 60000/durationms * 6 ' integer parts 'Now consider the fractional or remainder temp = 60000//durationms 'remainder temp = temp * 6 ' 6 times remainder temp = temp /durationms 'calc remainder part watts = watts +temp 'add integer and remainder 'Display Watts for counter1 =2 to 7 select case counter1 case 1 temp = watts/10000 ' 10000's case 2 temp = watts/1000 '1000's temp = temp//10 case 3 temp = watts/100 '100's temp = temp //10 case 4 temp = watts/10 '10's temp = temp//10 case 5 temp = watts//10 ' 1's case 6 temp = 87 - $30 'W case 7 temp = 32 - $30 'Space end select outbyte = temp + $30 gosub lcdout next counter1 'display duration outbyte = thous +$30 gosub lcdout outbyte = hunds +$30 gosub lcdout outbyte = 46 '. gosub lcdout outbyte = tens +$30 gosub lcdout outbyte = units +$30 gosub lcdout outbyte = 115 's gosub lcdout return '*******************