|
做RS485通讯,写了个调试软件,测试PC--RS232转485模块-MCU通讯是否正常
list p=16f77
#include"p16f77.inc" RECLEN equ 29h ;receive data count
REC1 equ 30h ;receive data buffer
REC2 equ 31h
REC3 equ 32h
REC4 equ 33h
REC5 equ 34h
REC6 equ 35h
REC7 equ 36h
REC8 equ 37h
TRANLEN equ 39h ;send data count
TRAN1 equ 40h ;send data buffer
TRAN2 equ 41h
TRAN3 equ 42h
TRAN4 equ 43h
TRAN5 equ 44h
TRAN6 equ 45h
TRAN7 equ 46h
TRAN8 equ 47h
WBAK equ 70h
BSTA equ 71h
BPCH equ 72h
BFSR equ 73h
ICOM equ 79h
ICOM1 equ 7ah
org 0
nop
goto main
org 04h
intr:
movwf WBAK
swapf STATUS,0
movwf BSTA
bcf STATUS,RP0
bcf STATUS,RP0
bcf STATUS,IRP
movf FSR,0
movwf BFSR
movf PCLATH,0
movwf BPCH
bcf PCLATH,3
bcf PCLATH,4
btfsc PIR1,RCIF
goto rcint
btfsc PIR1,TXIF
goto txint
intback:
movf BPCH,0
movwf PCLATH
movf BFSR
movwf FSR
swapf BSTA,0
movwf STATUS
swapf WBAK,1
swapf WBAK,0
retfie
;-----------------------
rcint:
movf RCSTA,0
movwf ICOM
movf RCREG,0
movwf ICOM1
btfsc ICOM,2 ;帧错误
goto frdeal
btfsc ICOM,1 ;溢出错?
goto oedeal
movlw REC1
addwf RECLEN,0
movwf FSR
movf ICOM1,0
movwf INDF
xorlw 0aah ;收到AAH,关闭接收,启动发送
btfss STATUS,Z
goto rec1
clrf RECLEN
bcf RCSTA,4
clrf TRANLEN
bsf PORTC,5
bsf STATUS,RP0
bsf TXSTA,TXEN ;允许发送
bcf STATUS,RP0
goto intback
rec1:
incf RECLEN
movlw 8
subwf RECLEN,0
btfsc STATUS,C
clrf RECLEN
goto intback
oedeal:
bcf RCSTA,4
nop
bsf RCSTA,4
frdeal:
bcf RCSTA,2
goto intback
;------------------------
txint:
movlw TRAN1
addwf TRANLEN,0
movwf FSR
movf INDF,0
movwf TXREG
incf TRANLEN
movlw d'8'
subwf TRANLEN,0
btfss STATUS,C
goto intback
bsf STATUS,RP0
wait:
btfss TXSTA,TRMT ;在关闭发送前,确定最后的数据发送结束
goto wait
bcf STATUS,RP0
clrf TRANLEN
bsf STATUS,RP0
bcf TXSTA,5 ;禁止发送
bcf STATUS,RP0
clrf RECLEN
bcf PORTC,5 ;允许接收
bsf RCSTA,4
goto intback
;--------------------
main:
bsf STATUS,RP0
clrf TRISD
bcf TRISC,6
bsf TRISC,7
bcf TRISC,5
movlw 67H
movwf SPBRG
bsf TXSTA,2 ;高速通信
bsf PIE1,RCIE
bsf PIE1,TXIE
bcf STATUS,RP0
movlw H'90'
movwf RCSTA
bcf PORTC,5 ;允许发送
clrf RECLEN
clrf TRANLEN
movlw 51h
movwf TRAN1
movlw 52h
movwf TRAN2
movlw 53h
movwf TRAN3
movlw 54h
movwf TRAN4
movlw 55h
movwf TRAN5
movlw 56h
movwf TRAN6
movlw 57h
movwf TRAN7
movlw 58h
movwf TRAN8
bsf INTCON,GIE
bsf INTCON,PEIE
goto $
;--------------------
end
|
|