|
大家参考参考,可以提提宝贵建议
- #include <reg52.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- uint countb=0,counta=0; // counta 为10ms计数器,countb为1s计数器,均为全局变量
- sbit s1 = P2^0;
- sbit s2 = P2^1;
- sbit s3 = P2^2;
- sbit s4 = P2^3;
- sbit en = P2^5;
- sbit speaker = P2^4;
- sbit k1=P3^2;
- sbit k2=P3^3;
-
- uchar a1,a2,b1,b2;
- code ledseg7[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; //0~9不带小数点的段码表
- // 0 1 2 3 4 5 6 7 8 9
- code ledseg8[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
- // 0 1 2 3 4 5 6 7 8 9 //0~9带小数点的段码表
- /********延时函数********/
- void Delay(uint xms) //延时程序,xms是形式参数
- {
- uint i,j;
- for(i=xms;i>0;i--)
- for(j=100;j>0;j--); //此处分号不可少
- }
- /*******显示函数********/
- void display(uchar a,uchar b,uchar c,uchar d)
- {
- en = 1; //启用数码管信号
- s1 = 0;
- P0 = ledseg7[a];
- Delay(5);
- s1 = 1;
- s2 = 0;
- P0 = ledseg8[b];
- Delay(5);
- s2 = 1;
- s3 = 0;
- P0 = ledseg7[c];
- Delay(5);
- s3 = 1;
- s4 = 0;
- P0 = ledseg7[d];
- Delay(5);
- s4 = 1;
- }
- /********重置,暂停设置******/
- void key()
- {
- if(k1==0) //开始,暂停
- {
- Delay(10);
- if(k1==0)
- {
- while(!k1);
- TR0=~TR0;
- }
- }
- if(k2==0) //复位
- {
- Delay(10);
- if(k2==0)
- {
- TH0=0xDC; TL0=0x00;
- countb=0;
- counta=0;
- TR0=0;
- while(k2!=0);
- }
- }
-
- }
- /********定时器T0中断函数*******/
- void timer0() interrupt 1
- {
- TH0=0xDC;TL0=0x00;
- counta++; //计数值加1
- if(counta==100) //若counta为99,(100*10ms=1s)
- {
- counta=0; // counta清0
- countb++;
- }
- if(countb==30)
- {
- TR0=0;
- s3=1;
- counta=0,countb=0;
- speaker=0;
- Delay(500);
- speaker=1;
- Delay(500);
- speaker=0;
- Delay(500);
- speaker=1;
- }
- }
- void main()
- {
- P0=0xff;
- TMOD=0x01; //定时器T0方式1
- TH0=0xDC; TL0=0x00; //10ms定时初值
- TR0=0;
- EA=1;
- ET0=1; //开总中断,开定时器T0中断,启动定时器T0
- while(1)
- {
- key();
- b2=countb/10; //取出记数秒数的十位
- b1=countb%10; //取出计数秒的个位
- a2=counta/10; //取出记数百分秒数的十位
- a1=counta%10; //取出计数百分秒的个位
- display(b2,b1,a2,a1); //调显示函数
- }
- }
复制代码
|
评分
-
查看全部评分
|