跪求单片机8个按键的独立式键盘实验C程序源码!!!AT89C51的芯片、共阴极的数码管
2个回答
展开全部
#include<reg51.h>
#include<intrins.h>
#define nop() _nop_()
#define keyport P1
#define ledport P2
unsigned char code seg[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //共阴数码管段码
void delayms(unsigned int ms) //12M
{
unsigned int t;
for(;ms>0;ms--)
for(t=123;t>0;t--);
}
unsigned char keyscan(void)
{
unsigned char temp;
keyport=0xFF;
nop();nop();
if(keyport!=0xFF)
{
delayms(10);
if(keyport!=0xFF)
{
switch(keyport)
{
case 0xFE:temp=1;break;
case 0xFD:temp=2;break;
case 0xFB:temp=3;break;
case 0xF7:temp=4;break;
case 0xEF:temp=5;break;
case 0xDF:temp=6;break;
case 0xBF:temp=7;break;
case 0x7F:temp=8;break;
}
}
}
return temp;
}
void display(unsigned char dat)
{
ledport=seg[dat];
}
void main(void)
{
unsigned char num;
for(;;)
{
num=keyscan();
display(num);
}
}
//按照你的图,proteus仿真通过,上电后显示0,按下第一个按键就显示1,以此类推。。。
//希望可以帮到你。
//如果你是单片机爱好者,可以加我QQ,我们共同进步!
//当然了,如果你感觉以上的键盘扫描方法效率低的话,可以试试以下的分时采样法,效果是一样的。
#include<reg51.h>
#include<intrins.h>
#define nop() _nop_()
#define keyport P1
#define ledport P2
unsigned char code seg[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char temp;
void display(unsigned char dat)
{
ledport=seg[dat];
}
void main(void)
{
TMOD |= 0x01;
TH0 = 0xEC;
TL0 = 0x78;
EA = 1;
ET0 = 1;
TR0 = 1;
for(;;)
{
display(temp);
}
}
void Timer0Interrupt(void) interrupt 1
{
static unsigned char c;
TH0 = 0xEC;
TL0 = 0x78;
if(keyport==0xFF) c=0;
if((c<100)&&(++c==10))
{
switch(keyport)
{
case 0xFE:temp=1;break;
case 0xFD:temp=2;break;
case 0xFB:temp=3;break;
case 0xF7:temp=4;break;
case 0xEF:temp=5;break;
case 0xDF:temp=6;break;
case 0xBF:temp=7;break;
case 0x7F:temp=8;break;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询