KST-51:从999999开始的倒计时程序,只显示有效位

此程序为手把手教你学51单片机的例程。例程中实现只显示有效位(高位0不显示),实现了数码管稳定显示不抖动。

for结合break 实现只显示有效位。

switch结合i 实现数码管动态刷新


#include 

sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;

unsigned char code LedChar[] = {  
    0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
    0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char LedBuff[6] = { //数码管显示缓冲区,初值0xff启动时都不亮
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
unsigned char i = 0;   //动态扫描索引
unsigned int cnt = 0;  //记录T1中断次数
unsigned char flag1s = 0;  //1s定时标志

void main()
{
    char j;  //循环变量
    unsigned long sec = 1000000;  //记录经过的秒数,倒计时为初值999999+1
    unsigned char buf[6];   //中间转换缓冲区

    EA = 1;       //总中断
    ENLED = 0;    //使能U3
    ADDR3 = 1;    //需要动态改变ADDR0-2,所以不需要再初始化
    TMOD = 0x10;  //设置T1为模式1
    TH1  = 0xFC;  //为T1赋初值0xFC67,定时1ms
    TL1  = 0x67;
    ET1  = 1;     //使能T1中断
    TR1  = 1;     //启动T1
    
    while (1)
    {
        if (flag1s == 1)  //判断1s定时标志
        {
            flag1s = 0;   //1s定时标志清零
            sec--;        //秒技术自减1
            //将sec按十进制从低到高一次提取到buf数组中
            buf[0] = sec%10;
            buf[1] = sec/10%10;
            buf[2] = sec/100%10;
            buf[3] = sec/1000%10;
            buf[4] = sec/10000%10;
            buf[5] = sec/100000%10;
            //从最高位开始,遇到0不显示,遇到非0退出循环
            for (j=5; j>=1; j--)
            {
                if (buf[j] == 0)
                    LedBuff[j] = 0xFF;
                else
                    break;
            }
            //将剩余的有效数字为如实转换
            for ( ; j>=0; j--)  //for()其实未对j操作,j既保持上个循环结束时的值
            {
                LedBuff[j] = LedChar[buf[j]];
            }
        }
    }
}
/* 定时器1中断服务函数 */
void InterruptTimer1() interrupt 3
{
    TH1 = 0xFC;  //重新加载初值
    TL1 = 0x67;
    cnt++;       //中断次数计数值加1
    if (cnt >= 1000)  //1000次即为1s
    {
        cnt = 0;      //清零计数值以重新开始下一秒计时
        flag1s = 1;   //设置1s定时标志为1
    }
    //以下代码完成数码管动态扫描刷新
    P0 = 0xFF;   //显示消隐
    switch (i)
    {
        case 0: ADDR2=0; ADDR1=0; ADDR0=0; i++; P0=LedBuff[0]; break;
        case 1: ADDR2=0; ADDR1=0; ADDR0=1; i++; P0=LedBuff[1]; break;
        case 2: ADDR2=0; ADDR1=1; ADDR0=0; i++; P0=LedBuff[2]; break;
        case 3: ADDR2=0; ADDR1=1; ADDR0=1; i++; P0=LedBuff[3]; break;
        case 4: ADDR2=1; ADDR1=0; ADDR0=0; i++; P0=LedBuff[4]; break;
        case 5: ADDR2=1; ADDR1=0; ADDR0=1; i=0; P0=LedBuff[5]; break;
        default: break;
    }
}

 

你可能感兴趣的:(源代码,单片机)