通过按键控制数码管,现在肿么不亮了,渣渣求教导
时间:10-02
整理:3721RD
点击:
我要通过16个的矩阵键盘对数码管进行控制。也就是让数码管显示1到16个数,大于9的用字母代替。下面是程序,求各位大神教导!
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
//此表为 LED 的字模, 共阴数码管 0 1 2 3 4 5 6 7 8 9 a b c d e f
unsigned char code font[] = {~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F,~0x77,~0x7C,~0x39,~0x5E,~0x79,~0x71}; //段码控制
//此表为8个数码管位选控制, 共阴数码管 1-8个 -
unsigned char code dispbit[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdF,0xbF,0x7F}; //位选控制 查表的方法控制
uint num;
void display()
{
P0 = dispbit[num-1];
}
void delay(uint xms)
{
uint i,j;
for(i = xms; i > 0; i--)
for(j = 110; j > 0; j--);
}
void matrixkeyscan()
{
uchar num,temp;
P3 = 0xfe;
temp = P3;
temp = temp&0xf0;
if(temp != 0xf0)
{
delay(10);//按键去抖动
temp = P3;
temp = temp&0xf0;
if(temp != 0xf0)
{
temp = P3;
switch(temp)
{
case 0xee:num = 1;break;
case 0xde:num = 2;break;
case 0xbe:num = 3;break;
case 0x7e:num = 4;break;
}
}
while(temp != 0xf0)
{
temp = P3;
temp = temp&0xf0;
}
display();
}
P3 = 0xfd;
temp = P3;
temp = temp&0xf0;
if(temp != 0xf0)
{
delay(10);
temp = P3;
temp = temp&0xf0;
if(temp != 0xf0)
{
temp = P3;
switch(temp)
{
case 0xed:num = 5;break;
case 0xdd:num = 6;break;
case 0xbd:num = 7;break;
case 0x7d:num = 8;break;
}
}
while(temp != 0xf0)
{
temp = P3;
temp = temp&0xf0;
}
display();
}
P3 = 0xfb;
temp = P3;
temp = temp&0xf0;
if(temp != 0xf0)
{
delay(10);
temp = P3;
temp = temp&0xf0;
if(temp != 0xf0)
{
temp = P3;
switch(temp)
{
case 0xeb:num = 9;break;
case 0xdb:num = 10;break;
case 0xbb:num = 11;break;
case 0x7b:num = 12;break;
}
}
while(temp != 0xf0)
{
temp = P3;
temp = temp&0xf0;
}
display();
}
P3 = 0xf7;
temp = P3;
temp = temp&0xf0;
if(temp != 0xf0)
{
delay(10);
temp = P3;
temp = temp&0xf0;
if(temp != 0xf0)
{
temp = P3;
switch(temp)
{
case 0xe7:num = 13;break;
case 0xd7:num = 14;break;
case 0xb7:num = 15;break;
case 0x77:num = 16;break;
}
}
while(temp != 0xf0)
{
temp = P3;
temp = temp&0xf0;
}
display();
}
}
void main()
{
while(1)
{
matrixkeyscan();
}
}
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
//此表为 LED 的字模, 共阴数码管 0 1 2 3 4 5 6 7 8 9 a b c d e f
unsigned char code font[] = {~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F,~0x77,~0x7C,~0x39,~0x5E,~0x79,~0x71}; //段码控制
//此表为8个数码管位选控制, 共阴数码管 1-8个 -
unsigned char code dispbit[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdF,0xbF,0x7F}; //位选控制 查表的方法控制
uint num;
void display()
{
P0 = dispbit[num-1];
}
void delay(uint xms)
{
uint i,j;
for(i = xms; i > 0; i--)
for(j = 110; j > 0; j--);
}
void matrixkeyscan()
{
uchar num,temp;
P3 = 0xfe;
temp = P3;
temp = temp&0xf0;
if(temp != 0xf0)
{
delay(10);//按键去抖动
temp = P3;
temp = temp&0xf0;
if(temp != 0xf0)
{
temp = P3;
switch(temp)
{
case 0xee:num = 1;break;
case 0xde:num = 2;break;
case 0xbe:num = 3;break;
case 0x7e:num = 4;break;
}
}
while(temp != 0xf0)
{
temp = P3;
temp = temp&0xf0;
}
display();
}
P3 = 0xfd;
temp = P3;
temp = temp&0xf0;
if(temp != 0xf0)
{
delay(10);
temp = P3;
temp = temp&0xf0;
if(temp != 0xf0)
{
temp = P3;
switch(temp)
{
case 0xed:num = 5;break;
case 0xdd:num = 6;break;
case 0xbd:num = 7;break;
case 0x7d:num = 8;break;
}
}
while(temp != 0xf0)
{
temp = P3;
temp = temp&0xf0;
}
display();
}
P3 = 0xfb;
temp = P3;
temp = temp&0xf0;
if(temp != 0xf0)
{
delay(10);
temp = P3;
temp = temp&0xf0;
if(temp != 0xf0)
{
temp = P3;
switch(temp)
{
case 0xeb:num = 9;break;
case 0xdb:num = 10;break;
case 0xbb:num = 11;break;
case 0x7b:num = 12;break;
}
}
while(temp != 0xf0)
{
temp = P3;
temp = temp&0xf0;
}
display();
}
P3 = 0xf7;
temp = P3;
temp = temp&0xf0;
if(temp != 0xf0)
{
delay(10);
temp = P3;
temp = temp&0xf0;
if(temp != 0xf0)
{
temp = P3;
switch(temp)
{
case 0xe7:num = 13;break;
case 0xd7:num = 14;break;
case 0xb7:num = 15;break;
case 0x77:num = 16;break;
}
}
while(temp != 0xf0)
{
temp = P3;
temp = temp&0xf0;
}
display();
}
}
void main()
{
while(1)
{
matrixkeyscan();
}
}
自己顶一个!
你的位选控制,没有打开,软件上没写