今天无聊就随便写了个4*4键盘的程序来试下单片机学习板!
分C和汇编还有PS仿真!
下面这个就是ps仿真图 已经通过,朋友们可以直接烧写到学习板上跑一下。
C语言的:
/******************************************************************
** 文件名:51学习板4*4键盘
** 创建人: 谢军洋
** 日 期: 2008/01/07
** 描 述:4*4键盘,用P1口(p1.0~p1.3列p1.4~p1.7行)。
** 版 本: 1.1
**------------------------------------------------------------------
** CPU:MCS-51
** 语言:C
** Fosc=12M
********************************************************************/
#include< at89x51.h>
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
void delay10ms(); //按键去抖。
uchar table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0xff};
void main()
{
uchar key;
while(1)
{
P1=0xff;
P1_4=0;
key=P1&0x0f;
if(key!=0x0f)
{
delay10ms();
key=P1&0x0f;
if(key!=0x0f)
{
switch(key)
{
case 0x0e:key=0;break;
case 0x0d:key=1;break;
case 0x0b:key=2;break;
case 0x07:key=3;break;
}
P0=table[key];
key=key&0x0f;
while(key!=0x0f)
{key=P1;
key=key&0x0f;
}
}
}
P1=0xff;
P1_5=0;
key=P1;
key=key&0x0f;
if(key!=0x0f)
{
delay10ms();
key=P1&0x0f;
if(key!=0x0f)
{
switch(key)
{
case 0x0e:key=4;break;
case 0x0d:key=5;break;
case 0x0b:key=6;break;
case 0x07:key=7;break;
}
P0=table[key];
key=key&0x0f;
while(key!=0x0f)
{key=P1;
key=key&0x0f;
}
}
}
P1=0xff;
P1_6=0;
key=P1;
key=key&0x0f;
if(key!=0x0f)
{
delay10ms();
key=P1&0x0f;
if(key!=0x0f)
{
switch(key)
{
case 0x0e:key=8 ;break;
case 0x0d:key=9 ;break;
case 0x0b:key=10;break;
case 0x07:key=11;break;
}
P0=table[key];
key=key&0x0f;
while(key!=0x0f)
{key=P1;
key=key&0x0f;
}
}
}
P1=0xff;
P1_7=0;
key=P1;
key=key&0x0f;
if(key!=0x0f)
{
delay10ms();
key=P1&0x0f;
if(key!=0x0f)
{
switch(key)
{
case 0x0e:key=12;break;
case 0x0d:key=13;break;
case 0x0b:key=14;break;
case 0x07:key=15;break;
}
P0=table[key];
key=key&0x0f;
while(key!=0x0f)
{key=P1;
key=key&0x0f;
}
}
}
}
}
void delay10ms()
{
uchar i,j;
for(i=50;i>0;i--)
for(j=200;j>0;j--);
}
汇编的:
/******************************************************************
** 文件名:51单片机学习板4*4键盘程序
** Copyright (c)单片机教程网http://www.51hei.com
** 创建人: 谢军洋
** 日 期: 2008/01/07
** 描 述:4*4键盘,用P1口(p1.0~p1.3列p1.4~p1.7行)。
** 版 本: 1.1
**------------------------------------------------------------------
** CPU:MCS-51
** 语言:汇编
** Fosc=12M
********************************************************************/
KEY EQU 30H
ORG 0000H
SJMP START
START:MOV P0,#00H
JIXU:MOV P1,#0FFH
CLR P1.4
MOV A,P1
ANL A,#0FH
XRL A,#0FH
JZ NOKEY0
LCALL DELAY10MS
MOV A,P1
ANL A,#0FH
XRL A,#0FH
JZ NOKEY0
MOV A,P1
ANL A,#0FH
CJNE A,#0EH,NEXT1
MOV KEY,#0
LJMP OK
NEXT1:CJNE A,#0DH,NEXT2
MOV KEY,#1
LJMP OK
NEXT2:CJNE A,#0BH,NEXT3
MOV KEY,#2
LJMP OK
NEXT3:CJNE A,#07H,NOKEY0
MOV KEY,#3
LJMP OK
NOKEY0:MOV P1,#0FFH
CLR P1.5
MOV A,P1
ANL A,#0FH
XRL A,#0FH
JZ NOKEY1
LCALL DELAY10MS
MOV A,P1
ANL A,#0FH
XRL A,#0FH
JZ NOKEY1
MOV A,P1
ANL A,#0FH
CJNE A,#0EH,NEXT5
MOV KEY,#4
LJMP OK
NEXT5:CJNE A,#0DH,NEXT6
MOV KEY,#5
LJMP OK
NEXT6:CJNE A,#0BH,NEXT7
MOV KEY,#6
LJMP OK
NEXT7:CJNE A,#07,NOKEY1
MOV KEY,#7
LJMP OK
NOKEY1:MOV P1,#0FFH
CLR P1.6
MOV A,P1
ANL A,#0FH
XRL A,#0FH
JZ NOKEY2
LCALL DELAY10MS
MOV A,P1
ANL A,#0FH
XRL A,#0FH
JZ NOKEY2
MOV A,P1
ANL A,#0FH
CJNE A,#0EH,NEXT9
MOV KEY,#8
SJMP OK
NEXT9:CJNE A,#0DH,NEXT10
MOV KEY,#9
SJMP OK
NEXT10:CJNE A,#0BH,NEXT11
MOV KEY,#10
SJMP OK
NEXT11:CJNE A,#07,NOKEY2
MOV KEY,#11
SJMP OK
NOKEY2:MOV P1,#0FFH
CLR P1.7
MOV A,#P1
ANL A,#0FH
XRL A,#0FH
JZ NEXT16
LCALL DELAY10MS
MOV A,P1
ANL A,#0FH
XRL A,#0FH
JZ NEXT16
MOV A,P1
ANL A,#0FH
CJNE A,#0EH,NEXT13
MOV KEY,#12
SJMP OK
NEXT13:CJNE A,#0DH,NEXT14
MOV KEY,#13
SJMP OK
NEXT14:CJNE A,#0BH,NEXT15
MOV KEY,#14
SJMP OK
NEXT15:CJNE A,#07H,NEXT16
MOV KEY,#15
SJMP OK
NEXT16:LJMP JIXU
OK:MOV A,KEY
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
LJMP JIXU
DELAY10MS:MOV R6,#10
D1:MOV R7,#248
DJNZ R7,$
DJNZ R6,D1
RET
TABLE: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H
DB 7FH,6FH,77H,7CH,39H,5EH,79H,71H
END