'CISECO PLC (c) 2008 - Electricty monitor v1.5 on v1.2 dev board, with temp and output relay and music and 12v battery mon
init:
setfreq m4
disablebod
'calibfreq -4'work around for some older 28x1's
symbol BAUD=T4800_4'set baud rate, can be any but needs to match the XBee setup
symbol XBEEdataoutPIN=4'output 4 is used to send data to the XBee
symbol XBEEdatainPIN=7'input 7 is used to recieve data from the XBee
symbol s1="E" 'serial code part 1
symbol s2="M" 'serial code part 2
gosub XBEEwake
pause 200
serout XBEEdataoutPIN,BAUD,("aA",s1,s2,"STARTED-") 'send alert to say the sensor has started
pause 100
'###############################################################################
'start the background counter
Poke $0F, 0 ' clear high bytes of Timer1
Poke $0E, 0 ' clear low bytes of Timer1
Poke $10, $03 ' set T1CON for Timer1
'###############################################################################
main:
b0=""
serin XBEEdatainPIN,BAUD,("a"),b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10
sertxd ("COMMAND RECIEVED a",b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10, 13, 10) 'echo the command to the programming editor, useful for debugging, can be deleted
if b0<>"C" then main 'ignore anything not a command
if b1<>s1 then main 'ignore anything not for this sensor first digit
if b2<>s2 then main 'ignore anything not for this sensor second digit
if b3="E" then echo_proc
if b3="Y" then capabilities_proc
if b3="C" then counter_proc
if b3="W" then version_proc
if b3="Z" and b4="Z" then sleep_proc
if b3="T" then temp_proc
if b3="O" then output_proc
if b3="M" then music_proc
if b3="R" then read_proc
goto main
'###############################################################################
read_proc:
if b4="0" then 'READ CHANNEL 0 IN (E)IGHT, (T)EN BIT or (V) volts MODE
if b5="E" then readadc 0,b0
serout XBEEdataoutPIN,BAUD,("aR",s1,s2,"R1E",#b0,"----")
goto main
endif
if b5="T" then readadc10 0,w0
serout XBEEdataoutPIN,BAUD,("aR",s1,s2,"R1T",#w0,"----")
goto main
endif
if b5="V" then readadc10 0,w5
'convert
w0=w5*64/430
serout XBEEdataoutPIN,BAUD,("aR",s1,s2,"R1V",#w0,"----")
goto main
endif
endif
goto main
'###############################################################################
music_proc:
play 3,0'piezo sounder on output 3
goto main
'###############################################################################
counter_proc:
if b4="A" and b5="D" then 'decrement by the value sentin plain 5 digit ascii - ie 00009
w6=0'clear then add bytes b6,b7,b8,b9,b10 up to make a decimal number, convert to lowest and highest bit and deduct from the counter
b6=b6-48 'an ascii 0 is actually considered 48
w1=b6*10000
w6=w6+w1
b7=b7-48
w1=b7*1000
w6=w6+w1
b8=b8-48
w1=b8*100
w6=w6+w1
b9=b9-48
w1=b9*10
w6=w6+w1
b10=b10-48
w6=w6+b10
'read the counter into w7 and take the sent number (w6) away from the counter
Peek $0F, b15
Peek $0E, b14
w7=w7-w6
Poke $0F, b15
Poke $0E, b14
'send an acknowledgement back stating the number deleted
if w6<10 then
serout XBEEdataoutPIN,BAUD,("aR",s1,s2,"CAD0000",#w6)
goto main
endif
if w6<100 then
serout XBEEdataoutPIN,BAUD,("aR",s1,s2,"CAD000",#w6)
goto main
endif
if w6<1000 then
serout XBEEdataoutPIN,BAUD,("aR",s1,s2,"CAD00",#w6)
goto main
endif
if w6<10000 then
serout XBEEdataoutPIN,BAUD,("aR",s1,s2,"CAD0",#w6)
goto main
endif
serout XBEEdataoutPIN,BAUD,("aR",s1,s2,"CAD",#w6)
endif
'send an acknowledgement back stating the current counter value
if b4="A" and b5="-" then
Peek $0F, b13
Peek $0E, b12
if w6<10 then
serout XBEEdataoutPIN,BAUD,("aR",s1,s2,"CA-0000",#w6)
goto main
endif
if w6<100 then
serout XBEEdataoutPIN,BAUD,("aR",s1,s2,"CA-000",#w6)
goto main
endif
if w6<1000 then
serout XBEEdataoutPIN,BAUD,("aR",s1,s2,"CA-00",#w6)
goto main
endif
if w6<10000 then
serout XBEEdataoutPIN,BAUD,("aR",s1,s2,"CA-0",#w6)
goto main
endif
serout XBEEdataoutPIN,BAUD,("aR",s1,s2,"CA-",#w6)
endif
if b4="A" and b5="R" and b6="E" and b7="S" and b8="E" and b9="T" then
Poke $0F, 0 ' clear high bytes of Timer1
Poke $0E, 0 ' clear low bytes of Timer1
serout XBEEdataoutPIN,BAUD,("aR",s1,s2,"CARESET-")'send an acknowledgement back stating the counter has been reset
goto main
endif
goto main
'###############################################################################
output_proc:
if b4="0" and b5="0" then low 0
endif
if b4="0" and b5="1" then high 0
endif
if b4="1" and b5="0" then low 1
endif
if b4="1" and b5="1" then high 1
endif
if b4="2" and b5="0" then low 2
endif
if b4="2" and b5="1" then high 2
endif
if b4="3" and b5="0" then low 3
endif
if b4="3" and b5="1" then high 3
endif
if b4="4" and b5="0" then low 4
endif
if b4="4" and b5="1" then high 4
endif
if b4="5" or b4="R" and b5="0" then low 5'onboard relay
endif
if b4="5" or b4="R" and b5="1" then high 5'onboard relay
endif
if b4="6" and b5="0" then low 6
endif
if b4="6" and b5="1" then high 6
endif
if b4="L" and b5="1" then high portc 1'onboard LED
endif
if b4="L" and b5="0" then low portc 1'onboard LED
endif
serout XBEEdataoutPIN,BAUD,("aR",s1,s2,"O",b4,b5,"-----")
goto main
'###############################################################################
sleep_proc:
'RESET- a soft reboot
if b5="R" and b6="E" and b7="S" and b8="E" and b9="T" then
reset
endif
'DOWN - permanently sleep
if b5="D" and b6="O" and b7="W" and b8="N"then
serout XBEEdataoutPIN,BAUD,("aA",s1,s2,"ZZDOWN--")
pause 100
gosub XBEEsleep
pause 100
end
goto main
endif
'SLEEP XBee and pause, leaves the microcontroller on still
if b10="S" or b10="M" or b10="Z" then
w6=0'clear then add bytes b6,b7,b8,b9,b10 up to make a decimal number, convert to lowest and highest bit and deduct from the counter
b5=b5-48 'an ascii 0 is actually considered 48
w1=b5*10000
w6=w6+w1
b6=b6-48
w1=b6*1000
w6=w6+w1
b7=b7-48
w1=b7*100
w6=w6+w1
b8=b8-48
w1=b8*10
w6=w6+w1
b9=b9-48
w6=w6+b9
if b10="Z" then'SLEEP the entire unit, stops all functions till it re wakes
sertxd ("SLEEP",#w6)
serout XBEEdataoutPIN,BAUD,("aA",s1,s2,"SLEEPING")
pause 200
gosub XBEEsleep
sleep w6
gosub XBEEwake
pause 100
sertxd ("AWAKE")
serout XBEEdataoutPIN,BAUD,("aA",s1,s2,"AWAKE---")
goto main
endif
if b10="S" then
let w2=1000
w6=w6-1
if w6=65535 then
let w6=0
endif
endif
if b10="M" then let w2=60000
endif
serout XBEEdataoutPIN,BAUD,("aA",s1,s2,"SNOOZING")
pause 500
gosub XBEEsleep
if w6=0 then goto jump
for w1=1 to w6
pause w2
next w1
jump:
gosub XBEEwake
pause 500
serout XBEEdataoutPIN,BAUD,("aA",s1,s2,"AWAKE---")
endif
goto main
'###############################################################################
temp_proc:
if b4="5" then
readtemp12 5,w3
' the dallas sensor gives back a reading that needs converting to degrees C
' as the first bits contain the sign + or -
b3 = w3 / 256 / 128
if b3 = 1 then
w3 = w3 ^ $ffff + 1
b10="-"
else
b10="+"
endif
w3 = w3 * 10 / 16
b12 = w3 / 10
b13 = w3 // 10
if b12 < 10 then
b11="0"
serout XBEEdataoutPIN,BAUD,("aR",s1,s2,"T5",b10,"0",#b12,".",#b13,"-")
else
serout XBEEdataoutPIN,BAUD,("aR",s1,s2,"T5",b10,#b12,".",#b13,"-")
endif
endif
goto main
'###############################################################################
version_proc:
serout XBEEdataoutPIN,BAUD,(b1,b2,"CISECO PLC Electricty monitor v1.3")
goto main
'###############################################################################
capabilities_proc:
serout XBEEdataoutPIN,BAUD,(b1,b2,",E,Y,CA,CAD,CARESET,W,ZZxxxxxS,ZZxxxxxM,ZZDOWN,ZZxxxxxZ")
goto main
'###############################################################################
echo_proc:
serout XBEEdataoutPIN,BAUD,("aR",b1,b2,"HERE----")
goto main
'###############################################################################
XBEEwake:
high 6'take XBee SLEEP pin high
high 4'take XBee DATA pin high
high 7'take XBee RESET pin high
return
'###############################################################################
XBEEsleep:
low 6'take XBee SLEEP pin low
low 4'take XBee DATA pin low
low 7'take XBee RESET pin low
return
goto main