#include
#define KEY P1 //键盘
#define SEG P0 //数码管
sbit COM=P2^0; // 数码管com
sbit OC = P2^1;//573锁存器
char code TAB[]={//0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xa7,0xa1,0x84,0x84,0xbf,0x7f};//共阳极
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7D,0x07,0x7f,0x6F}; //共阴极
unsigned char scan[4]={0xef,0xdf,0xbf,0x7f}; //扫描码 低电平动作扫描
void delay1ms(int x);
void main(void)
{
unsigned char col,row; //定义列行
unsigned char rowkey,kcode; //定义按键,按键码
COM=0; //点亮数码管
OC=0; // 低电平使能
while(1)
{
for(col=0;col<4;col++) //4列按键
{
KEY =scan[col];
rowkey = ~KEY&0X0F; //例如 读入低4位 反相清除高4位,求出行键值
while(rowkey!=0) //如果按下键
{
if(rowkey==0x01) row = 0; //如果第0行被按下
if(rowkey ==0x02) row = 1; //如果第1行被按下
if(rowkey == 0x04) row = 2; //如果第2行被按下
if(rowkey == 0x08) row = 3; //如果第3行被按下
kcode = 4*col+row; //算出按键号码
SEG = TAB[kcode]; //显示
while(rowkey!=0) //如果未放开
rowkey = ~ KEY&0x0f; //再次读入
}
delay1ms(1); //消除抖动1ms
}
}
}
void delay1ms(int x) //延迟消抖
{
int i,j;
for(i=0;i for(j=0;j<120;j++); // 120 * 8 = 960us = 1ms }
上一篇:51单片机运行过程
下一篇:MCS-51单片机存储器扩展
推荐阅读最新更新时间:2024-03-16 15:30