定时器大作业

功能:-9.99-99.99,每50ms增加0.01,终止状态为99.99

#include<reg52.h>
char duan[12]={0x3f,0x06,0x5b,0x4f, 0x66, 0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x40}; 
char wei[8] = {0xff, 0xfb, 0xf7, 0xf3, 0xef, 0xeb, 0xe7, 0xe3 };
char shownum[8] = {10,10,10,10,11,9,9,9};  //显示8位数值
char showpos = 4;   //显示位   
int  t0_time = 0;    //T0中断次数  0-50
char  incflag = 0;    //是否增1   
void T0int( )  interrupt 1  // T0定时1ms中断响应子函数 

     TH0 = 0xFC;
   TL0 = 0x18; 
    showpos++;
    if(showpos==8)
        showpos=4;
    if(incflag==0)
     {
        t0_time ++;
       if(t0_time==50)  //2用来调整数值,
       {  
            t0_time = 0;
          shownum[7]--;
            if(shownum[7]<0)
            {
                shownum[7]=9;
                shownum[6]--;
                if(shownum[6]<0)
                {
                    shownum[6]=9;
                    shownum[5]--;
                }
            }
        }
    }
    if(shownum[4]==11&&shownum[5]==0&&shownum[6]==0&&shownum[7]==0)
    {
        incflag=1;
        shownum[4]=0;
    }
    if(incflag==1)
    {
        t0_time++;
        if(t0_time==50)
        {
            t0_time=0;
            shownum[7]++;
            if(shownum[7]==10)
            {
                shownum[7]=0;
                shownum[6]++;
                if(shownum[6]==10)    
                {
                    shownum[6]=0;
                    shownum[5]++;
                    if(shownum[5]==10)
                    {
                        shownum[5]=0;
                        shownum[4]++;
                    }
                }
            }
        }
    }
    if(shownum[4]==9&&shownum[5]==9&&shownum[6]==9&&shownum[7]==9)
        incflag=-1;
}

void Show1(void)  //显示
{   
    P0 = duan[ shownum[showpos] ] +(showpos==5)*0x80;   //加小数点
    P2 = wei[showpos];         
}

void init()    //初始化
{  
TMOD = 0x01;  //参数设置
TH0 = 0xFC;    //定时1ms 
TL0 = 0x18;
ET0 = 1;
EA = 1;
TR0 = 1;
}
main()
{  
   init();
    while(1)      
        Show1();
}
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值