sbit k=P1^0//选取一个I/O端口连接一个按键
unsigned char r//设置一个计数变量
void delay(uchar p)//12MHz@p微秒延时函数
{
unsigned char i,j
for(i=0i<pi++){
for(j=0j<50j++)
{}}
}
void key()//按键函数
{
if (k==0)//查询按键是否按下
{
delay(20) //等待按键连接稳定
if(k==0) //如果按键仍然是连接状态
{
r++//标志+1
if(r>1){r=0}//可以任意多种选择,这里只设定0、1两种状态
while(!k)//等待按键抬起
}
}
}
void set_out()//设置输出方式函数
{
key()//查询按键值
if(r==0){***心形显示***}
if(r==1){***星形显示***}
//if(r==?){***?形显示***}
}
单片机8*8点阵(0~F十六进制数字,可做成8*8图形)仿真实例。
)
LED EQU P3LED灯显示定义
KEY EQU P1键盘接口定义
TIME EQU 30H
CNTA EQU 31H
CNTB EQU 32H
--------------------程序入口定义
ORG 0000H
JMP START芯片复位后的程序执行处
ORG 000BH
JMP T0LED8x8 点阵扫描显示,T0 中断
ORG 0030H
--------------------初始化处理
START:
MOV TIME, #00H
MOV CNTA, #00H
MOV CNTB, #00H
MOV TMOD, #01H
MOV TH0, #0F0H
MOV TL0, #60H
SETB TR0
SETB ET0
SETB EA
MOV LED,#00HLED灯初始化全灭
--------------------扫描循环体
LOOP:
MOV KEY,#0FH装入键盘首次的扫描值
MOV A,KEY读出键盘值放入累加器A
CJNE A,#0FH,GO1读出的键值是否有变化,有变则跳到有键按下处理程序
JMP LOOP无键按下跳回主循环
GO1:
CALL DL20MS有键按下延时20毫秒去抖
MOV A,KEY将键值给累加器
CJNE A,#0FH,GO2重新判断键盘现状
JMP LOOP是抖动则跳回主循环
GO2:
MOV KEY,#0F0H确定有键按下之后装入二次的扫描值
MOV B,KEY将二次扫描值放入寄存器B中
ORL A,BA与B相或,得到一个字节的数据在A中
-----------------------键值对比处理
NE1:
CJNE A,#0EEH,NE2将A中的数据与键值表对比,不同则跳到下一个对比
MOV A,#0将输出显示数据送到寄存器
JMP KEYEND跳出对比程序
NE2:
CJNE A,#0EDH,NE31
MOV A,#1
JMP KEYEND
NE3:
CJNE A,#0EBH,NE42
MOV A,#2
JMP KEYEND
NE4:
CJNE A,#0E7H,NE53
MOV A,#3
JMP KEYEND
NE5:
CJNE A,#0DEH,NE64
MOV A,#4
JMP KEYEND
NE6:
CJNE A,#0DDH,NE75
MOV A,#5
JMP KEYEND
NE7:
CJNE A,#0DBH,NE86
MOV A,#6
JMP KEYEND
NE8:
CJNE A,#0D7H,NE97
MOV A,#7
JMP KEYEND
NE9:
CJNE A,#0BEH,NE108
MOV A,#8
JMP KEYEND
NE10:
CJNE A,#0BDH,NE119
MOV A,#9
JMP KEYEND
NE11:
CJNE A,#0BBH,NE12A
MOV A,#10
JMP KEYEND
NE12:
CJNE A,#0B7H,NE13B
MOV A,#11
JMP KEYEND
NE13:
CJNE A,#07EH,NE14C
MOV A,#12
JMP KEYEND
NE14:
CJNE A,#07DH,NE15D
MOV A,#13
JMP KEYEND
NE15:
CJNE A,#07BH,NE16E
MOV A,#14
JMP KEYEND
NE16:
CJNE A,#077H,KEYENDF
MOV A,#15
KEYEND: MOV CNTB, A
MOV DPTR, #PDATA
MOVC A, @A+DPTR
MOV LED,A将输出显示数据送出显示
JMP LOOP跳回主循环
PDATA: DB 80H,0F9H, 24H, 30H, 59H, 12H, 02H,0F8H 显示码数据表
DB 00H, 10H, 08H, 03H, 86H, 21H, 06H, 0EH
-------------------20毫秒延时,主要用于去抖动。(100,100)
DL20MS:
MOV R6,#100
DL20MS_1:
MOV R7,#100
DJNZ R7,$
DJNZ R6,DL20MS_1
RET
T0LED: 8x8 点阵扫描显示,T0 中断
MOV TH0, #0F0H
MOV TL0, #60H
MOV DPTR, #TAB X
MOV A, CNTA
MOVC A, @A+DPTR
MOV P0, A P3
MOV DPTR, #DIGIT Y
MOV A, CNTB
MOV B, #8
MUL AB
ADD A, CNTA
MOVC A, @A+DPTR
MOV P2, A P1
INC CNTA
MOV A, CNTA
CJNE A, #8, NEXT
MOV CNTA, #00H
NEXT:
RETI
TAB:
DB 0FEH,0FDH,0FBH,0F7H,0EFH,0DFH,0BFH,07FH
DIGIT:
DB 00H,00H,3EH,41H,41H,41H,3EH,00H 0
DB 00H,00H,00H,00H,21H,7FH,01H,00H 1
DB 00H,00H,21H,43H,45H,49H,31H,00H 2
DB 00H,00H,42H,49H,49H,59H,66H,00H 3
DB 00H,00H,0CH,14H,24H,7FH,04H,00H 4
DB 00H,00H,72H,51H,51H,51H,4EH,00H 5
DB 00H,00H,3EH,49H,49H,49H,26H,00H 6
DB 00H,00H,40H,40H,40H,4FH,70H,00H 7
DB 00H,00H,36H,49H,49H,49H,36H,00H 8
DB 00H,00H,32H,49H,49H,49H,3EH,00H 9
DB 00H,00H,07H,38H,48H,38H,07H,00H A
DB 00H,00H,7FH,49H,49H,49H,36H,00H B
DB 00H,00H,3EH,41H,41H,41H,22H,00H C
DB 00H,00H,7FH,41H,41H,41H,3EH,00H D
DB 00H,00H,7FH,49H,49H,49H,41H,00H E
DB 00H,00H,7FH,48H,48H,48H,40H,00H F
END
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)