|
- /*数码管前三位显示一个跑表,从000到999之间以1%秒速度运行
- */
- #include
- #define UINT unsigned int
- #define UCHAR unsigned char
- sbit s2 = P3 ^ 4;
- sbit s3 = P3 ^ 5;
- sbit s4 = P3 ^ 6;
- sbit s5 = P3 ^ 7;
- sbit dula = P2 ^ 6;
- sbit wela = P2 ^ 7;
- UINT time, cnt;
- /*0 1 2 3 4 5 6
- *7 8 9 A b C d
- *E F H L P U .
- */
- UCHAR code table[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d,
- 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x37, 0x5e,
- 0x79, 0x71, 0x76, 0x38, 0x73, 0x3e, 0x80,
- 0x00};
- void main()
- {
- char pressKey();
- void display(UCHAR, UCHAR, UCHAR);
- UCHAR digits, tens, hundreds;
- time = cnt = 0;
- digits = tens = hundreds = 0;
- TMOD = 0X01;
- EA = 1;
- ET0 = 1;
- while(1)
- {
- if(pressKey() == 1) //开始
- {
- ET0 = 1;
- TR0 = 1;
- }
- else if(pressKey() == 2) //pause
- TR0 = 0;
- else if(pressKey() == 3) //stop,清零, 按下这个键后无法实现清零
- {
-
- TR0 = 0;
- ET0 = 0;
- time = 0;
- cnt = 0;
- }
- else if(pressKey() == 4) //清零并重新计时,这里的功能也无法实现
- {
- ET0 = 0;
- TR0 = 0;
- time = 0;
- cnt = 0;
- ET0 = 1;
- TR0 = 1;
- }
- digits = time % 10;
- tens = time / 10 % 10;
- hundreds = time / 100;
- display(hundreds, tens, digits);
- }
- }
- void display(UCHAR tube1, UCHAR tube2, UCHAR tube3)
- {
- void delay(UINT);
- wela = 1;
- P0 = 0xff;
- wela = 0;
- dula = 1;
- P0 = table[tube1];
- dula = 0;
- wela = 1;
- P0 = 0xfe;
- wela = 1;
- delay(1);
-
- wela = 1;
- P0 = 0xff;
- wela = 0;
- dula = 1;
- P0 = table[tube2];
- dula = 0;
- wela = 1;
- P0 = 0xfd;
- wela = 1;
- delay(1);
-
- wela = 1;
- P0 = 0xff;
- wela = 0;
- dula = 1;
- P0 = table[tube3];
- dula = 0;
- wela = 1;
- P0 = 0xfb;
- wela = 1;
- delay(1);
- }
- char pressKey()
- {
- void delay(UINT);
- if(!s2)
- {
- delay(5);
- while(!s2)
- ;
- return 1;
- }
- else if(!s3)
- {
- delay(5);
- while(!s3)
- ;
- return 2;
- }
- else if(!s4)
- {
- delay(5);
- while(!s4)
- ;
- return 3;
- }
- else if(!s5)
- {
- delay(5);
- while(!s5)
- ;
- return 4;
- }
- return 0;
- }
- void timer0() interrupt 1
- {
- TH0 = (65536 - 50000) / 256;
- TL0 = (65536 - 50000) % 256;
- if(++cnt == 2)
- {
- cnt = 0;
- if(++time == 1000)
- time = 0;
- }
- }
- void delay(UINT ms)
- {
- UINT x, y;
- for(x = ms; x; x--)
- for(y = 197; y; y--)
- ;
- }
复制代码
停止和停止并清零这两个功能实现不了,,应该怎么使计时器停止工作和重新工作呢?
- /*哪里出错了*/
- else if(pressKey() == 3) //stop,清零, 按下这个键后无法实现清零
- {
-
- TR0 = 0;
- ET0 = 0;
- time = 0;
- cnt = 0;
- }
- else if(pressKey() == 4) //清零并重新计时,这里的功能也无法实现
- {
- ET0 = 0;
- TR0 = 0;
- time = 0;
- cnt = 0;
- ET0 = 1;
- TR0 = 1;
- }
复制代码
|
|