按一次键,开始按秒速依次显示1357EFH,再按一次键清屏,如此反复。
- #include <reg51.h>
- typedef unsigned int u16;
- typedef unsigned char u8;
- sbit S2=P3^0;
- u8 code display[]={0x06,0x4F,0x6D,0x07,0x79,0x71,0x76,0x38};//1357EFHL
- u16 i;
- u8 j,s;
- bit KeySec;
- bit flag;
- void InitTimer0()
- {
- TMOD = 0x01;
- TH0 = 0xFC;
- TL0 = 0x18;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
- void key_scan()
- {
- static bit sign;
- static u8 count;
- if(!S2)
- {
- if(++count>=10&&sign==0)
- {
- sign=1;
- KeySec=~KeySec;
- }
- }
- else
- {
- sign=0;
- count=0;
- }
- }
- void main()
- {
- InitTimer0();
- while(1)
- {
- if(flag)
- {
- flag=0;
- key_scan();
- if(KeySec)
- {
- P0=0x00;
- P2=~(0x01<<j);
- if(j<=s)
- P0=display[j];
- else P0=0x00;
- }
- else
- {
- P0=0x00;
- s=0;
- i=0;
- }
- }
- }
- }
- void Timer0Interrupt(void) interrupt 1
- {
- TH0 = 0xFC;
- TL0 = 0x18;
- if(++i>=1000)
- {
- i=0;
- if(++s>=7)
- s=7;
- }
- flag=1;
- j=++j%8;
- }
复制代码
|