这是我编的用小键盘(8279)控制步进电机的汇编程序
- Z8279 EQU 212H
- D8279 EQU 210H
- D8251 EQU 228H
- Z8251 EQU 22aH
- LEDMOD EQU 00 ;左边输入,八位显示外部译码八位显示
- LEDFEQ EQU 38H ;扫描频率
- STACK SEGMENT STACK
- STA DW 512 DUP (?)
- TOP EQU LENGTH STA
- STACK ENDS
- DATA SEGMENT
- LED DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH,77H,7CH,39H
- DB 5EH,79H,71H
- MESS1 DB 0DH,0AH,0DH,0AH
- DB ' Speed-----Press 1~F',0DH,0AH
- DB ' Direction-----Press + OR -',0DH,0AH
- DB ' ESC-----Press 0',0DH,0AH,'
- MESS2 DB 0dh,' Dir Now----','
- SPDLY DB 1
- DIRE DB '+'
- DATA ENDS
- CODE SEGMENT
- ASSUME CS:CODE,DS:DATA,SS:STACK
- START: MOV AX,STACK ;初始化
- MOV SS,AX
- MOV SP,TOP
- MOV AX,DATA
- MOV DS,AX
- MOV DX,20BH ;8255初始化
- MOV AL,80H
- OUT DX,AL
- MOV CX,100
- LOOP $
- MOV CX,05H
- NEXT:PUSH CX
- MOV DX,Z8251 ;初始化8251
- MOV AL,00H
- OUT DX,AL
- MOV CX,04H
- LOOP $
- OUT DX,AL
- MOV CX,04H
- LOOP $
- OUT DX,AL
- MOV CX,04H
- LOOP $
- MOV AL,40H
- OUT DX,AL
- MOV CX,100
- LOOP $
- MOV AL,0CEH ;异步方式,设置为8个数据位,两个停止位波特率因子为16
- OUT DX,AL ;无奇偶校验位
- MOV CX,100
- LOOP $
- MOV AL,25H ;设为请求发送,允许发送,允许接收状态
- OUT DX,AL
- MOV CX,100
- LOOP $
- POP CX
- LOOP NEXT
- NOP
- MOV DX,Z8279
- MOV AL,LEDMOD
- OUT DX,AL
- MOV AL,LEDFEQ
- OUT DX,AL
- MOV DX,D8279
- MOV AL,7FH
- OUT DX,AL
- MOV DX,D8279
- MOV AL,7FH
- OUT DX,AL
- MOV DX,D8279
- MOV AL,7DH
- OUT DX,AL
- MOV DX,D8279
- MOV AL,7FH
- OUT DX,AL
- MOV DX,D8279
- MOV AL,70H
- OUT DX,AL
- MOV DX,D8279
- MOV AL,5EH
- OUT DX,AL
- MOV DX,D8279
- MOV AL,79H
- OUT DX,AL
- MOV DX,D8279
- MOV AL,77H
- OUT DX,AL ;以上为写(AEDK8688)
- NOP
- BG:MOV DX,Z8279
- MOV AL,0C2H
- OUT DX,AL ;置空FIFO寄存器
- PUSH DX
- LEA DX,MESS1 ;显示信息1
- MOV AH,09H
- INT 21H
- LEA DX,MESS2
- MOV AH,09H
- INT 21H
- MOV DL,DIRE ;显示方向信息
- MOV AH,06H
- INT 21H
- POP DX
- WAIIT:MOV AH,0BH
- INT 21H
- CMP AL,0FFH;有键输入则为0FFH
- JNE WAT;无键入转
- L3:MOV AH,08H
- INT 21H
- L4:CMP AL,'+'
- JE L0
- CMP AL,'-'
- JE L1
- CMP AL,'9'
- JNA DA
- SUB AL,37H
- CMP AL,0AH
- Jb WAIIT
- CMP AL,0FH
- Ja WAIIT
- JMP JIAN
- DA: SUB AL,30H
- CMP AL,00H
- JB WAT
- JMP PAN
- WAT:NOP
- IN AL,DX
- MOV BL,AL
- AND AL,80H
- CMP AL,80H
- JE WAT ;FIFO正在清除期间则跳转等待
- MOV AL,BL
- AND AL,0FH
- CMP AL,00H
- JE WAIIIT ;无键按下转
- BG3:MOV DX,Z8279
- MOV AL,40H ;写 读FIFO RAM命令字
- OUT DX,AL
- MOV DX,D8279 ;读入FIFO RAM内容
- IN AL,DX
- NOP
- JMP PAN
- WAIIIT:PUSH DX
- MOV DX,Z8251
- MOV AH,02H
- IN AL,DX ;有数据送到吗?
- AND AL,AH
- JZ WAIIT ;读入寄存器空,没有数据送到则跳转至发送
- L5:MOV DX,D8251 ;有数据送到则从数据缓冲区读入
- IN AL,DX
- JMP L4
- PAN:CMP AL,00H ;是结束符则结束
- JZ AK
- JIAN:MOV BL,AL ; 查表,将键码放入AL中
- CMP AL,0fH;速度不在1~15之间
- JA BG1
- MOV SPDLY,AL;
- JMP BB0
- L4:JMP BG
- BG1: CMP AL,13H ;方向控制键
- JE L0
- CMP AL,12H
- JNE L4
- L1:MOV DIRE,'-'
- JMP BG2
- L0:MOV DIRE,'+'
- BG2:push dx
- LEA DX,MESS2
- MOV AH,09H
- INT 21H
- MOV DL,DIRE;显示方向信息
- MOV AH,06H
- INT 21H
- POP DX
- MOV DX,Z8279 ;清FIFO RAM寄存器
- MOV AL,0C2H
- OUT DX,AL
- JMP BB1
- L2:JMP L3
- BB0: mov cx,08h
- mov al,00h
- clear: MOV DX,D8279
- out dx,al
- loop clear
- mov dx,z8279
- mov al,83h
- out dx,al
- MOV AL,BL
- LEA BX,LED
- XLAT
- MOV DX,D8279 ;将AL中内容写到数码管上
- OUT DX,AL
- MOV DX,Z8279 ;清FIFO RAM寄存器
- MOV AL,0C2H
- OUT DX,AL
- JMP BB1
- AK: JMP FINI
- BG4:JMP BG3
- L6:JMP L5
- BB1: MOV AH,0BH ;判断PC机是否有键按下
- INT 21H
- CMP AL,0FFH
- JE L2
- MOV DX,Z8279
- IN AL,DX
- NOP
- AND AL,0FH
- CMP AL,00H
- JNE BG4
- MOV DX,Z8251
- MOV AH,02H
- IN AL,DX ;有数据送到吗?
- AND AL,AH
- JNZ L6
- XOR CX,CX
- MOV CL,SPDLY
- CALL DELAY
- MOV DX,208H
- MOV AL,00000001B
- OUT DX,AL
- CMP DIRE,'+' ;控制转向
- JNE BB2
- XOR CX,CX
- MOV CL,SPDLY
- CALL DELAY
- MOV DX,208H
- MOV AL,00000010B
- OUT DX,AL
- XOR CX,CX
- MOV CL,SPDLY
- CALL DELAY
- MOV DX,208H
- MOV AL,00000100B
- OUT DX,AL
- XOR CX,CX
- MOV CL,SPDLY
- CALL DELAY
- MOV DX,208H
- MOV AL,00001000B
- OUT DX,AL
- JMP BB1
- BB2:XOR CX,CX
- MOV CL,SPDLY
- CALL DELAY
- MOV DX,208H
- MOV AL,00001000B
- OUT DX,AL
- XOR CX,CX
- MOV CL,SPDLY
- CALL DELAY
- MOV DX,208H
- MOV AL,00000100B
- OUT DX,AL
- XOR CX,CX
- MOV CL,SPDLY
- CALL DELAY
- MOV DX,208H
- MOV AL,00000010B
- OUT DX,AL
- JMP BB1
- FINI: MOV DX,Z8279 ;清FIFO寄存器并关蜂鸣器
- MOV AL,0C2H
- OUT DX,AL
- MOV AH,4CH
- INT 21H
- DELAY PROC NEAR
- PUSH CX
- MOV CX,80 ;延时子程序
- DELAY1: PUSH CX
- MOV CX,2000H
- LOOP $
- POP CX
- LOOP DELAY1
- POP CX
- LOOP DELAY
- RET
- DELAY ENDP
- CODE ENDS
- END START
|