// Timer 0 take as interrupt // Timer 1 take as interrupt #include // 宣告延時函式 void delay_1500ms(void); unsigned int timer0_counter = 150; unsigned int timer1_counter = 150; int seg_pos = 0, seg_num = 0; int LED_mode = 0; //LED bit lightoff = 1; bit lighton = 0; char code LED_light[17] = { ~0x00, ~0x01, ~0x03, ~0x07, ~0x0F, ~0x1F, ~0x3F, ~0x7F, ~0xFF, ~0x7F, ~0x3F, ~0x1F, ~0x0F, ~0x07, ~0x03, ~0x01 }; //Segment // 宣告 D 型正反器街接腳 sbit P2_6 = P2^6; sbit P2_7 = P2^7; // 宣告七段顯示器函式 void Slect_Seg(unsigned char number); void Display_Seg(unsigned char display); //宣告數字的表示 char code NUM[10] = { 0x3F, // 0 0x06, // 1 0x5B, // 2 0x4F, // 3 0x66, // 4 0x6D, // 5 0x7D, // 6 0x07, // 7 0x7F, // 8 0x6F // 9 }; char code POS[6] = { ~0x01, // 0xfe 就是 11111110 ~0x02, // 0000 0010 ~0x04, // 0000 0100 ~0x08, // 0000 1000 ~0x10, // 0001 0000 ~0x20 // 0010 0000 }; void main(void){ //開啟 Timer0 和 Timer1 TMOD = 0x11; //interrupt control (timer0) TL0 = (65536 - 9216) % 256; TH0 = (65536 - 9216) / 256; TF0 = 0; TR0 = 1; //開啟計時器 timer0 ET0 = 1; //開啟 TF0 的中斷模組開關 EA = 1; //開啟中斷模組總開關 //interrupt control (timer1) TL1 = (65536 - 9216) % 256; TH1 = (65536 - 9216) / 256; TF1 = 0; TR1 = 1; //開啟計時器 timer0 ET1 = 1; //開啟 TF0 的中斷模組開關 } // timer0 在計時到的時候(TF0 = 1)會呼叫的中斷副函式 void my_timer0(void) interrupt 1 { //設定初始值 TL0 = (65536 - 9216) % 256; TH0 = (65536 - 9216) / 256; TF0 = 0; // timer0_counter 一直遞減 timer0_counter--; //當 timer0_counter 為 0 時,實作七段顯示器 if (timer0_counter == 0){ // *********實作七段顯示器********* Slect_Seg(POS[seg_pos]); // 設定哪顆亮 Display_Seg(NUM[seg_num]); // 選數字 seg_pos = (seg_pos + 1) % 6; seg_num = (seg_num + 1) % 10; //將 timer0_counter 的值設回 150 timer0_counter = 150; } } // timer1 在計時到的時候(TF1 = 1)會呼叫的中斷副函式 void my_timer1(void) interrupt 3 { //設定初始值 TL1 = (65536 - 9216) % 256; TH1 = (65536 - 9216) / 256; TF1 = 0; // timer1_counter 一直遞減 timer1_counter--; //當 timer1_counter 為 0 時,實作七段顯示器 if (timer1_counter == 0){ // ********* 實作 LED ********* for(LED_mode = 0; LED_mode < 16; LED_mode++) { P1 = LED_light[LED_mode]; } //將 timer0_counter 的值設回 150 timer1_counter = 150; } } void Slect_Seg(unsigned char number) { int j = 0; // 關閉 D 型正反器(預防措施) P2_7 = 0; // 指定哪顆亮 P0 = number; // 開啟控制哪顆亮的 D 型正反器 P2_7 = 1; // 延時(吃哪顆亮) for(j = 0; j < 120; j++); // 關閉 D 型正反器(讓吃進去的那顆固定亮) P2_7 = 0; } void Display_Seg(unsigned char display) { int j = 0; // 關閉 D 型正反器(預防措施) P2_6 = 0; // 指定號碼 P0 = display; // 開啟控制哪顆亮的 D 型正反器 P2_6 = 1; // 延時(吃號碼) for(j = 0; j < 120; j++); // 關閉 D 型正反器(讓吃進去的號碼固定住) P2_6 = 0; }