C51 COMPILER V9.54 INTERRUPT_2 05/14/2018 01:49:10 PAGE 1 C51 COMPILER V9.54, COMPILATION OF MODULE INTERRUPT_2 OBJECT MODULE PLACED IN .\Objects\Interrupt_2.obj COMPILER INVOKED BY: C:\Keil_v5\C51\BIN\C51.EXE Interrupt_2.c OPTIMIZE(8,SPEED) BROWSE DEBUG OBJECTEXTEND PRINT(.\Listin -gs\Interrupt_2.lst) TABS(2) OBJECT(.\Objects\Interrupt_2.obj) line level source 1 // Timer 0 take as interrupt 2 // Timer 1 take as interrupt 3 4 #include 5 6 // 宣告延時函式 7 void delay_1500ms(void); 8 9 unsigned int timer0_counter = 150; 10 unsigned int timer1_counter = 150; 11 int seg_pos = 0, seg_num = 0; 12 int LED_mode = 0; 13 //LED 14 bit lightoff = 1; 15 bit lighton = 0; 16 17 char code LED_light[17] = { 18 ~0x00, 19 ~0x01, 20 ~0x03, 21 ~0x07, 22 ~0x0F, 23 ~0x1F, 24 ~0x3F, 25 ~0x7F, 26 ~0xFF, 27 ~0x7F, 28 ~0x3F, 29 ~0x1F, 30 ~0x0F, 31 ~0x07, 32 ~0x03, 33 ~0x01 34 }; 35 36 //Segment 37 // 宣告 D 型正反器街接腳 38 sbit P2_6 = P2^6; 39 sbit P2_7 = P2^7; 40 41 // 宣告七段顯示器函式 42 void Slect_Seg(unsigned char number); 43 void Display_Seg(unsigned char display); 44 45 //宣告數字的表示 46 char code NUM[10] = { 47 0x3F, // 0 48 0x06, // 1 49 0x5B, // 2 50 0x4F, // 3 51 0x66, // 4 52 0x6D, // 5 53 0x7D, // 6 54 0x07, // 7 C51 COMPILER V9.54 INTERRUPT_2 05/14/2018 01:49:10 PAGE 2 55 0x7F, // 8 56 0x6F // 9 57 }; 58 59 char code POS[6] = { 60 ~0x01, // 0xfe 就是 11111110 61 ~0x02, // 0000 0010 62 ~0x04, // 0000 0100 63 ~0x08, // 0000 1000 64 ~0x10, // 0001 0000 65 ~0x20 // 0010 0000 66 }; 67 68 69 void main(void){ 70 1 71 1 //開啟 Timer0 和 Timer1 72 1 TMOD = 0x11; 73 1 74 1 //interrupt control (timer0) 75 1 TL0 = (65536 - 9216) % 256; 76 1 TH0 = (65536 - 9216) / 256; 77 1 TF0 = 0; 78 1 TR0 = 1; //開啟計時器 timer0 79 1 ET0 = 1; //開啟 TF0 的中斷模組開關 80 1 EA = 1; //開啟中斷模組總開關 81 1 82 1 83 1 //interrupt control (timer1) 84 1 TL1 = (65536 - 9216) % 256; 85 1 TH1 = (65536 - 9216) / 256; 86 1 TF1 = 0; 87 1 TR1 = 1; //開啟計時器 timer0 88 1 ET1 = 1; //開啟 TF0 的中斷模組開關 89 1 } 90 91 92 // timer0 在計時到的時候(TF0 = 1)會呼叫的中斷副函式 93 void my_timer0(void) interrupt 1 { 94 1 95 1 //設定初始值 96 1 TL0 = (65536 - 9216) % 256; 97 1 TH0 = (65536 - 9216) / 256; 98 1 TF0 = 0; 99 1 100 1 // timer0_counter 一直遞減 101 1 timer0_counter--; 102 1 103 1 104 1 //當 timer0_counter 為 0 時,實作七段顯示器 105 1 if (timer0_counter == 0){ 106 2 107 2 // *********實作七段顯示器********* 108 2 Slect_Seg(POS[seg_pos]); // 設定哪顆亮 109 2 Display_Seg(NUM[seg_num]); // 選數字 110 2 seg_pos = (seg_pos + 1) % 6; 111 2 seg_num = (seg_num + 1) % 10; 112 2 113 2 //將 timer0_counter 的值設回 150 114 2 timer0_counter = 150; 115 2 116 2 } C51 COMPILER V9.54 INTERRUPT_2 05/14/2018 01:49:10 PAGE 3 117 1 } 118 119 120 // timer1 在計時到的時候(TF1 = 1)會呼叫的中斷副函式 121 void my_timer1(void) interrupt 3 { 122 1 123 1 //設定初始值 124 1 TL1 = (65536 - 9216) % 256; 125 1 TH1 = (65536 - 9216) / 256; 126 1 TF1 = 0; 127 1 128 1 // timer1_counter 一直遞減 129 1 timer1_counter--; 130 1 131 1 132 1 //當 timer1_counter 為 0 時,實作七段顯示器 133 1 if (timer1_counter == 0){ 134 2 135 2 // ********* 實作 LED ********* 136 2 for(LED_mode = 0; LED_mode < 16; LED_mode++) { 137 3 P1 = LED_light[LED_mode]; 138 3 } 139 2 140 2 //將 timer0_counter 的值設回 150 141 2 timer1_counter = 150; 142 2 143 2 } 144 1 } 145 146 147 void Slect_Seg(unsigned char number) { 148 1 int j = 0; 149 1 150 1 // 關閉 D 型正反器(預防措施) 151 1 P2_7 = 0; 152 1 153 1 // 指定哪顆亮 154 1 P0 = number; 155 1 156 1 // 開啟控制哪顆亮的 D 型正反器 157 1 P2_7 = 1; 158 1 159 1 // 延時(吃哪顆亮) 160 1 for(j = 0; j < 120; j++); 161 1 162 1 // 關閉 D 型正反器(讓吃進去的那顆固定亮) 163 1 P2_7 = 0; 164 1 } 165 166 void Display_Seg(unsigned char display) { 167 1 int j = 0; 168 1 // 關閉 D 型正反器(預防措施) 169 1 P2_6 = 0; 170 1 171 1 // 指定號碼 172 1 P0 = display; 173 1 174 1 // 開啟控制哪顆亮的 D 型正反器 175 1 P2_6 = 1; 176 1 177 1 // 延時(吃號碼) 178 1 for(j = 0; j < 120; j++); C51 COMPILER V9.54 INTERRUPT_2 05/14/2018 01:49:10 PAGE 4 179 1 180 1 // 關閉 D 型正反器(讓吃進去的號碼固定住) 181 1 P2_6 = 0; 182 1 } MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = 314 ---- CONSTANT SIZE = 33 ---- XDATA SIZE = ---- ---- PDATA SIZE = ---- ---- DATA SIZE = 10 ---- IDATA SIZE = ---- ---- BIT SIZE = 2 ---- END OF MODULE INFORMATION. C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)