Code:
ASM
myint
btfss PIR1,0 ; timer1 o/flow
bra Data?
; keypress stuff here
EndKey:
; end of keypress code
bcf PIR1,0
bra end_int
Data?
btfss PIR1,5 ; is the received data flag set ?
goto dot
movf RCREG,w
movwf _RX_BUF ; save the received byte immediately
movwf TXREG ; Load the TX register immediately to pass the byte on
nop
x1:
btfss PIR1,TXIF ; Wait for TX send to complete
bra x1
; progrss register bits 0-3 get set as each element of the header
; is received. If a bad byte is detected then progress is reset
; and the code starts waiting for another $ sign
prog0 btfsc _RX_PROGRESS,0 ; has the progress,0 step been completed
bra prog1 ; yes, test for progress,1
movf _RX_BUF,w
xorlw '$' ; test if this byte is a $ sign
btfss STATUS,Z
goto dump ; not a $ sign, clear the progress register
bsf _RX_PROGRESS,0 ; no, set this bit now
movf TMR1L,w
subwf _randnum,f
goto out ; is a $ sign, don't clear progress, allow more
prog1 btfsc _RX_PROGRESS,1 ; same as above, validate the received data/header
bra prog2
movf _RX_BUF,w
xorlw 'B'
btfss STATUS,Z
goto dump
bsf _RX_PROGRESS,1
goto out
prog2 btfsc _RX_PROGRESS,2
bra prog3
movf _RX_BUF,w
xorlw '7'
btfss STATUS,Z
goto dump
bsf _RX_PROGRESS,2
goto out
; The fourth byte to be received is the 'type of message' character
; as for prog0 to prog2, if the fourth byte doesn't meet the test
; criteria of being a valid message type this data sentence is aborted
prog3 btfsc _RX_PROGRESS,3
bra prog4
clrf _M_type
clrf _M_type1
typep?:
movf _RX_BUF,w ; decimal points
xorlw 'P'
bnz typem?
bsf _M_type,0
bra duntype
typem?: ; message ascii digit
movf _RX_BUF,w
xorlw 'M'
bnz typef?
clrf _Disp_mode
bsf _M_type,1
bra duntype
typef?: ; font
movf _RX_BUF,w
xorlw 'F'
bnz typew?
BSF _M_type,3
bra duntype
typew?: ; UDC write command
movf _RX_BUF,w
xorlw 'W'
bnz typee?
bsf _M_type,2
bra duntype
typee?: ; effect
movf _RX_BUF,w
xorlw 'E'
bnz types?
bsf _M_type,4
bra duntype
types?: ; speed
movf _RX_BUF,w
xorlw 'S'
bnz typed?
bsf _M_type,5
bra duntype
typed?: ; digit delay
movf _RX_BUF,w
xorlw 'D'
bnz typev?
bsf _M_type,7
bra duntype
typev?:
movf _RX_BUF,w
xorlw 'V'
bnz typec?
bsf _M_type,6
bra duntype
typec?:
movf _RX_BUF,w ; clock function
xorlw 'C'
bnz typea?
bsf _M_type1,0
bra duntype
typea?:
movf _RX_BUF,w ; alarm function
xorlw 'A'
bnz typel?
bsf _M_type1,1
bra duntype
typel?:
movf _RX_BUF,w ; Letter play
xorlw 'L'
bnz typez?
bsf _M_type1,2
bra duntype
typez?:
movf _RX_BUF,w ; set total number of digits in array
xorlw 'Z'
bnz dump ;typenul?
bsf _M_type1,3
bra duntype
duntype:
bsf _RX_PROGRESS,3
clrf _RX_BYTE_CNT
lfsr 1,_RX_BUFFER
bra out
;'; If we got to here then the message type is valid.
;'; now to pick the correct data byte from the stream relevant to this device's
;'; enumerated position in the chain, and then save it for processing
prog4 ;duntype
capture_bits:
movff _RX_BUF,POSTINC1 ; capture all bytes of data
incf _RX_BYTE_CNT,f
inc_dun:
btfss _M_type,2 ; Is this a UDC write ?
bra typnot2
movf _RX_BYTE_CNT,w
xorlw 18
bz goodmes
bra out
; For all other, non- type 2 messages, count data bytes
; and when the current byte number equals this device's
; enumerated position put the current data byte in the buffer
typnot2
movf _RX_BUF,w
xorlw 13 ; Is this the final character of a message ?
bz goodmes
btfsc _M_type1,0 ; Clock command
bra out
btfsc _M_type1,3 ; Declare total number of tubes in array
bra out
btfsc _M_type1,1 ; Alarm command
bra out
btfsc _M_type1,2 ; FLW functions 'L' letter play
bra out
btfsc _M_type,7 ; Delay functions
bra out
btfsc _M_type,1 ; Message functions
bra out
ld_tube_dat:
movf _RX_BYTE_CNT,w
xorwf _Device,w
bnz dev2
movff _RX_BUF,_Tube_dat ; Tube_dat now has the correct data byte with respect
bra out ; to this device's position in the chain
dev2
movf _RX_BYTE_CNT,w
xorwf _Device+1,w
bnz dev3
movff _RX_BUF,_Tube_dat+1 ; Tube_dat+1 now has the correct data byte with respect
bra out ; to this device's position in the chain
dev3
movf _RX_BYTE_CNT,w
xorwf _Device+2,w
bnz dev4
movff _RX_BUF,_Tube_dat+2 ; Tube_dat+2 now has the correct data byte with respect
bra out ; to this device's position in the chain
dev4
movf _RX_BYTE_CNT,w
xorwf _Device+3,w
bnz out
movff _RX_BUF,_Tube_dat+3 ; Tube_dat+3 now has the correct data byte with respect
bra out ; to this device's position in the chain
goodmes
movf TMR4,w
subwf _randnum,w
movwf TMR4
bsf _control,7
dump
clrf _RX_PROGRESS
dot:
btfss INTCON,2 ;Did a timer0 overflow occur ?
bra out
bcf INTCON,2 ; do timer0 maintenance
bsf _control,1
movlw 160
movwf TMR0L
out:
end_int:
RETFIE FAST
endasm
Bookmarks