找回密码
 立即注册

QQ登录

只需一步,快速开始

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

按键控制数码管加减演示

[复制链接]
跳转到指定楼层
楼主
ID:165487 发表于 2018-6-4 10:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
   #include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//段码
uchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
//待显示的3位缓冲
uchar Num_Buffer[]={0,0,0};
//按键代码,按键计数
uchar Key_Code,Key_Counts=0;
//延时
void DelayMS(uint x)
{
        uchar i;
        while(x--) for(i=0;i<120;i++);
}       
//显示函数
void Show_Counts_ON_DSY()
{
        uchar i,j=0x01;
        Num_Buffer[2]=Key_Counts/100;
        Num_Buffer[1]=Key_Counts/10%10;
        Num_Buffer[0]=Key_Counts%10;
        for(i=0;i<3;i++)
        {
                j=_cror_(j,1);
                P0=0xff;
                P0=DSY_CODE[Num_Buffer[i]];
                P2=j;
                DelayMS(1);
        }
}
//主程序
void main()
{
        uchar i;
        P0=0xff;
        P1=0xff;
        P2=0x00;
        Key_Code=0xff;
        while(1)
        {
                Show_Counts_ON_DSY();
                P1=0xff;
                Key_Code=P1;
                //有键按下时,数码管刷新显示30次,该行代码同时起到延时作用
                if(Key_Code!=0xff)
                for(i=0;i<30;i++) Show_Counts_ON_DSY();
                switch(Key_Code)
                {
                        case 0xfe:        if(Key_Counts<255) Key_Counts++;
                                                break;
                        case 0xfd:        if(Key_Counts>0) Key_Counts--;
                                                break;
                        case 0xfb:        Key_Counts=0;
                }
                Key_Code=0xff;
        }
}

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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