找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1702|回复: 0
打印 上一主题 下一主题
收起左侧

双机通信16按键显示

[复制链接]
跳转到指定楼层
楼主
ID:350061 发表于 2018-6-12 10:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
主机#define KEY P1
//unsigned char code num[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x7f};          
unsigned char code num[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};          //共yang数码管

unsigned char KeyValue;
int i;
void Delay10ms(unsigned int c);    //延时10ms
void KeyDown();                 //检测按键函数


void main(void)
{

        while(1)
        {
                        unsigned char temp=0;
                        KeyDown();
                        TMOD=0x20;                                                       
                        TH1=0xfd;
                        TL1=0xfd;
                        SCON=0x40;
                        PCON=0x00;
                        TR1=1;                 
       
                i=KeyValue;
                if(i<=9)
                {
                         shuchu0 = num[0];
                         shuchu2 = num[KeyValue];
                }
                else
                {
                        i=i-10;
                        shuchu0 = num[1];
                        shuchu2 = num[i];

                }
                while(1){          
                                        temp=KeyValue;
                                        SBUF=temp;
                                        while(TI==0);
                                                TI=0;
                                        break;
                                         }                                        
                        }
       
                               
}

/*******************************************************************************
* 函 数 名         : KeyDown
* 函数功能                   : 检测有按键按下并读取键值
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/

void KeyDown(void)
{
        char a = 0;
        KEY=0x0f;
        if(KEY!=0x0f)//读取按键是否按下
        {
                Delay10ms(1);//延时10ms进行消抖
                if(KEY!=0x0f)//再次检测键盘是否按下
                {
                       
                        //测试列
                        KEY=0X0F;
                        switch(KEY)
                        {
                                case(0X07):        KeyValue=0;break;
                                case(0X0b):        KeyValue=4;break;
                                case(0X0d): KeyValue=8;break;
                                case(0X0e):        KeyValue=12;break;
                        }
                        //测试行
                        KEY=0XF0;
                        switch(KEY)
                        {
                                case(0X70):        KeyValue=KeyValue+3;break;
                                case(0Xb0):        KeyValue=KeyValue+2;break;
                                case(0Xd0): KeyValue=KeyValue+1;break;
                                case(0Xe0):        KeyValue=KeyValue;break;
                        }
                        while((a<50) && (KEY!=0xf0))         //检测按键松手检测
                        {
                                Delay10ms(1);
                                a++;
                        }
                }
        }
}


void Delay10ms(unsigned int c)   
{
    unsigned char a, b;
    for (;c>0;c--)
        {
                for (b=38;b>0;b--)
                {
                        for (a=130;a>0;a--);
                }         
        }      
}
从机#include<reg51.h>  
unsigned char code num[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};          //共yang数码管
unsigned char i;
#define shuchu0 P0       
#define shuchu2 P2
void main(void)
{
         while(1){
                unsigned char temp=0;
                TMOD=0x20;
                TH1=0xfd;
                TL1=0xfd;
                SCON=0x50;
                PCON=0x00;
                TR1=1;        
                while(1)
                        {
                        while(RI==0);
                        RI=0;
                        temp=SBUF;
                         i=temp;
                         if(i<=9)
                {
                         shuchu0 = num[0];
                         shuchu2 = num[i];
                }
                else
                {
                        i=i-10;
                        shuchu0 = num[1];
                        shuchu2 = num[i];

                }        
                        }
                }               
}  


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表