From 0d809b77ba6b5fe1f6433abd965822660b659120 Mon Sep 17 00:00:00 2001 From: jj97181818 Date: Mon, 14 May 2018 01:34:54 +0800 Subject: [PATCH] add segment display --- Interrupt_1.c | 193 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 116 insertions(+), 77 deletions(-) diff --git a/Interrupt_1.c b/Interrupt_1.c index 2d86351..7ed3341 100644 --- a/Interrupt_1.c +++ b/Interrupt_1.c @@ -7,20 +7,66 @@ void delay_1500ms(void); unsigned int timer0_counter = 150; +int seg_pos = 0, seg_num = 0; +//LED bit lightoff = 1; bit lighton = 0; -sbit P1_0 = P1^0; -sbit P1_1 = P1^1; -sbit P1_2 = P1^2; -sbit P1_3 = P1^3; -sbit P1_4 = P1^4; -sbit P1_5 = P1^5; -sbit P1_6 = P1^6; -sbit P1_7 = P1^7; -void main(void){ - +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 +}; + + +int main(void){ + int i = 0; //開啟 Timer0 和 Timer1 TMOD = 0x11; @@ -32,93 +78,45 @@ void main(void){ ET0 = 1; //開啟 TF0 的中斷模組開關 EA = 1; //開啟中斷模組總開關 + while(1){ - // do LED move up to down per 1.5 sec - // using Timer 1 Mode 1 to generate 10ms delay - P1 = 0xFF; - while (1) { + // ********* 實作 LED ********* + for(i = 0; i < 16; i++) { + P1 = LED_light[i]; delay_1500ms(); - - P1_0 = lighton; - delay_1500ms(); - - P1_1 = lighton; - delay_1500ms(); - - P1_2 = lighton; - delay_1500ms(); - - P1_3 = lighton; - delay_1500ms(); - - P1_4 = lighton; - delay_1500ms(); - - P1_5 = lighton; - delay_1500ms(); - - P1_6 = lighton; - delay_1500ms(); - - P1_7 = lighton; - delay_1500ms(); - - P1_7 = lightoff; - delay_1500ms(); - - P1_6 = lightoff; - delay_1500ms(); - - P1_5 = lightoff; - delay_1500ms(); - - P1_4 = lightoff; - delay_1500ms(); - - P1_3 = lightoff; - delay_1500ms(); - - P1_2 = lightoff; - delay_1500ms(); - - P1_1 = lightoff; - delay_1500ms(); - - P1_0 = lightoff; - } + } } } void delay_1500ms(void){ - int time = 0; - while(time < 150) { + int i; + for (i = 0; i < 150; i++) { //設定初始值 - TF1 = 0; TR1 = 0; - TL1 = (65536-9216) % 256; - TH1 = (65536-9216) / 256; + TF1 = 0; + TL1 = (65536 - 9216) % 256; + TH1 = (65536 - 9216) / 256; //開啟計時器 timer1 TR1 = 1; //當 TF1 沒有溢位 while(TF1 == 0); - - //關閉計時器 - TR1 = 0; - - //將 TF1 歸零 - TF1 = 0; - time++; } + + //關閉計時器 + TR1 = 0; + + //將 TF1 歸零 + TF1 = 0; } // timer0 在計時到的時候(TF0 = 1)會呼叫的中斷副函式 void my_timer0(void) interrupt 1 { - + //設定初始值 TL0 = (65536 - 9216) % 256; TH0 = (65536 - 9216) / 256; @@ -130,11 +128,52 @@ void my_timer0(void) interrupt 1 { //當 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; } } + +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; +}