📄 other.asm
字号:
#include <P18F6520.INC>
#define OSC4M
#define IrTXD PORTA,0
#define IrRXD PORTB,0
GPRSDATA UDATA_ACS
SerialBuf res 1
delay_cnt1 res 1 ;只在ASM文件中定义了这两个变量。
delay_cnt2 res 1
cUseInINT res 1
rBitCounter res 1
EXTERN ByteReceived
EXTERN RevTemp
EXTERN IrTX9D
EXTERN Ir_ByteToSend
CODE
; ----------------------------------------------------
RB0INT ;软件模拟红外接收
GLOBAL RB0INT ; export so linker can see it
movf PORTB,W
call HalfBit ;延时半个bit,到达起始位的中心点
btfsc IrRXD ;1tc
goto SerialExit ;是干扰,退出
;仍为低:是起始位
;start bit
;延时1个bit,到达 bit0的中心点
call DelayForNextBitCenter
;bit0
clrf SerialBuf
movlw .8
movwf rBitCounter
SerialLoop:
btfsc IrRXD
goto SetBit1
bcf STATUS,C
goto SetBit2
SetBit1:
bsf STATUS,C
SetBit2:
rrcf SerialBuf,F
call DelayForNextBitCenter
decfsz rBitCounter,F
goto SerialLoop
;再延时1个bit,跳过偶校验位,到达停止位的中心点
call DelayForNextBitCenter
;;; btfss IrRXD
;SerialError:
;;; goto SerialExit
;received stop bit
;ReceiveOK:
;收到一字节
; bcf INTCON, INT0IF ;不清除INT0IF,这样在中断程序中就知道来的是红外还是485
movf SerialBuf,W
movwf RevTemp
return ; 如何转到串口1的正常接收流程里去??
SerialExit:
;停止位不为1,有错误,清除INT0IF退出
bcf INTCON, INT0IF ; Clear the RB interrupt flag.
return
; --------------------------------------------------------------------------
; Half Bit Time=833us/2=417us,417us/1.11746us=373tc
; HalfBit routine 的延时数=373-17=356c ;进入中断后经过17tc才调用Halfbit
; call =2 tc
; movlw =1 tc
; movwf =1 tc
; Delay Loop =351 tc(117x3)
; return =2 tc
; Total =356tc
; ------------------------------------------------------------------
; 如果是4M晶振,4分频后,1tc=1us
; Half Bit Time=833us/2=417us,417us/1us=417tc
; HalfBit routine 的延时数=417-17=400c ;进入中断后经过17tc才调用Halfbit
; call =2 tc
; movlw =1 tc
; movwf =1 tc
; Delay Loop =393 tc(131x3)
; nop =1 tc
; return =2 tc
; Total =400tc
HalfBit:
movlw D'131'
movwf cUseInINT
HalfBit1:
decfsz cUseInINT,F
goto HalfBit1
return
; --------------------------------------------------------------------------
; 3.579545M晶振,4分频后,1tc=1.11746us
; 1 BIT TIME=833us/1.11746us=745 tc
; FullBit routine 的延时数=745 tc
; call =2 tc
; movlw =1 tc
; movwf =1 tc
; Delay Loop =246*3=738tc
; nop =1 tc
; return =2 tc
; Total =745tc
; ---------------------------------------------------------
;
; 如果是4M晶振,4分频后,1tc=1us
; 1BIT需要等待的机器周期数 = 833us/1us=833 tc
; FullBit routine 的延时数=833 tc
; call =2 tc
; movlw =1 tc
; movwf =1 tc
; Delay Loop =275*3=825tc
; nop*2 =2 tc
; return =2 tc
; Total =833tc
; 275单字节装不下,用两次
; ---------------------------------------------------------
DelayForNextBitCenter: ;前面的语句消耗6tc
movlw D'200'
movwf cUseInINT
DFN1:
decfsz cUseInINT,F
goto DFN1
;耗掉 2+1+1+600=604,剩下833-604=229个tc
movlw D'75' ;1tc
movwf cUseInINT ;1tc
DFN2: ;227,刨掉return还有225,正好cUseInINT=75
decfsz cUseInINT,F
goto DFN2
return
; =================================
; 发送一字节子程序
; 入口:A 待发送字节
; 使用:SerialBuf,rBitCounter,rCounter
; 要求:发送前,应做以下准备
; 1.数据域的数据已加33H
; 2.关闭接收中断
; 3.关闭总中断
; 发送后,打开接收中断,打开总中断
;*****************************************************************************
; SEND SERIAL DATA
SendByte
GLOBAL SendByte
movlw .8
movwf rBitCounter
;发送起始位
bcf IrTXD
call DelayForNextBitCenter
;发送8bit数据
SendSDLoop:
btfsc Ir_ByteToSend,0
goto SetTX1
SetTX0:
bcf IrTXD
goto Loop_Judge
SetTX1:
bsf IrTXD
Loop_Judge:
call DelayForNextBitCenter
rrcf Ir_ByteToSend,F
decfsz rBitCounter,F
goto SendSDLoop
;发送偶校验位
movf IrTX9D,W ; 读出接收到的数据
btfss STATUS,Z ;Z=1?(bit9=0)
goto SB1
bcf IrTXD ;yes,bit9=0
goto SB2
SB1:
bsf IrTXD
SB2:
call DelayForNextBitCenter
;发送停止位
bsf IrTXD
call DelayForNextBitCenter
nop
return
; =================================
delay1ms
GLOBAL delay1ms ; export so linker can see it
movlw 0x01
goto DL0
delay2ms
GLOBAL delay2ms ; export so linker can see it
movlw 0x02
goto DL0
delay4ms
GLOBAL delay4ms ; export so linker can see it
DECF delay_cnt1
DECF delay_cnt1
movlw 0x05
goto DL0
delay10ms
GLOBAL delay10ms ; export so linker can see it
; 4MHz crystal,延时10.027ms,周期数 T=D10027
movlw D'13'
goto DL0
delay20ms
GLOBAL delay20ms ; export so linker can see it
movlw D'26'
goto DL0
delay50ms
GLOBAL delay50ms ; export so linker can see it
movlw D'65'
goto DL0
delay200ms
GLOBAL delay200ms ; export so linker can see it
movlw D'255'
DL0
movwf delay_cnt2
SETF delay_cnt1
DL1
DECF delay_cnt1
BNZ DL1
clrwdt
DECF delay_cnt2
BNZ DL1
nop
done
return
END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -