author:c.Jun,l.Jin
date: Mar 11,2006
;-----------------------------------------------------------------------------------DATA SECTION BEGIN
; R0specially reserved for data to Recv pointer!!!
; R1hold the pointer to write
; R2!
; R3!
; R4! used by DS18B20,exclusive to I.S.R
; R5!!!
; R6!!!
; R7!!!specially reserved for delay micro.!!!
; P0serial in/out (experimental)
; P1command execution indicator (experimental)
; P2DS18B20 DQ read/write
;; flag-bit zone(00h~10h at addr:20H~21H) max of 16
donebit00h;flag bit:recieved=1
todobit01h;msg todo
reportingbit02h;data ready to send
dirtbit03h;buffer is dirty
chaosbit04h;out of order :frame
syn1bit05h;1st byte of SYN
badbit06h;argument not matched
mutexbbit07h;buffer access muturally
iddleSbit08h;call iddle state
rebooSbit09h;call reboot state
checkSbit0ah;call system complete check then report
lookSbit0bh;call look presence info.
rtemprSbit0ch;call read temperature
wvalveSbit0dh;call write valve data
presencebit0eh;DS18B20 device found
search_finbit0fh;22H.1 : find a device ROM done?
nxtbit10h;any more device?
gbit11h
;; buffer for serial
seqequ24h;the next frame.no waiting to receive: max value is 255
lengequ25h;the command defined msg's total length
sumequ26h;checksum of the msg
cmdequ27h; max of 1 bytes
argequ28h;(28h~2fh) max of 8 bytes*********first byte must bits-addr
tbufequ27h
temprequ28h
nequ26h
;; approp-data zone(4fh~6fh) max of 48 bytes available
ROMsequ30h;30h~5fh 6 ROMs (64-bit SN each)
vdataequ60h;060h~06fh,control data for valve,2 byte for each room
pdataequ70h;for Room 0~7,presence state
;; constant
bodybitP3.4;body detector
DQbitP3.5;DS18B20 slaves-bus
SYNequ00h
;-----------------------------------------------------------------------------------DATA SECTION END
org0000h
ljmpstartup
;org0003h
;ljmpisrEX0
org000bh
ljmpisrT0
;org0013h
;ljmpisrEX1
;org001bh
;ljmpisrET1
org0023h
ljmpisrES
;org002bh
;ljmpisrT2
org0030h
;;------------------I.S.R----------------------------------------------------------
isrT0:;dead-loop resolve
decsp
decsp;force executing next instruction following "jxb x,___FCKpd___1quot;
CLRTR0
MOVTL0,#0FFH
MOVTH0,#0FFH;auto reload,auto disable
reti
isrT1:nop
reti
isrES:;FSM modeling & scatter-listing
jbRI,doRi
jmpdoTi
doRi:clrRI
MOVA,#0
XRLA,seq
jnzseq1
MOVA,SBUF;iddle for beginning
XRLA,#SYN
jzisSyn;new beginning?
setbchaos
jmperr
seq1:movA,#1;seq=1?
xrlA,seq
jnzseq2
movA,SBUF
orlA,#0fh
xrlA,#0fh;buf=cmd? COMMANDёЯЛДО»Оª0
jzisCmd
setbbad
jmperr
seq2:movA,leng;how many before a checksum incoming
clrC
subbA,seq;seq<leng?
jzstop;seq=leng
jcerr;seq>leng
fill:;argument valid?...
mov@R0,SBUF
jbbad,err
incR0
incseq
cplP2.1
jmpsay
stop:movsum,SBUF
lcallcheksum
movA,sum;0:ok non-0:wrong!
jzept
setbbad
jmperr
ept:setbtodo;request need to do...
jnbmutexb,err;what's odd!
clrmutexb;unlock bufferrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
movseq,#0
setbP2.2
setbP2.1
setbP2.0
;so : any msg following except "SYN" would be regarded as error.
jmpsay
isSyn:clrtodo;set all for beginning Receiving
clrdone
setbP1.0
movseq,#1
movleng,#0
setbmutexb;termless lock bufferrrrrrrrrrrrrrrrrrrrrrrr
movR0,#cmd
movR1,#tempr
setbP2.2
setbP2.1
clrP2.0
jmpsay
isCmd:movcmd,SBUF
LCALLparse;msg length will be set here!!!!!!!!!!
jnbbad,z49
jmperr
z49:incseq
clrP2.1
jmpsay
err:setbdirt;reset in major:need emergent action!
clrP2.2
say:;say sth.:-)
MOVA,SBUF
CPLA
MOVP0,A
LCALLd200ms
reti
doTi:clrTI
jbreporting,report;DMA when major requiring
jmpsayT
report:djnzleng,Nend
clrreporting
setbdone;inform major:task completted
clrmutexb
jmpsayT
Nend:incR1
movSBUF,@R1
movtbuf,@R1;save a copy
sayT:movA,tbuf
cplA
movP0,A
lcalld200ms
lcalld200ms
lcalld200ms
reti
;;------------------function---------------------------------------------------------
parse:;process different command in frame(used only in isr)
movA,#0ah;readPresenceCmd
xrlA,cmd
jzdo1
movA,#0bh;readtemprCmd
xrlA,cmd
jzdo2
movA,#0ch;conrolCmd(write)
xrlA,cmd
jzdo3
movA,#0dh;iddleCmd
xrlA,cmd
jzdo4
movA,#0eh;rebootCmd
xrlA,cmd
jzdo5
movA,#0fh;checkCmd
;else
cor:setbbad
ret
do1:movleng,#3;1 bytes: room map for body-sensor
setblookS
incR0
jmpp_exit
do2:movleng,#3;1 syn,1 comd,1 args, 1 sum(experimental)
setbrtemprS
incR0
jmpp_exit
do3:movleng,#3;
setbwvalveS
incR0
jmpp_exit
;;system command
do4:movleng,#2
setbiddleS
jmpp_exit
do5:movleng,#2
setbrebooS
jmpp_exit
do6:movleng,#2
setbcheckS
jmpp_exit
p_exit:;movcmd,SBUF;save a copy of cmd(for rescue-mode...)
RET
cheksum:;Р£СйºНº¯Кэ(appriciate R2)
movB,R2
;R0 at:cmd+arg+sum
decR0
movR2,leng
decR2;leng-1=cmd+args to be calculated
clrC
movA,#0;as SYN=0 ;-)
plus:ADDCA,@R0
decR0
djnzR2,plus
incR0;reset at: cmd
xrlA,sum;compare to checksum received
movsum,A;zero or Non-zero: right or wrong
movR2,B
ret
;;------------------I.N.I.T-----------------------------------------------
startup:;;infrastructure initial
movPCON,#80h;²ЁМШВК±对ц
movSCON,#0d0h;ЙиЦГ´®РР·½К½3£REN=1Н²½=4800££Ё12M¾§Хс£©
movTMOD,#22h
movTL1,#0f3h
movTH1,#0f3h
MOVTL0,#0FFH
MOVTH0,#0FFH;dead-loop resolution ready
setbTR1
;;run-time initial
data_init:
movSP,#70h;for call's parameters
mov20h,#0
mov21h,#0;clear bit flag
movR0,#cmd
movseq,#0
movR1,#tempr
movDPTR,#msg
;*******
;;let's go~~
;setbES
;setbET0
;setbEA
;jmp main
hello:;I'm alive! only once when startup
movA,#0
MOVCA,@A+DPTR
movB,A
xrlA,#'
;is it end?
jzlearn
MOVSBUF,B
incDPTR
jnbTI,$
clrTI
jmphello
learn:;lcallsearch_ds;search how many slaves on the DS18B20 bus
;movsbuf,arg;told how many slaves alive(1 byte)
go0:;;let's go~~
setbES
setbET0
setbEA
;;-------------------M.A.I.N-----------------------------------------
;; scatter-list modeling
main:jbdirt,exam0
jbdone,exam1
jbtodo,exam2
rela:jmpmain
r_rte:jmprte
r_wva:jmpwva
exam0:clrES;cut off communic with outside
jmpdata_init;emergency reset
exam1:clrdone;task done!
clrP1.0
jmpmain
exam2:clrtodo;task todo!
jbrebooS,reb
jbiddleS,idd
jbcheckS,che
jblookS,loo
jbrtemprS,r_rte
jbwvalveS,r_wva
jmpmain
;;system command
reb:clrrebooS;reboot state engaged 10 seconds after msg incoming
clrP1.7
mov0a6h,#01eh;start watchdog timer
mov0a6h,#0e1h
movA,#0
jz$;down soon...
idd:clriddleS;iddle state engaged 2 seconds after msg incoming
clrP1.6
movtmp,#10
d2s:lcalld200ms
djnztmp,d2s
orlPCON,#05h
nop;...
lcalld200ms;I'm awake!
movP2,#0
lcalld200ms
movP2,#0ffh
setbP1.6
jmpmain
;;data output
che:clrcheckS
clrP1.5;roger;-)
movleng,#1;copy over!
LCALLINIT_1820;check DS18B20
jbflag1,main
movsbuf,#0ffh;some slave(s) dead,but no spec
;
setbP1.5
jmpmain
;;presence data request
loo:jbmutexb,rela;leave locked bufferrrrrrrrrrrrrrrrrrrrrrrrrrrr
setbmutexb
clrlookS
clrP1.4
movleng,#1
;movsbuf,P2
;jbTI,$;will be soon
movC,body
rlcA
jbreporting,cancel
setbreporting
movSBUF,A
movtbuf,A
lcalld200ms
lcalld200ms
setbP1.4
jmpmain
;;tempreture data request
rte:jbmutexb,relay
setbmutexb
clrrtemprS
clrP1.3
movleng,#8;roger;-)
movR5,#8; 1 room (experimental)
movR1,#tempr
decR1
movsnp,#sn
decsnp
rget:;; translate the room number to 64-bit DS18B20 internal code
incR1
incsnp;SN will be use in GET_TEMPER
clrC
movA,arg;the "map"
rrcA
movarg,A
jncrget
clrES
LCALLGET_TEMPER
setbES
lcalldisplay;indicate what's read
movA,arg
jnzrget
; send data DMA
movR1,#tempr
jbreporting,cancel;cancel current task
setbreporting
movSBUF,@R1
movtbuf,@R1;save a copy
setbP1.3
jmpmain
;; data input
wva:jbmutexb,relay
setbmutexb
clrwvalveS
clrP1.2
movleng,#1
;lcallvalve;;;;;;;;;;;;;;;
setbP1.2
relay:jmpmain
cancel:jmpdata_init
;;------------------M.I.C.R.O --------------------------------------------------
d200ms:;СУК±200MS(uninterrruptable!!!)
MOVR5,#20
d1:MOVR6,#20
d2:MOVR7,#250
DJNZR7,$
DJNZR6,d2
DJNZR5,d1
RET
;;-------------------DS18B20(R1,R2,R3,R4)-------------------------------------------------
GET_TEMPER:;读³цЧª»»ºуµДОВ度Цµ
LCALLINIT_1820;ПИё´О»DS18B20
JBFLAG1,step2;ЕР断DS1820КЗ·с´жФЪ?ИфDS18B20²»´жФЪФт·µ»Ш
RET
step2:;if need to address...
;movB,R0
;movR0,snp
;movA,@R0
;lcallWRITE_1820
;movR0,B
MOVA,#0CCH; Мш¹эROMЖ¥Ед
LCALLWRITE_1820
MOVA,#44H; ·¢³цОВ度Чª»»ГьБо
LCALLWRITE_1820
movR2,#3;СУК±Т»段К±¼д,µИ´эADЧª»»½бКш,12О»µД»°750О¢Гл
zz2:movR3,#250
djnzR3,$
djnzR2,zz2
LCALLINIT_1820;Ч¼±ё读ОВ度З°ПИё´О»
;if need to address...
;movB,R0
;movR0,snp
;movA,@R0
;lcallWRITE_1820
;movR0,B
MOVA,#0CCH;Мш¹эROMЖ¥Ед
LCALLWRITE_1820
MOVA,#0BEH;·¢³ц读ОВ度ГьБо
LCALLWRITE_1820
LCALLread_temp;½«读³цµДОВ度Кэ¾Э±£´жµ½35H/36H
RET
INIT_1820:;ХвКЗDS18B20ё´О»³хК¼»¯ЧУ³МРт
setbDQ;pull high DQs
clrDQ;pull low DQs
MOVR4,#239;Цч»ъ·¢³цСУК±480О¢ГлµДё´О»µНВц³е
DJNZR4,$
setbDQ;И»ºуАЯКэ¾ЭПЯ
movR4,#28;wait 15~60us
djnzR4,$
MOVR4,#20;µИ´эDS18B20»ШУ¦,delay 100us(60us~240us)
de1:jnbDQ,presen
djnzR4,de1
jmpabsen
presen:SETBFLAG1;ЦГ±кЦ¾О»,±нК¾DS1820´жФЪ
MOVR4,#50;delay 225us
DJNZR4,$
setbDQ
ret
absen:mov@R1,#0ffh
CLRFLAG1;Зе±кЦ¾О»,±нК¾DS1820²»´жФЪ
setbDQ
RET
WRITE_1820:;Р´DS18B20µДЧУ³МРт(УР¾ЯМеµДК±РтТªЗу)
MOVR2,#8;Т»¹²8О»Кэ¾Э
CLRC
WR1:CLRDQ;as a write slot
MOVR3,#6
DJNZR3,$
RRCA;within 15us write "0" or "1"
MOVDQ,C;last for 60us-15us
MOVR3,#23
DJNZR3,$
SETBDQ;release
NOP;at least 1us recovery time
DJNZR2,WR1
SETBDQ
RET
read_temp:;读DS18B20µД³МРт,´УDS18B20ЦР读³цБ½ёцЧЦ½ЪµДОВ度Кэ¾Э
movR4,#2
rer:lcallread16_1820
MOV@R1,A;save temperature low byte
incR1
djnzR4,rer
decR1
decR1;reset data pointer at low byte
incR1
movA,@R1
rrcA;get lowest bit of temprH
decR1
movA,@R1
RRCA
mov@R1,A;append lowest bit of temprH
;И¥µфЧоµНБ½О»ºНЧоёЯБщО»£±£БфЦР¼д°ЛО»ЧчОªУРР§Цµ£ЧоёЯОВ度63.75度£ЧоµНОВ度-64.75度
;¾«度£º0.25度
;;translate to Int6:decl2
incR1
movA,@R1
rrcA
rrcA
decR1
movA,@R1
RRCA
mov@R1,A
RET
read16_1820:;读DS18B20Т»ёцbyte
MOVR2,#8;Кэ¾ЭТ»¹²УР8О»
reL:CLRDQ;as a read slot:pull low
CLRC
NOP
SETBDQ;release after 2us
MOVR3,#6
DJNZR3,$
MOVC,DQ;sample within 15us after pull-low
MOVR3,#23;slot last 60us
DJNZR3,$;then least 1us recovery time
RRCA
DJNZR2,reL
ret
display: ;ПФК¾ЧУ³МРт
movA,@R1
clrC
rrcA
clrC
rrcA;И¥µфР¡Кэ²¿·Ц2О»
movB,#10 ;½«temprЦРµДК®Бщ½шЦЖКэЧª»»³Й10½шЦЖ
clrC
divab
movbit4,A;К®О»ФЪa
movbit3,B;ёцО»ФЪb
movDPTR,#numtab ;Цё定²й±нЖфК¼µШЦ·
movA,bit4;И¡ёцО»Кэ
MOVCA,@A+DPTR;²йК®О»КэµД7段´ъВл
movbit4,A
movA,bit3;И¡ёцО»Кэ
MOVCA,@A+DPTR;²йёцО»КэµД7段´ъВл
movbit3,A
movA,@R1
anlA,#03h
movR2,A
movDPTR,#dec1tab;ПФК¾К®·ЦО»
movcA,@A+DPTR
movbit2,A
movA,R2
movDPTR,#dec2tab;ПФК¾°Щ·ЦО»
movcA,@A+DPTR
movbit1,A
movR2,#4
dr2:movR3,#100;ПФК¾10´О;;;;;;;;;;;;;;;;
dr3:movP0,bit1
clrP2.6
acalld15us
setbP2.6
movP0,bit2
clrP2.5
acalld15us
setbP2.5
movP0,#0DFh;ПФК¾Р¡Кэµг
clrP2.4
acalld15us
setbP2.4
movP0,bit3
clrP2.4
acalld15us
setbP2.4
movP0,bit4
clrP2.3
acalld15us
setbP2.3
djnzR3,dr3
djnzR2,dr2;total time cusume:>750ms
ret
d15us:;1MSСУК±(°´12MHZЛг)
MOVR4,#5
DJNZR4,$
RET
numtab: db 28h,7eh,0a2h,62h,74h,61h,21h,7ah,20h,60h
;0 1 2 3 4 5 6 7 8 9
dec1tab:db 28h,0a2h,61h,7ah
;0 2 5 7
dec2tab:db 28h,61h,28h,61h
;0 5 0 5
msg :db "I am alive!",0dh,0ah,'
;------------------------------------------------------------------------
end