屏幕截圖 2022-11-30 220739.png (75.54 KB, 下載次數: 31)
下載附件
2022-11-30 22:07 上傳
- #include<reg51.h>
- unsigned char code table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- unsigned char count;
- int time_count;
- sbit clk = P1^0;
- sbit set = P3^4;
- sbit start = P3^5;
- char start_flag = 0;
- void delay(unsigned int time)
- {
- unsigned int j = 0;
- for(; time>0; time--)
- for(j=0; j<125; j++);
- }
- key0() interrupt 0
- {
- delay(10);
- count++;
- if(count==20)
- count=0;
- }
- void display_mode()
- {
- P2 = 0x00;
- delay(500);
- P2 = 0x3f;
- delay(500);
- }
- void time_init()
- {
- TMOD=0x10; //工作于方式1
- //0 0 方式0 13位計數器 TMOD=0x00
- //0 1 方式1 16位計數器 TMOD=0x01
- //1 0 方式2 自動重裝8位計數器 TMOD=0x02
- //1 1 方式3 T0分為2個8位獨立計數器,T1為無中斷重裝8位計數器 TMOD=0x03
- EA=1; // 中斷允許
- ET1=1; // 中斷1打開
- TH1=(65536-50000)/256; // (65536-50000)/256=60.6875
- TL1=(65536-50000)%256;
- // TR1=1;
- }
- void timer1(void) interrupt 3
- {
- TH1=(65536-50000)/256;
- TL1=(65536-50000)%256;
- time_count++;
- if (time_count==20) //達到1s
- {
- time_count = 0;
- clk = ~clk;
- }
- }
- void main(void)
- {
- EA = 1;
- EX0 = 1;
- IT0 = 1;
- time_init();
- P0 = table[0];
- P2 = table[0];
- display_mode();
- while(1)
- {
- if(set == 0)
- {
- delay(200);
- if(set == 0)
- {
- count++;
- if(count == 20)
- count = 0;
- }
- }
- if(start == 0)
- {
- delay(200);
- if(start == 0)
- {
- if(start_flag == 0)
- {
- start_flag = 1;
- TR1=1;
- }
- else if(start_flag == 1)
- {
- start_flag = 0;
- TR1=0;
- }
- }
- }
- P0=table[count/10];
- P2=table[count%10];
- }
- }
復制代碼
|