第一个程序(一共两个)
这个代码是单片机自带的程序,按下矩阵键盘数码管可以显示对应数字。还有一个我改的,数码管却不亮了。附件有开发板原理图,两个代码编译都没问题,求解,谢谢了。图片就是原理图了,不用下载附件了.
#include "reg52.h"
#define uint unsigned int ;
#define uchar unsigned char ;
#define GPIO_DIG P0
#define GPIO_KEY P1
uchar KeyValue;
uchar temp;
uchar i;
uchar code smgduan[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}
void delay(unsigned int i) //延时函数
{
while(i--);
}
void KeyDown(void)
{
char a=0;
GPIO_KEY=0x0f;
if(GPIO_KEY!=0x0f
{
delay(1000);
if(GPIO_KEY!=0x0f
{
GPIO_KEY=0X0F;
switch(GPIO_KEY)
{
case(0X07): KeyValue=0;break;
case(0X0b): KeyValue=1;break;
case(0X0d): KeyValue=2;break;
case(0X0e): KeyValue=3;break;
}
GPIO_KEY=0XF0;
switch(GPIO_KEY)
{
case(0X70): KeyValue=KeyValue;break;
case(0Xb0): KeyValue=KeyValue+4;break;
case(0Xd0): KeyValue=KeyValue+8;break;
case(0Xe0): KeyValue=KeyValue+12;break;
}
while((a<50)&&(GPIO_KEY!=0xf0)
{
delay(1000);
a++;
}
}
}
}
void main()
{
while(1)
{
KeyDown();
GPIO_DIG=smgduan[KeyValue];
}
}
#include "reg52.h"
#include <intrins.h>
#define uint unsigned int ;
#define uchar unsigned char ;
#define GPIO_DIG P0
#define GPIO_KEY P1
sbit L0=P1^3;
sbit L1=P1^2;
sbit L2=P1^1;
sbit L3=P1^0;
uchar KeyValue;
uchar temp;
uchar i;
uchar code smgduan[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
void delay(unsigned int i)
{
while(i--);
}
void KeyDown(void)
{
while(1)
{
P1=0x7f;
for(i=0;i<=3;i++)
{
if(L0==0) KeyValue=i*4+0;
if(L1==0) KeyValue=i*4+1;
if(L2==0) KeyValue=i*4+2;
if(L3==0) KeyValue=i*4+3;
temp=P1;
temp=_cror_(temp,1);
P1=temp;
}
}
}
void main()
{
while(1)
{
KeyDown();
GPIO_DIG=smgduan[KeyValue];
}
}
|