|
单片机步进电机课程设计
可实现调速以及正反转等功能
- IOY0 EQU 3000H ;片选IOY0 对应的端口始地址
- MY8255_A EQU IOY0+00H*4 ;8255 的 A 口地址,输出,键盘列选
- MY8255_B EQU IOY0+01H*4 ;8255 的 B 口地址,输出,数码管编码
- MY8255_C EQU IOY0+02H*4 ;8255 的 C 口地址,低四位,读键盘行号,高四位输出,控制步进电机
- MY8255_MODE EQU IOY0+03H*4 ;8255的控制寄存器地址
- STACK1 SEGMENT STACK ;定义一个栈区
- DW 256 DUP(?) ;开辟出256个字单元空间
- STACK1 ENDS
- DATA SEGMENT
- DTABLE1 DB 6DH,79H,73H,77H,39H,06H,5BH,4FH,66H,80H ;这十个分别表示的数码管显示是 5,E,P,A,C,1,2,3,4,.
- DTABLE3 DB 10H,30H,20H,60H,40H,0C0H,80H,90H ;歩进电机正向转速表
- DATA ENDS
- CODE SEGMENT
- ASSUME CS:CODE,DS:DATA
- START: MOV AX,DATA
- MOV DS,AX
- MOV SI,3000H;建立缓冲区,存放要显示的键值
- ;[SI][SI+2][SI+4][SI+6]中分别存放四个数码管应显示的编码在DTABLE1中对应的位置
- MOV BYTE PTR [SI],00H;内容s
- MOV BYTE PTR [SI+2],09H;内容.
- MOV BYTE PTR [SI+4],04H;内容c
- MOV BYTE PTR [SI+6],05H;内容1
- MOV BYTE PTR [SI+8],01H;内容E
- MOV DX,MY8255_MODE;初始化8255工作方式
- MOV AL,81H;方式0,A输出(数码管片选)、B口输出(数码管)、C口低四位输入(按键),高四位输出(步进电机)
- OUT DX,AL
- KAISHI:
- MOV SI,3000H
- MOV DH,[SI+8]
- CMP DH,08H
- JA ZHI1;高于跳转
- CMP DH,01H
- JB ZHI8;不高于跳转
- JMP JIXU
- ZHI1: MOV BYTE PTR[SI+8],01H
- JMP JIXU
- ZHI8: MOV BYTE PTR[SI+8],08H
- JIXU: CALL KEYSCAN
- MOV AH ,1;判断是否有按键按下
- INT 16H ;键盘服务
- JZ SFRUN;等于0,读入字符,则跳转
-
- QUIT: MOV AX,4C00H
- INT 21H
-
- SFRUN:
- MOV SI,3000H
- MOV DH,[SI]
- CMP DH,01H
- JNZ JIXU;为0则跳转
- MOV DH,[SI+4]
- CMP DH,04H
- JNZ ZIJIAN;不为4则跳转
- INC BYTE PTR[SI+8] ;地址si+8处的字节变量加1
- JMP RUN
- KAISHI1: JMP KAISHI
- ZIJIAN :
- DEC BYTE PTR[SI+8]
- RUN:
- MOV AL,[SI+8]
- MOV BX,OFFSET DTABLE3
- AND AX,00FFH
- ADD BX,AX
- DEC BX
- MOV AL,[BX]
- MOV DX,MY8255_C
- OUT DX,AL
- DANG1: MOV DH,[SI+6]
- CMP DH,05H ;1
- JNZ DANG2
- PUSH CX;cx的值压入堆栈保存
- MOV CX,10H;循环10次
- DA1: CALL DALLY1 ;1档速度
- CALL DALLY
- CALL DALLY1
- CALL DALLY1
- CALL DALLY1
- CALL KEYSCAN
- JMP KAISHI
-
- DANG2: CMP DH,06H;2
- JNZ DANG3
- DA2: CALL DALLY1 ;2档速度
- CALL DALLY1
- CALL DALLY1
- JMP KAISHI
- DANG3: CMP DH,07H;3
- JNZ DANG4
-
-
- DA3: CALL DALLY1 ;3档速度
- CALL DALLY1
- JMP KAISHI
- DANG4: CMP DH,08H;4
- JNZ KAISHI1
- DA4: CALL DALLY1 ;4档速度
- CALL KEYSCAN
- JMP KAISHI
-
- DALLY1 PROC NEAR ;软件延迟子程序
- PUSH CX
- PUSH AX
- MOV CX,00AFH
- D11:MOV AX,1000H
- D22: DEC AX
- JNZ D22
- PUSH CX;进栈
- PUSH AX;进栈
- CALL KEYSCAN
- POP AX;出栈
- POP CX;出栈
- LOOP D11
- POP AX;出栈
- POP CX;出栈
- RET;返回
- DALLY1 ENDP
- KEYSCAN PROC NEAR;键盘扫描子程序
- BEGIN:
- CALL DIS;刷新
- CALL CLEAR;清屏
- CALL CCSCAN;扫描按键
- JNZ GETKEY1;有键按下则跳置 GETKEY1
- JMP GOBACK1
- GETKEY1: CALL DIS;显示刷新
- CALL DALLY
- CALL DALLY
- CALL CLEAR;清屏
- CALL CCSCAN;再次扫描按键
- JNZ GETKEY2;有键按下则跳置 GETKEY2
- JMP BEGIN;否则跳回开始继续循环
-
- GETKEY2: MOV CH,0FEH;设置当前检测的是第几列
- MOV CL,00H
-
-
- COLUM: MOV AL,CH;选取一列,将x1~x4中一个置0
- MOV DX,MY8255_A
- OUT DX,AL
- MOV DX,MY8255_C;读y1~y4,用于判断是哪一行按键闭合
- IN AL,DX
- L1: TEST AL,01H;是否为第 1 行
- JNZ L2;不是则继续判断
- MOV AL,00H;设置第 1 行第 1 列的对应的键值
- JMP KCODE
- BEGIN1: JMP BEGIN
- L2: TEST AL,02H;是否为第 2 行
- JNZ L3;不是则继续判断
- MOV AL,04H;设置第 2 行第 1 列的对应的键值
- JMP KCODE
-
- L3: TEST AL,04H;是否为第 3 行
- JNZ L4;不是则继续判断
- MOV AL,08H;设置第 3 行第 1 列的对应的键值
- JMP KCODE
-
- L4: TEST AL,08H;是否为第 4 行
- JNZ NEXT;不是则继续判断
- MOV AL,0CH;设置第 4 行第 1 列的对应的键值
-
- KCODE: ADD AL,CL;将第一列的值加上当前列数,等于按键值
- CALL PUTBUF;保存按键值
- PUSH AX
-
- KON:;显示刷新
- CALL DIS;等待按键弹起
- CALL CLEAR;清屏
- CALL CCSCAN;扫描按键,判断按键是否弹起
- JNZ KON;未弹起则继续循环等待弹起
- POP AX
- NEXT:
- INC CL;当前检测的列数递增
- MOV AL,CH
- TEST AL,08H;检测是否扫描到第 4 列
- JZ BEGIN1;是则跳回到开始处
- ROL AL,1;没检测到第 4 列则准备检测下一列
- MOV CH,AL
- JMP COLUM
- GOBACK1: RET
- KEYSCAN ENDP
-
- CCSCAN PROC NEAR;扫描是否有按键闭合子程序
- MOV AL,00H
- MOV DX,MY8255_A;将 4 列全选通,X1~X4 置 0
- OUT DX,AL
- MOV DX,MY8255_C
- IN AL,DX;读 Y1~Y4
- NOT AL
- AND AL,0FH;取出 Y1~Y4 的反值
- RET
- CCSCAN ENDP
- CLEAR PROC NEAR ;清除数码管显示子程序
- MOV DX,MY8255_B ;段位置0即可清除数码管显示
- MOV AL,00H
- OUT DX,AL
- RET
- CLEAR ENDP
- DIS PROC NEAR ;显示键值子程序
- PUSH AX ;AX入栈
- PUSH SI ;SI入栈,堆栈里保存了AX的0700,SI的3000
- MOV SI,3006H ;此处为3006H是先显示的第四个管,下面SI DEC两次即显示第三个数码管
- MOV DL,0F7H
- MOV AL,DL ;AL 0F7H
- AGAIN:
- PUSH DX ;DX入栈,堆栈里第一次是保存了DX的C4F7
- MOV DX,MY8255_A
- OUT DX,AL ;0F7H输出到A口,设置x1~x4,选通一个数码管
- MOV AL,[SI] ;SI指向一个缓冲区,取出缓冲区中存放键值在DTABLE1中的偏移量(05H)
- MOV BX,OFFSET DTABLE1 ;DTABLE1的偏移地址移入BX
- AND AX,00FFH ;AX与00FFH"按位与",和0相与清除,和1相与保留,保留了后8位
- ADD BX,AX ;DTABLE1的偏移地址加上键值的偏移量,即要输出的数的地址
- MOV AL,[BX]
- MOV DX,MY8255_B
- OUT DX,AL ;写入数码管A~Dp
- CALL DALLY ;过程嵌套,调用DALLY1过程
- DEC SI
- DEC SI ;取下一个键值
- POP DX ;DX出栈
- MOV AL,DL ;DL中存放是否显示完的标志
- TEST AL,01H ;判断是否显示完?
- JZ OUT1 ;显示完,返回
- ROR AL,1 ;AL循环右移一位
- MOV DL,AL ;存放DL,相当于DL循环右移一位
- JMP AGAIN ;未显示完,调回继续
- OUT1: POP SI ;SI出栈
- POP AX ;AX出栈
- RET
- DIS ENDP
- PUTBUF PROC NEAR ;保存子键子程序
- MOV SI,3000H
- CMP AL,00H
- JNZ MODE
- DIR:
- CMP BYTE PTR[SI+4],03H
- JZ DIR1
- MOV BYTE PTR[SI+4],03H
- JMP LAST
- DIR1:
- MOV BYTE PTR[SI+4],04H
- JMP LAST
- MODE:
- CMP AL,01H
- JNZ STOP
- CMP BYTE PTR[SI],01H
- JZ MODE1
- MOV BYTE PTR[SI],01H
- JMP LAST
- MODE1:
- MOV BYTE PTR[SI],02H
- JMP LAST
- STOP:
- CMP AL,02H
- JNZ GRD1
- MOV BYTE PTR[SI],00H
- MOV BYTE PTR[SI+4],04H
- MOV BYTE PTR[SI+6],05H
- GRD1:
- CMP AL,04H
- JNZ GRD2
- MOV BYTE PTR[SI+6],05H
- GRD2:
- CMP AL,05H
- JNZ GRD3
- MOV BYTE PTR[SI+6],06H
- GRD3:
- CMP AL,06H
- JNZ GRD4
- MOV BYTE PTR[SI+6],07H
- GRD4:
- CMP AL,07H
- JNZ LAST
- MOV BYTE PTR[SI+6],08H
- LAST:
- CALL DIS
- CALL CLEAR
- CALL CCSCAN
- GOBACK:
- RET
- PUTBUF ENDP
- DALLY PROC NEAR ;软件延迟子程序
- PUSH CX
- MOV CX,00FFH
- D1:
- MOV AX,00FFH
- D2:
- DEC AX
- JNZ D2
- LOOP D1
- POP CX
- RET
- DALLY ENDP
- CODE ENDS
- END START
复制代码
|
|