;基于串口通讯的子程序调试软件
;大强制作
;nie_zq@163.net

RX0         EQU     10H
RX1         EQU     11H
RX2         EQU     12H
RX3         EQU     13H
RX4         EQU     14H
TX0         EQU     15H
TX1         EQU     16H
TX2         EQU     17H
TX3         EQU     18H
TX4         EQU     19H
PARAM0      EQU     11H
PARAM1      EQU     12H
PARAM2      EQU     13H
PARAM3      EQU     14H
RET0        EQU     16H
RET1        EQU     17H
RET2        EQU     18H
RET3        EQU     19H


;---------------------------------------------------
            ORG     0000H
            SJMP    INIT_COM
            ORG     0023H
            AJMP    COM_INT
            ORG     0030H
INIT_COM:   SETB    P3.0
            SETB    P3.1
            MOV     IP,#00H
                             ;SCON: SM0|SM1|SM2|REN|TB8|RB8|TI|RI
            MOV     SCON,#11010000B ; 1   1   0   1   0   0  0  0
            ANL     PCON,#01111111B
            MOV     TCON,#00H
            MOV     IE,#00H
            MOV     TH1,#0FFH;波特率28800*TMOD
            MOV     TL1,#0FFH
                              ;TMOD: GATE|C/!T|M1|M0|GATE|C/!T|M1|M0
            MOV     TMOD,#00100000B;   0    0  1   0   0    0   0  1
            SETB    TR1
            SETB    ES
            SETB    EA
            MOV     SP,#50H
;------------------------------------------------
            CLR     P3.3;Vpp=5V
            CLR     P3.4
            SJMP    $

;------------------------------------------------
;           在下面添加被调试的子程序入
;------------------------------------------------
PROCESS_1:  CLR     P3.3;Vpp=0v
            SETB    P3.4
            MOV     TX1,#0;显示0v
            RET

PROCESS_2:  CLR     P3.3;Vpp=5V
            CLR     P3.4
            MOV     TX1,#5;显示5v
            RET

PROCESS_3:  CLR     P3.4;Vpp=12v
            SETB    P3.3
            MOV     TX1,#12;显示12v
            RET

PROCESS_4:  
            RET

PROCESS_5:  
            RET

PROCESS_6:  
            RET

;--------------------------------------------------------------------------
;
;--------------------------------------------------------------------------

DELAY:      MOV     R6,#5
            MOV     R5,#5
            DJNZ    R6,$
            DJNZ    R5,$
            DJNZ    R7,DELAY
            RET

TX_PC:      CLR     ES		;发送5个字节
            CLR     TI
            MOV     SBUF,TX0
            JNB     TI,$
            CLR     TI
            MOV     SBUF,TX1
            JNB     TI,$
            CLR     TI
            MOV     SBUF,TX2
            JNB     TI,$
            CLR     TI
            MOV     SBUF,TX3
            JNB     TI,$
            CLR     TI
            MOV     SBUF,TX4
            JNB     TI,$
            CLR     TI
            SETB    ES
            RET

RX_PC:      CLR     ES	        ;接收5个字节
            JNB     RI,$
            MOV     RX0,SBUF
            MOV     TX0,RX0
            CLR     RI
            JNB     RI,$
            MOV     RX1,SBUF
            CLR     RI
            JNB     RI,$
            MOV     RX2,SBUF
            CLR     RI
            JNB     RI,$
            MOV     RX3,SBUF
            CLR     RI
            JNB     RI,$
            MOV     RX4,SBUF
            CLR     RI
            SETB    ES
            RETI

COM_INT:    PUSH    A          ;串口中断服务
            PUSH    00H
            JNB     TI,RX_INT
            POP     00H
            POP     A
            RETI

RX_INT:     LCALL   RX_PC
            MOV     A,RX0	;接收缓冲中的第一个字节是命令,后4个是参数

            CJNE    A,#00H,IS_WAIT;00表示设置引脚电平
            LCALL   UPDATE_PINS
            SJMP    TX_RETS

IS_WAIT:    CJNE    A,#0FFH,AA   ;FF表示暂停，显示程序指针
WAIT_GO:    POP     00H
            POP     A
            POP     TX2
            POP     TX1
            PUSH    TX1
            PUSH    TX2
            PUSH    A
            PUSH    00
            ACALL   WAIT
            POP     00
            POP     A
            RET

AA:         CJNE    A,#01H,BB	;根据RX0调用相应的子程序，RX1-RX4是传递
            LCALL   PROCESS_1	;给子程序的参数
            SJMP    TX_RETS
BB:         MOV     A,RX0
            CJNE    A,#02H,CC
            LCALL   PROCESS_2
            SJMP    TX_RETS
CC:         MOV     A,RX0
            CJNE    A,#03H,DD
            LCALL   PROCESS_3
            SJMP    TX_RETS
DD:         MOV     A,RX0
            CJNE    A,#04H,EE
            LCALL   PROCESS_4
            SJMP    TX_RETS
EE:         MOV     A,RX0
            CJNE    A,#05H,FF
            LCALL   PROCESS_5
            SJMP    TX_RETS
FF:         MOV     A,RX0
            CJNE    A,#06H,TX_RETS
            LCALL   PROCESS_6
TX_RETS:    LCALL   TX_PC      ;将处理结果送电脑显示出来
            SETB    ES
            SJMP    COM_INT_RETI
COM_INT_RETI:
            POP     00H
            POP     A
            RET


WAIT:       PUSH    A		;进入等待循环，直到受到RX0=0FEH才返回
RX_GO:      MOV     TX0,#0FFH
            ACALL   TX_PC
            ACALL   RX_PC
            MOV     A,RX0
            CJNE    A,#0FEH,RX_GO
            MOV     TX0,#0FEH
            ACALL   TX_PC
            POP     A
            RET

BREAK:      POP     TX2		;获得PC指针
            POP     TX1
            PUSH    TX1
            PUSH    TX2
            ACALL   WAIT
            RET


UPDATE_PINS:MOV     P0,RX1	;设置引脚电平
            MOV     P1,RX2
            MOV     P2,RX3
            ORL     RX4,#00000011B;不能改变串口通讯的引脚
            MOV     P3,RX4
            MOV     TX1,P0
            MOV     TX2,P1
            MOV     TX3,P2
            MOV     TX4,P3
            RET

