star 发表于 2012-8-21 17:57:48

做RS485通讯,写了个调试软件,测试PC--RS232转485模块-MCU通...

做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
bcfSTATUS,RP0
bcfSTATUS,RP0
bcfSTATUS,IRP
movf FSR,0
movwf BFSR
movf PCLATH,0
movwf BPCH
bcfPCLATH,3
bcfPCLATH,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   
bcfRCSTA,4
clrf TRANLEN
bsfPORTC,5
bsfSTATUS,RP0
bsfTXSTA,TXEN ;允许发送
bcfSTATUS,RP0
goto intback
rec1:
incf RECLEN
movlw 8
subwf RECLEN,0
btfsc STATUS,C
clrf RECLEN
goto intback
oedeal:
bcfRCSTA,4
nop
bsfRCSTA,4
frdeal:
bcfRCSTA,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
bsfSTATUS,RP0
wait:
btfss TXSTA,TRMT;在关闭发送前,确定最后的数据发送结束
goto wait
bcfSTATUS,RP0
clrf TRANLEN
bsfSTATUS,RP0
bcfTXSTA,5;禁止发送
bcfSTATUS,RP0
clrf RECLEN
bcfPORTC,5;允许接收
bsfRCSTA,4
goto intback
;--------------------
main:
bsfSTATUS,RP0
clrf TRISD
bcfTRISC,6
bsfTRISC,7
bcfTRISC,5
movlw 67H
movwf SPBRG
bsfTXSTA,2;高速通信
bsfPIE1,RCIE
bsfPIE1,TXIE
bcfSTATUS,RP0
movlw H'90'
movwf RCSTA
bcfPORTC,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
bsfINTCON,GIE
bsfINTCON,PEIE
goto $
;--------------------

end

页: [1]
查看完整版本: 做RS485通讯,写了个调试软件,测试PC--RS232转485模块-MCU通...