本人是新手,有些基本的问题弄不清楚。
还有本人在做矩阵键盘检测时,不知为什么,按键按了数码管只显示0,没有反应。
请各位大虾指点下。谢谢哈
#include <REGX51.H>
#define uint unsigned int
#define uchar unsigned char
sbit Key_ce=P1^3;
sbit Dig_ce=P1^1;
sbit Seg_ce=P1^0;
void Key();
uchar code Seg_tab[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x4f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void display(uchar z)
{
P0 = Seg_tab[z];
Seg_ce = 1;
Seg_ce = 0;
}
void delay(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=124;j>0;j--);
}
void main()
{ P0 = 0;
P1 = 0x0c;
while(1)
{
Key();
}
}
void Key()
{
uchar temp1,temp2,keynum;
P0 = 0xfe;
Dig_ce = 1;
Dig_ce = 0;
delay(1);
Key_ce = 0;
temp1 = P0;
temp1 = temp1&0x0f;
if(temp1!=0x0f)
{
delay(10);
temp2 = P0;
temp2 = temp2&0x0f;
if(temp1==temp2)
{
switch(temp2)
{
case 0x0e:keynum=0;break;
case 0x0d:keynum=1;break;
case 0x0b:keynum=2;break;
case 0x07:keynum=3;break;
}
while(temp2!=0x0f) // 按键释放检测
{
temp2 = P0;
temp2 = temp2&0x0f;
}
}
}
Key_ce = 1;
P0 = 0xfe;
Dig_ce = 1;
Dig_ce = 0;
display(keynum);
}
-
学习板原理图.pdf
(2011-8-23 19:50 上传)
534.89 KB, 下载次数: 9
这是我的单片机原理图