第八章 函数进阶和按键(2)

独立按键的综合应用,让其中一个数码管点亮,S2数字加一,S3数字减一,S4归零,S5从当前位置开始计时。

#include <reg52.h>

sbit KeyS2=P3^4;
sbit KeyS3=P3^5;
sbit KeyS4=P3^6;
sbit KeyS5=P3^7;

sbit d1=P1^0;

sbit dula=P2^6;
sbit wela=P2^7;

char flag=0;
static int cnt_time=0;

unsigned char code LedChar[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71
};
unsigned char num=0;

void delay();

void main()
{
    wela=1;
    P0=0xdf;
    wela=0;
    dula=1;
    P0=LedChar[num];
    dula=0;
    P3=0xff;
    TMOD=0x11;
    TH0=0xb8;
    TL0=0x00;
    TH1=0xdc;
    TL1=0x00;
    TR0=1;

    EA=1;
    ET0=1;
    ET1=1;
    while(1)
    {
        switch(flag)
        {
            case 1: flag=0;num++;if(num>=10) num=0;break;
            case 2: flag=0;if(num>0) num--;if(num<=0) num=9;break;
            case 3: flag=0;num=0;break;
            case 4: TR1=1; if(cnt_time>=100){cnt_time=0; num++;if(num>=10) num=0;}break;
            default:break;
        }
        dula=1;P0=LedChar[num];dula=0;          
    }

}

void Key_Test() interrupt 1
{
    static unsigned char cnt;
    TH0=0xb8;
    TL0=0x00;

    cnt++;
    if(KeyS2==0)
    {
        if(cnt>=10)
        {
            cnt=0;
            if(KeyS2==0)
            {
                flag=1;
            }
        }
    }
    if(KeyS3==0)
    {
        if(cnt>=10)
        {
            cnt=0;
            if(KeyS3==0)
            {
                flag=2;
            }
        }
    }
    if(KeyS4==0)
    {
        if(cnt>=10)
        {
            cnt=0;
            if(KeyS4==0)
            {
                flag=3;
            }
        }
    }
    if(KeyS5==0)
    {
        if(cnt>=10)
        {
            cnt=0;
            if(KeyS5==0)
            {
                flag=4;
            }
        }
    }

}

void T1_Timer() interrupt 3
{

    TH1=0xdc;
    TL1=0x00;
    cnt_time++;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值