pc机我采用串口助手,单片机at89c2051。目的,从pc机传命令“aa2425”(字符串)给单片机,单片机接收到数据后正确,上传50h(16进制),错误上传05h。 但是结果传送“aa4aaa"(61h61h34h61h61h61h ---16进制传)是正确的,程序如下,请高手指点一二。 ORG 0000h AJMP MAIN ORG 0023H AJMP SERVUART org 0030h MAIN: MOV PSW,#00H ;0区 默认 ;;;;;;;;;;;;;;设置T1;;;;;;;;;;;;;;;;;;;;;;;;;;;;; mov TMOD,#20H ;T1方式2 MOV TH1,#0FDH ;12MHZ,BOTERATE 9600,加倍192000 MOV TL1,#0FDH ;自动加载值 SETB TR1 ;启动定时器1 ,注意顺序
MOV SCON,#50H ;串口工作方式1,允许接收数据 MOV PCON,#00H ;SMOD=0不加倍
SETB EA ;开中断 SETB ES ;允许串口中断 MOV R1,#06H ;接收字节个数
HERE: SJMP HERE
ORG 0060H SERVUART: CLR EA PUSH PSW JBC RI,LOOP1 JBC TI,ENDT
LOOP1: mov A,R1 XRL A,#06H JZ RECEIVE1
XRL A,#05H JZ RECEIVE2
XRL A,#04H JZ RECEIVE3
XRL A,#03H JZ RECEIVE4
XRL A,#02H JZ RECEIVE5
XRL A,#01H JZ RECEIVE6 RECEIVE1: MOV A,SBUF CJNE A,#61H,ERROR SJMP NEXT RECEIVE2: MOV A,SBUF CJNE A,#61H,ERROR SJMP NEXT RECEIVE3: MOV A,SBUF CJNE A,#32H,ERROR SJMP NEXT RECEIVE4: MOV A,SBUF CJNE A,#34H,ERROR SJMP NEXT RECEIVE5: MOV A,SBUF CJNE A,#32H,ERROR SJMP NEXT RECEIVE6: MOV A,SBUF CJNE A,#35H,ERROR SJMP NEXT
ERROR: MOV A,#05H ;错误应答信号 MOV SBUF,A WAIT1: JBC TI,ENDT SJMP WAIT1
NEXT: DEC R1 ;不正确r1不减一,直到下一个数据对 CJNE R1,#00H,ENDT CLR P3.7 MOV A,#50H ;正确应答信号 MOV SBUF,A WAIT2: JBC TI,ENDT SJMP WAIT2
ENDT: POP PSW SETB EA RETI END
|