[51单片机] 如何用矩阵键盘输入一多位数在数码管上显示

[复制链接]
7855|15
 楼主| cyl0856 发表于 2013-8-4 16:18 | 显示全部楼层 |阅读模式
本帖最后由 cyl0856 于 2013-8-5 13:50 编辑

求前辈高手们赐教,如何编程实现用矩阵键盘输入一多位数在数码管上显示,要c语言的,最好附上思路,
单片机芯片是STC89c52的,板子是德飞莱的,由于原理我也不懂,所以方框图也没法给,我希望实现的功能是在
矩阵键盘上按“1”键就在第一个数码管显示“1”,再按“2”键就在第二个数码管上显示“2”,最终用矩阵键盘输入一多位数,不知道能否实现?
这问题纠结好多天了,希望高手们指点小弟,
邮箱:554048952@qq.com
dirtwillfly 发表于 2013-8-4 20:30 | 显示全部楼层
又见伸手党
ZG11211 发表于 2013-8-4 21:29 | 显示全部楼层
楼主的提问,连百度也搜索不出来
林志财 发表于 2013-8-4 21:51 | 显示全部楼层
输入一多位数
好似问的都唔系好清楚。
又见伸手党
ayb_ice 发表于 2013-8-5 08:00 | 显示全部楼层
最好注释详细点,外加方框图
qzc13457 发表于 2013-8-5 11:09 | 显示全部楼层
硬件不同,程序不同,自己写吧亲
coody 发表于 2013-8-5 11:25 | 显示全部楼层
LZ忘记附上email或QQ了,帮写好了发给他。如5楼所说的,最好注释详细点,注释要中文的,外加方框图,程序要完整的工程。
 楼主| cyl0856 发表于 2013-8-5 13:55 | 显示全部楼层
coody 发表于 2013-8-5 11:25
LZ忘记附上email或QQ了,帮写好了发给他。如5楼所说的,最好注释详细点,注释要中文的,外加方框图,程序要 ...

多谢兄台指点,但是它的原理我不懂,方框图没法给,还请兄弟多帮忙
coody 发表于 2013-8-5 14:27 | 显示全部楼层
你找一本基础的课本,里面详细得很。虽然实际的产品已经很少用这样的方案。
tergy2012 发表于 2013-8-5 15:45 | 显示全部楼层
看到好问题,路过学学习
林志财 发表于 2013-8-5 21:53 | 显示全部楼层
看来要看那个闲着才帮你搞全套
林志财 发表于 2013-8-5 21:59 | 显示全部楼层
拿来主义那个给你慢慢改吧,改到合适你的
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
sbit key1=P3^4;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0};
uchar num,temp,num1;
void delay(uint z)
{
        uint x,y;
        for(x=z;x>0;x--)
                for(y=110;y>0;y--);
}
uchar keyscan();
void display(uchar aa);
void main()
{
        num=17;
        dula=1;
        P0=0;
        dula=0;
        wela=1;
        P0=0xc0;
        wela=0;
       
        while(1)
                {
                        display(keyscan());
                }
}
void display(uchar aa)
{
                        dula=1;
                        P0=table[aa-1];
                        dula=0;               
}
uchar keyscan()
{
                        P3=0xfe;
                        temp=P3;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                                {
                                        delay(5);
                                        temp=P3;
                                        temp=temp&0xf0;
                                        while(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;
                                                }
                                        }
                                }

                        P3=0xfd;
                        temp=P3;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                                {
                                        delay(5);
                                        temp=P3;
                                        temp=temp&0xf0;
                                        while(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;
                                                }
                                        }
                                }


                        P3=0xfb;
                        temp=P3;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                                {
                                        delay(5);
                                        temp=P3;
                                        temp=temp&0xf0;
                                        while(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;
                                                }
                                        }
                                }


                        P3=0xf7;
                        temp=P3;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                                {
                                        delay(5);
                                        temp=P3;
                                        temp=temp&0xf0;
                                        while(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;
                                                }
                                        }
                                }
return num;

}
jianwen9015 发表于 2013-8-5 22:03 | 显示全部楼层
百度上有很多例子。。。。
xyz549040622 发表于 2013-8-5 23:47 | 显示全部楼层
方框图没法给!!!源码不给,原理图不给!!!!这个虎头蛇尾的问答。。。
 楼主| cyl0856 发表于 2013-8-6 15:18 | 显示全部楼层
林志财 发表于 2013-8-5 21:59
拿来主义那个给你慢慢改吧,改到合适你的
#include
#define uint unsigned int

老兄,我就是改不成才来问的,我本想用一循环右移来使位选在按下一个键后自动右移,以便在第二个数码管显示,但是就是不成功,我不知道该将这个循环右移放在哪?
这是我改的代码,劳烦看看错在哪?
#include<reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
uchar tema[5];
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x80,0};
uint x,y;
uchar num,temp,z,n1=0,n2=0,n3=0,n4=0,h;

void delay(uint z)
{
        uint x,y;
        for(x=z;x>0;x--)
                for(y=110;y>0;y--);
}


uchar keyscan()
{
        P1=0xfe;
        temp=P1;
        temp=temp&0xf0;
        while(temp!=0xf0)
        {
                delay(5);
                temp=P1;
                temp=temp&0xf0;
                while(temp!=0xf0)
                {
                        n1=1;
                        temp=P1;
                        switch(temp)
                        {
                                case 0xee:num=1;
                                                break;
                                case 0xde:num=8;
                                                break;
                                case 0xbe:num=5;
                                                break;
                                case 0x7e:num=2;
                                                break;
                        }
                        while(temp!=0xf0)
                        {
                                temp=P1;
                                temp=temp&0xf0;
                        }
                       
                }
        }
        P1=0xfd;
        temp=P1;
        temp=temp&0xf0;
        while(temp!=0xf0)
        {
                delay(5);
                temp=P1;
                temp=temp&0xf0;
                while(temp!=0xf0)
                {
                        n2=1;
                        temp=P1;
                        switch(temp)
                        {
                                case 0xed:num=17;
                                                break;
                                case 0xdd:num=9;
                                                break;
                                case 0xbd:num=6;
                                                break;
                                case 0x7d:num=3;
                                                break;
                        }
                        while(temp!=0xf0)
                        {
                                temp=P1;
                                temp=temp&0xf0;
                        }
                       
                }
        }
        P1=0xfb;
        temp=P1;
        temp=temp&0xf0;
        while(temp!=0xf0)
        {
                delay(5);
                temp=P1;
                temp=temp&0xf0;
                while(temp!=0xf0)
                {
                        n3=1;
                        temp=P1;
                        switch(temp)
                        {
                                case 0xeb:num=9;
                                                break;
                                case 0xdb:num=10;
                                                break;
                                case 0xbb:num=7;
                                                break;
                                case 0x7b:num=4;
                                                break;
                        }
                        while(temp!=0xf0)
                        {
                                temp=P1;
                                temp=temp&0xf0;
                        }
                       
                }
        }
        P1=0xf7;
        temp=P1;
        temp=temp&0xf0;
        while(temp!=0xf0)
        {
                delay(5);
                temp=P1;
                temp=temp&0xf0;
                while(temp!=0xf0)
                {
                        n4=0;
                        temp=P1;
                        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=P1;
                                temp=temp&0xf0;
                        }
                       
                }
        }

        return num;
       
}

void display(uchar wan,uchar qin)
{
       
                dula=1;
                P0=table[wan-1];
                dula=0;
                P0=0xff;
                wela=1;
                P0=qin;
                wela=0;
                delay(1);
               
}

void main()
{       
        uchar b,i=0,n;
        b=0xfe;
        num=18;
       
        while(1)
        {
                tema=keyscan();
                n=n1+n2+n3+n4;
                if(n!=0)
                {
                       
                        display(tema,b);       
                        b=_cror_(b,1);
                        n=0;
                        i++;       
                }       
        }
       
}
林志财 发表于 2013-8-9 14:17 | 显示全部楼层
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit s1=P3^7;
bit flag;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0};
uchar num,temp,num1,num2,wl1,wl2,wl3,wl4;
void delay(uint z)
{
        uint x,y;
        for(x=z;x>0;x--)
                for(y=110;y>0;y--);
}

void init()
{
        EA=1;
        EA=1;//开总中断
        EX0=1;//开外部中断0
        TCON=0x01;
        num=17;
        num2=0;
        wl1=0;
        wl2=0;
        wl3=0;
        wl4=0;
}
void key_num()
{
        while(flag)
    {
                P1=0xfc;
                temp=P1;
                temp=temp&0xf0;
        while(temp!=0xf0)
        {
                                                                               
              delay(5);
              temp=P1;
              temp=temp&0xf0;
              while(temp!=0xf0)
              {
                   temp=P1;
                               num2++;
                                        if(num2==2)
                                        wl1=num;
                                        else if(num==3)
                                        wl2=num;
                                        else if(num==4)
                                        wl3=num;
                                        else if(num==5)
                                        wl4=num;
                                    else
                                    num2=0;
                   switch(temp)
                       {
                             case 0xee:num=0;
                                       break;
                             case 0xde:num=1;
                                       break;
                             case 0xbe:num=2;
                                       break;
                             case 0x7e:num=3;
                                       break;
                        }
                       while(temp!=0xf0)
                        {
                              temp=P1;
                              temp=temp&0xf0;
                        }
               }
          }
                P1=0xfa;
                temp=P1;
                temp=temp&0xf0;
        while(temp!=0xf0)
        {
                                                                               
              delay(5);
              temp=P1;
              temp=temp&0xf0;
              while(temp!=0xf0)
              {
                   temp=P1;
                               num2++;
                                   if(num2==2)
                                        wl1=num;
                                        else if(num==3)
                                        wl2=num;
                                        else if(num==4)
                                        wl3=num;
                                        else if(num==5)
                                        wl4=num;
                                    else
                                    num2=0;
                   switch(temp)
                       {
                             case 0xea:num=4;
                                       break;
                             case 0xda:num=5;
                                       break;
                             case 0xba:num=6;
                                       break;
                             case 0x7a:num=7;
                                       break;
                        }
                       while(temp!=0xf0)
                        {
                              temp=P1;
                              temp=temp&0xf0;
                        }
               }
          }
                P1=0xf6;
                temp=P1;
                temp=temp&0xf0;
        while(temp!=0xf0)
        {
                                                                               
              delay(5);
              temp=P1;
              temp=temp&0xf0;
              while(temp!=0xf0)
              {
                   temp=P1;
                               num2++;
                                   if(num2==2)
                                        wl1=num;
                                        else if(num==3)
                                        wl2=num;
                                        else if(num==4)
                                        wl3=num;
                                        else if(num==5)
                                        wl4=num;
                                    else
                                    num2=0;
                   switch(temp)
                       {
                             case 0xee:num=8;
                                       break;
                             case 0xde:num=9;
                                       break;
            
                        }
                       while(temp!=0xf0)
                        {
                              temp=P1;
                              temp=temp&0xf0;
                        }
               }
          }       
                P2=0x0c;
                P0=table[wl1];
                delay(4);                                                       
                P2=0x08;
                P0=table[wl2];
                delay(4);                       
            P2=0x04;
                P0=table[wl3];
                delay(4);                       
                P2=0x00;
                P0=table[wl4];
                delay(4);
                       
          }

}
main()
{

        init();
    while(1)
        {
       
                key_num();
        }
}
void exter0() interrupt 0
{
        num1++;
        if(num1%2==1)
        {
                flag=1;
                s1=0;       
        }
        else
        flag=0;       
}
没有测试过你自己看看
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

3

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部