给你一个示例参考
- #include <STC89C5xRC.H>
- unsigned char code table[]={
- 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- unsigned char code wela[]={
- 0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
- unsigned char data dis_buf[6];
- unsigned int num,count;
- unsigned char tim50ms,i,j;
- bit flag=0;
- void Timer0Init(void) //50毫秒@12.000MHz
- {
- TMOD= 0x51; //设置定时器模式
- TH0 = 0x3C; //设置定时初值
- TL0 = 0xB0; //设置定时初值
- TH1 = 0x00; //设置定时初值
- TL1 = 0x00; //设置定时初值
- TR0 = 1; //定时器0开始计时
- TR1 = 1; //定时器1开始计数
- EA = 1; //开总中断
- ET0 = 1; //开定时器0中断
- }
- void main()
- {
- Timer0Init();
- for(i=0;i<6;i++)
- {//显示缓存初始为0
- dis_buf[i]=table[0];
- }
- while(1)
- {
- if(flag)
- {
- flag=0;
- count=num;
- for(i=0;i<5;i++)
- {//数据分解
- dis_buf[5-i]=table[count%10];//由低位到高位保存到显示缓存
- count/=10;
- }
- }
- P0=0x00;//消隐
- P2=wela[j+1];//送段码
- P0=dis_buf[j+1];//送位码
- j++;
- j%=5;
- }
- }
- void timer0() interrupt 1 //定时器0周期50ms
- {
- tim50ms++;
- if(tim50ms==20)//1s时间到
- {
- tim50ms=0;
- TR1 = 0; //关闭定时器1
- num=TH1<<8|TL1;//读取T1计数值就是外部脉冲频率(不大于65KHz)
- TH1 = 0x00; //清0
- TL1 = 0x00; //清0
- TR1 = 1; //定时器1开始计数
- flag= 1;
- }
- TH0 = 0x3C; //重新设置定时初值
- TL0 = 0xB0; //重新设置定时初值
- }
复制代码
|