- #include<reg52.h>
- unsigned char code Tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//段码表
- unsigned char x;
-
- sbit P34=P3^4; //T0控制管脚
- void delay1ms(unsigned int n) //1ms基准延时
- {
- unsigned char i;
- while(n--)
- for(i=0;i<125;i++);
- }
- void display(unsigned char x) //显示函数
- {
- P2=0xbf;
- P0=Tab[x/10]; //显示十位
- delay1ms(1);
- P2=0x7f;
- P0=Tab[x%10]; //显示个位
- delay1ms(1);
-
- }
- void main()
- {
-
- x=0;
- TMOD=0x06; //T0的计数器工作模式2 0000 0110 C/T'=1为计数器
- EA=1; //开放总中断
- EX0=1; //允许INT0中断
- ET0=1; //允许T0中断
-
- IT0=1; //INT0为负跳变触发中断
- TR0=1; //将T0的高,低位赋值255,只要按键一次就溢出,触发中断
- TH0=TL0=255;
- while(1) //无限循环等待中断
- display(x);
-
-
- }
- void int0() interrupt 1 //T0的中断编号为1
- {
- if(P34==0){
- delay1ms(20); //消陡
- if(P34==0){
- x=(x+1)%100;
- }
- }
- }
- void clear0() interrupt 0 //INT0的中断编号为0
- {
- x=0;
- }
复制代码
|