51单片机计时器实现1000以内的计数功能

#include <reg51.h>


#include <intrins.h>


unsigned char code LED7Code[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,//0-9字模


0x88,0x83,0xc6,0xa1,0x86,0x8e};//共阳极7段数码管A-F字模,原来的是错误的.


unsigned char num,temp;
unsigned int a, b,c;


void system_init()


{


  EA=1; //开中断总开关


  ET0=1;//加一行,开定时器T0中断


  TMOD=0x01; //定时器0方式1


  TH0=(65535-50000)/256; //定时50ms


  TL0=(65535-50000)%256;


  num=0;


  temp=0;


  TR0=1; //开定时器0


  P0=LED7Code[num]; //不能是num++


}
void delay(int m){
while(m--);
}
void main()


{


  system_init(); //参数初始化


  while(1)


  {
   if(temp==20) //定时1s,刷新数码管数字显示


{

temp=0; //标志位置零,计数程序修改后是这样的

num++; //原来的写法有问题

//显示字符,不能是num++

}

    a = num % 10;
    b = num/10%10;
c = num/100;
P2 = 0xdf;
delay(500);
P0=LED7Code[a];
P2 = 0x7f;
delay(500);
P0= LED7Code[b];
P2=0xbf;
delay(500);
P0= LED7Code[c];


  }


}


void Time() interrupt 1 using 1    //中断函数


{


    TH0=(65535-50000)/256; //定时50ms


    TL0=(65535-50000)%256;


temp++;


}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值