研究生
最后登录1970-1-1
在线时间 小时
注册时间2017-7-2
|
发表于 2017-10-24 19:54:23
|
显示全部楼层
贴一个我写的51的,秀一下
ORG 00H
AJMP START
ORG 0BH //定时器0中断入口
LJMP TIME0
ORG 023H
LJMP UART//串口中断入口
ORG 30H
START:
L_RD BIT P2.5 //选择并行或者串行端口
L_RS BIT P2.6 //读
L_RW BIT P2.7 //EN
L_CS BIT P3.2 //片选
L_REST BIT P3.3 //复位
COM EQU 68H
DAT EQU 69H
MOV SP,#70H
CLR L_REST
ACALL DELAY
CLR L_CS
SETB L_REST
LCALL DELAY
MOV COM,#0E2H
ACALL WRI
LCALL DELAY
MOV COM,#0A0H
ACALL WRI
MOV COM,#0C8H
ACALL WRI
MOV COM,#0A6H
ACALL WRI
MOV COM,#0A4H
ACALL WRI
MOV COM,#0A2H
ACALL WRI
MOV COM,#0F8H
ACALL WRI
MOV COM,#01H
ACALL WRI
MOV COM,#81H
ACALL WRI
MOV COM,#23H
ACALL WRI
MOV COM,#25H
ACALL WRI
MOV COM,#2FH
ACALL WRI
LCALL DELAY
MOV COM,#40H
ACALL WRI
MOV COM,#0AFH
ACALL WRI
LCALL DELAY
;以上是显示模块初始化
MOV R1,#08H
MOV A,#0B0H
LOOP1:
MOV COM,A
ACALL WRI
ACALL DELAY
MOV COM,#10H
ACALL WRI
ACALL DELAY
MOV COM,#00H
ACALL WRI
ACALL DELAY
MOV R3,#80H
LOOP2:
MOV DAT,#00H
ACALL WRD
DJNZ R3,LOOP2
ADD A,#01H
DJNZ R1,LOOP1
//以上是清屏程序
MOV SCON,#50H //串口设置为工作方式1
MOV TMOD,#21H //设置定时器1工作方式2 定时器0工作方式1,16位计数器
MOV PCON,#00H //波特率不加倍
MOV TH1,#0F3H //波特率2400
MOV TL1,#0F3H
SETB TR1 //定时器1启动
//设置定时器0
MOV TH0,#15H
MOV TL0,#0A0H //定时60ms
SETB TR0
SETB ET0 //定时器0中断允许
SETB ES //串口中断允许
SETB EA //总中断允许
;以上是串口设置
MOV 20H,#0AH
MOV 21H,#00H
MOV 22H,#0AH
MOV 23H,#01H
MOV 24H,#0AH
MOV 25H,#02H
MOV 6FH,#00H //定时器0计数
MOV R0,#20H //蛇尾y
MOV R1,#21H //蛇尾x
MOV 08H,#24H //蛇头y
MOV 09H,#25H //蛇头x
MOV 0CH,#04H //表示向右前进
MOV R4,#04H
MOV R5,#00H //表示要输入的字节
MOV 06H,#05H //苹果y
MOV 07H,#02H //苹果x
MOV A,#04H //表示向右前进
MOV DPTR,#RANDYX
MOV COM,#0B2H
ACALL WRI
MOV COM,#10H
ACALL WRI
MOV COM,#00H
ACALL WRI
MOV DAT,#30H
ACALL WRD
ACALL WRD
ACALL WRD
ACALL WRD
ACALL WRD
ACALL WRD
//贪吃蛇初始化
ACALL SETAPPLE
//循环扫描6FH,确定延时时间是否到240ms,如果是,执行处理程序
DECIDE:
MOV A,6FH
CJNE A,#03H,DECIDE
MOV 6FH,#00H
MOV A,R4
SETB RS0
SETB RS0
J_UP:
CJNE A,#01H,J_DOWN
CJNE @R0,#00H,$+5
AJMP OUT
DEC @R0
AJMP C_W
J_DOWN:
CJNE A,#02H,J_LEFT
CJNE @R0,#1FH,$+5
AJMP OUT
INC @R0
AJMP C_W
J_LEFT:
CJNE A,#03H,J_RIGHT
CJNE @R1,#00H,$+5
AJMP OUT
DEC @R1
AJMP C_W
J_RIGHT:
CJNE @R1,#3FH,$+5
AJMP OUT
INC @R1
AJMP C_W
;检测键值,跳到相应的处理程序
OUT:
AJMP $
;出局
C_W:
//检测当前蛇头位置上是否有苹果或自己
//检测是否有苹果
MOV A,@R0
CJNE A,06H,Z_J
MOV A,@R1
CJNE A,07H,Z_J
CJNE R0,#50H,$+5
MOV R0,#20H
CJNE R4,#01H,$+4
INC @R0
CJNE R4,#02H,$+4
DEC @R0
INC R0
INC R0
CJNE R1,#51H,$+5
MOV R1,#21H
CJNE R4,#03H,$+4
INC @R1
CJNE R4,#04H,$+4
DEC @R1
INC R1
INC R1
//蛇头向后移动两位
//将苹果放在蛇头
MOV @R0,06H
MOV @R1,07H
CLR RS0
CLR RS0
ACALL SETAPPLE
AJMP DECIDE
//检测是否有自己
Z_J:
MOV A,@R0
MOV B,#04H
DIV AB
ADD A,#0B0H
MOV COM,A
MOV 18H,A
ACALL WRI
MOV A,B
CJNE A,#00H,$+5
MOV R5,#03H
CJNE A,#01H,$+5
MOV R5,#0CH
CJNE A,#02H,$+5
MOV R5,#30H
CJNE A,#03H,$+5
MOV R5,#0C0H
MOV 02H,R5
MOV A,@R1
MOV B,#08H
DIV AB
ADD A,#10H
MOV COM,A
MOV 19H,A
ACALL WRI
MOV A,#02H
MUL AB
MOV COM,A
MOV 1AH,A
ACALL WRI
ACALL RDD //获取12864数据
//判断所选位置上是否存在点,如果不存在点,放置蛇头,清除蛇尾
MOV A,DAT
ANL A,R5
CJNE A,#00H,OUT
//放置蛇头
//重新输入写地址
MOV COM,18H
ACALL WRI
MOV COM,19H
ACALL WRI
MOV COM,1AH
ACALL WRI
MOV A,DAT
ORL A,02H
MOV DAT,A
ACALL WRD
ACALL WRD
CLR RS0
CLR RS0
//清除蛇尾
MOV A,@R1
CLR ES
mov sbuf,A
jnb ti,$
CLR TI
SETB ES
MOV A,@R0
MOV B,#04H
DIV AB
ADD A,#0B0H
MOV COM,A
MOV 18H,A
ACALL WRI
MOV A,B
CJNE A,#00H,$+5
MOV R5,#0FCH
CJNE A,#01H,$+5
MOV R5,#0F3H
CJNE A,#02H,$+5
MOV R5,#0CFH
CJNE A,#03H,$+5
MOV R5,#3FH
MOV A,@R1
MOV B,#08H
DIV AB
ADD A,#10H
MOV COM,A
MOV 19H,A
ACALL WRI
MOV A,#02H
MUL AB
MOV COM,A
MOV 1AH,A
ACALL WRI
ACALL RDD
//重新输入写地址
MOV COM,18H
ACALL WRI
MOV COM,19H
ACALL WRI
MOV COM,1AH
ACALL WRI
MOV A,DAT
ANL A,R5
MOV DAT,A
ACALL WRD
ACALL WRD
//需要判断R0,R1,R2,R3是否超出范围,并更新数据
SETB RS0
SETB RS0
MOV A,@R0
CJNE R4,#01H,$+4
INC @R0
CJNE R4,#02H,$+4
DEC @R0
INC R0
INC R0
CJNE R0,#50H,$+5
MOV R0,#20H
MOV @R0,A
MOV A,@R1
CJNE R4,#03H,$+4
INC @R1
CJNE R4,#04H,$+4
DEC @R1
INC R1
INC R1
CJNE R1,#51H,$+5
MOV R1,#21H
MOV @R1,A
CLR RS0
CLR RS0
INC R0
INC R0
CJNE R0,#50H,$+5
MOV R0,#20H
INC R1
INC R1
CJNE R1,#51H,$+5
MOV R1,#21H
AJMP DECIDE
SETAPPLE:
MOV A,#00H
MOVC A,@A+DPTR
MOV 06H,A
MOV B,#04H
DIV AB
ADD A,#0B0H
MOV COM,A
MOV 18H,A
ACALL WRI
MOV A,B
CJNE A,#00H,$+5
MOV R5,#03H
CJNE A,#01H,$+5
MOV R5,#0CH
CJNE A,#02H,$+5
MOV R5,#30H
CJNE A,#03H,$+5
MOV R5,#0C0H
MOV 02H,R5
MOV A,#00H
INC DPTR
MOVC A,@A+DPTR
MOV 07H,A
MOV B,#08H
DIV AB
ADD A,#10H
MOV COM,A
MOV 19H,A
ACALL WRI
MOV A,#02H
MUL AB
MOV COM,A
MOV 1AH,A
ACALL WRI
INC DPTR
ACALL RDD
MOV COM,18H
ACALL WRI
MOV COM,19H
ACALL WRI
MOV COM,1AH
ACALL WRI
MOV A,DAT
ANL A,R5
CJNE A,#00H,SETAPPLE
MOV A,DAT
ORL A,02H
MOV DAT,A
ACALL WRD
ACALL WRD
RET
//读数据
RDD:
MOV P0,#0FFH //先对引脚置高电平
CLR L_CS
CLR L_RD
SETB L_RS
SETB L_RW
NOP
NOP
SETB L_RD //先读一次空数据
NOP
NOP
CLR L_RD
NOP
NOP
MOV DAT,P0 //再读有效数据
NOP
NOP
SETB L_RD
NOP
NOP
RET
;写指令
WRI:
CLR L_CS //chip select,打开片选
SETB L_RD //disable read,读失能
CLR L_RS //select command,选择命令
CLR L_RW //select write,选择写模式
NOP
NOP
MOV P0,COM
NOP
NOP
SETB L_RW
NOP
NOP
RET
;写数据
WRD:
CLR L_CS //chip select,打开片选
SETB L_RD //disable read,读失能
SETB L_RS //select data,选择数据
CLR L_RW //select write,选择写模式
NOP
NOP
MOV P0,DAT
NOP
NOP
SETB L_RW
NOP
NOP
RET
;延时
DELAY:
SETB RS0
MOV R7,#01H
DELAY1:
MOV R6,#0FFH
DELAY2:
DJNZ R6,DELAY2
DJNZ R7,DELAY1
CLR RS0
RET
//串口中断操作
UART:
CLR RI
PUSH ACC
MOV A,SBUF
CJNE R4,#01H,$+8
CJNE A,#02H,CHANGE
AJMP RETURN
CJNE R4,#02H,$+8
CJNE A,#01H,CHANGE
AJMP RETURN
CJNE R4,#03H,$+8
CJNE A,#04H,CHANGE
AJMP RETURN
CJNE R4,#04H,$+8
CJNE A,#03H,CHANGE
AJMP RETURN
CHANGE:
MOV 04H,A
MOV 0CH,A
RETURN:
POP ACC
RETI
//以上是串口中断函数
//定时中断
TIME0:
MOV TH0,#15H
MOV TL0,#0A0H //定时60ms
INC 6FH
RETI
;交替产生y,x坐标,共100对
RANDYX:
DB 0AH, 10H,0AH, 20H,13H, 3BH,17H, 13H,10H, 11H,17H, 7H,18H, 34H,18H, 16H,13H, 1EH,3H, 35H
DB 16H, 11H,1FH, 0CH,0AH, 1AH,8H, 0CH,17H, 0CH,11H, 13H,0FH, 3BH,7H, 0H,1DH, 2CH,10H, 21H
DB 3H, 40H,0BH, 28H,0DH, 0H,11H, 21H,8H, 3H,1BH, 3H,0AH, 32H,1FH, 26H,19H, 2FH,15H, 2BH
DB 1EH, 22H,0DH, 2BH,0EH, 9H,0EH, 23H,0DH, 19H,18H, 21H,10H, 6H,8H, 1DH,14H, 2DH,20H, 3CH
DB 3H, 3DH,5H, 3EH,13H, 0FH,2H, 13H,1AH, 5H,0BH, 8H,5H, 4H,16H, 6H,14H, 0BH,15H, 20H
DB 9H, 35H,1AH, 3CH,0H, 29H,0H, 7H,3H, 17H,8H, 16H,8H, 19H,0DH, 29H,19H, 11H,1BH, 2FH
DB 1AH, 0CH,2H, 27H,16H, 7H,3H, 26H,1AH, 34H,3H, 2FH,1DH, 1DH,2H, 0FH,17H, 3H,0EH, 23H
DB 1FH, 0DH,4H, 29H,3H, 3FH,8H, 27H,1AH, 35H,10H, 31H,1DH, 3AH,7H, 23H,19H, 0CH,18H, 1EH
DB 13H, 11H,3H, 13H,1DH, 1EH,9H, 31H,19H, 1H,1CH, 33H,0AH, 2BH,9H, 3CH,9H, 0BH,18H, 1FH
DB 15H, 3EH,0AH, 5H,19H, 17H,13H, 0CH,2H, 0AH,1AH, 13H,0CH, 2AH,1AH, 31H,0CH, 0CH,15H, 0H
END |
|