16f873;RS232硬件编程
RCDATAEQU30HRCFLAGEQU31H
RCOneSIGN EQU0
RCWrongEQU5
#INCLUDE <P16877.INC>
org 0
nop
goto start
org 4
intentr
goto intsever
;____________________________
org 9
start
clrf RCFLAG
clrf RCDATA
InitUart
bsfSTATUS,RP0
bcfRCFLAG,RCOneSIGN
movlw b'10111111' ;TX=output
movwf TRISC
movlw b'10100110'
movwf TXSTA
movlw 19h
movwf SPBRG;9600,high speed mode
bsfSTATUS,RP0
movlw b'00100000'
movwf PIE1
movlw b'11000000'
movwf INTCON
bcfSTATUS,RP0
movlw b'10010000'
movwf RCSTA
main
btfsc RCFLAG,RCOneSIGN
goto TXLoop
;btfss RCFLAG,RCWrong
goto main
TXLoop
bcfSTATUS,RP0
movf RCDATA,w
movwf TXREG
bsfSTATUS,RP0
btfss TXSTA,TRMT
goto $-2
bcfSTATUS,RP0
bcfRCFLAG,RCOneSIGN
goto main
;*****************************
intsever
movf STATUS,w
btfsc STATUS,RP0
bcfSTATUS,RP0
movwf RCFLAG
btfsc RCSTA,OERR
goto RCerror
movf RCREG,w
movwf RCDATA
bsfRCFLAG,RCOneSIGN
retfie
RCerror
bcfSTATUS,RP0
bcfRCSTA,CREN
movf RCREG,w
movf RCREG,w
movlw b'10010000'
movwf RCSTA
btfsc RCFLAG,RCWrong
bsfSTATUS,RP0
clrf RCFLAG
retfie
;----------------
end
注意,这个程序是先收再发的,如果接收不到数据那么发送也就不启动。
页:
[1]