本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
在网上看到有关这类的文章,自己想了想不知道这样可以不,发出来大家探讨下,举个简单例子,数码管动态扫描时,按键盘数码管数字增加
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit sm_1=P2^2;
sbit sm_2=P2^3;
sbit sm_3=P2^4;
uchar temp,num;
unsigned char code table[] = { 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,
0x88,0x83,0xC6,0xA1,0x86,0x8e,0xc7,0x8c,0xc1, 0xff, 0xf7 };
bit keybit;
void delay(uchar z) //100us
{
uchar x,y;
for(x=z;x>0;x--)
for(y=47;y>0;y--);
}
void display()
{
uchar i;
for(i=0;i<8;i++)
{
switch(i)
{
case 0: sm_1=0; sm_2=0; sm_3=0; break;
case 1: sm_1=1; sm_2=0; sm_3=0; break;
case 2: sm_1=0; sm_2=1; sm_3=0; break;
case 3: sm_1=1; sm_2=1; sm_3=0; break;
case 4: sm_1=0; sm_2=0; sm_3=1; break;
case 5: sm_1=1; sm_2=0; sm_3=1; break;
case 6: sm_1=0; sm_2=1; sm_3=1; break;
case 7: sm_1=1; sm_2=1; sm_3=1; break;
}
P1=table[num];
delay(10);
P1=0xff;
}
}
void keyscan()
{
P3=0xff;
temp=P3&0xff;
if(temp!=0xff)
{
TR0=1;
}
}
void init()
{
TMOD=0x01;
ET0=1;
EA=1;
TH0 = 0xF8;
TL0 = 0x30;
}
void main()
{
init();
while(1)
{
display();
keyscan();
}
}
void timer0() interrupt 1
{
uchar keytemp;
TH0 = 0xF8;
TL0 = 0x30;
keybit=1;
if(1==keybit)
{
if(temp!=0xff)
keytemp=temp;
keybit=0;
P3=0xff;
temp=P3&0xff;
if(0xff==temp)
{
switch(keytemp)
{
case 0xfe : num++;break;
case 0xfd : num=2;break;
case 0xfb : num=3;break;
case 0xf7 : num=4;break;
case 0xff : num=9;break;
default : num=5; break;
}
if(10==num) num=0;
keytemp=0;
TR0=0;
}
}
}
如果需要做时钟之类把KEYSCAN中 TR0=1; 变成KEYFLAG=1;之类给个标志位 |