中斷是硬體或軟體向處理器發出的信號,表示需要立即注意的事件。每當發生中斷時,控制器完成當前指令的執行,並開始執行中斷服務程序(ISR)或中斷處理程序。ISR告訴處理器或控制器當中斷髮生時該做什麼。中斷可以是硬體中斷,也可以是軟體中斷。
Hardware Interrupt
硬體中斷是從外部設備(如磁碟控制器或外部外設)發送到處理器的電子警報信號。例如,當我們按下鍵盤上的鍵或移動滑鼠時,它們會觸發硬體中斷,導致處理器讀取按鍵或滑鼠位置。
Software Interrupt
軟體中斷是由異常情況或指令集中的特殊指令引起的,該指令集在由處理器執行時引起中斷。例如,如果處理器的算術邏輯單元運行一個將數字除以零的命令,則會導致除以零異常,從而導致計算機放棄計算或顯示錯誤消息。軟體中斷指令的工作方式類似於子程序調用。
What is Polling?
連續監視的狀態稱爲輪詢。微控制器不斷地檢查其他設備的狀態;在這樣做的同時,它不做其他操作,並且消耗所有的處理時間來進行監視。這個問題可以通過使用中斷來解決。
在中斷方法中,控制器僅在中斷髮生時才響應。因此,控制器不需要定期監視接口和內置設備的狀態(標誌、信號等)。
Interrupts v/s Polling
這裡有一個類比,它區分了中斷和輪詢;
Interrupt | Polling |
---|---|
An interrupt is like a shopkeeper. If one needs a service or product, he goes to him and apprises him of his needs. In case of interrupts, when the flags or signals are received, they notify the controller that they need to be serviced. | The polling method is like a salesperson. The salesman goes from door to door while requesting to buy a product or service. Similarly, the controller keeps monitoring the flags or signals one by one for all devices and provides service to whichever component that needs its service. |
Interrupt Service Routine
對於每個中斷,必須有一個中斷服務程序(ISR),或中斷處理程序。當中斷髮生時,微控制器運行中斷服務程序。對於每個中斷,內存中都有一個固定的位置,用來保存中斷服務程序ISR的地址。爲保存isr地址而預留的存儲器位置表稱爲中斷向量表。
Interrupt Vector Table
有六個中斷,包括8051中的復位。
Interrupts | ROM Location (Hex) | Pin |
---|---|---|
Interrupts | ROM Location (HEX) | |
Serial COM (RI and TI) | 0023 | |
Timer 1 interrupts(TF1) | 001B | |
External HW interrupt 1 (INT1) | 0013 | P3.3 (13) |
External HW interrupt 0 (INT0) | 0003 | P3.2 (12) |
Timer 0 (TF0) | 000B | |
Reset | 0000 | 9 |
當復位引腳被激活時,8051跳到地址位置0000。這是上電復位。
爲定時器預留了兩個中斷:一個用於定時器0,一個用於定時器1。中斷向量表中的內存位置分別爲000BH和001BH。
爲硬體外部中斷留出兩個中斷。埠3中的引腳12和引腳13分別用於外部硬體中斷INT0和INT1。中斷向量表中的內存位置分別爲0003H和0013H。
串行通信有一個屬於接收和發送的單一中斷。內存位置0023H屬於此中斷。
Steps to Execute an Interrupt
當中斷激活時,微控制器將執行以下步驟;
微控制器關閉當前執行的指令並將下一條指令(PC)的地址保存在堆棧上。
它還會在內部保存所有中斷的當前狀態(即不在堆棧上)。
它跳到中斷向量表的內存位置,該表保存中斷服務例程的地址。
微控制器從中斷向量表中獲取ISR的地址並跳到它。它開始執行中斷服務子程序RETI(從中斷返回)。
在執行RETI指令時,微控制器返回到中斷的位置。首先,它從堆棧中獲取程序計數器(PC)地址,方法是將堆棧的頂部字節彈出到PC中,然後從該地址開始執行。
Edge Triggering vs. Level Triggering
中斷模塊有兩種類型:負電平觸發或邊緣觸發。
Level Triggered | Edge Triggered |
---|---|
A level-triggered interrupt module always generates an interrupt whenever the level of the interrupt source is asserted. | An edge-triggered interrupt module generates an interrupt only when it detects an asserting edge of the interrupt source. The edge gets detected when the interrupt source level actually changes. It can also be detected by periodic sampling and detecting an asserted level when the previous sample was de-asserted. |
If the interrupt source is still asserted when the firmware interrupt handler handles the interrupt, the interrupt module will regenerate the interrupt, causing the interrupt handler to be invoked again. | Edge-triggered interrupt modules can be acted immediately, no matter how the interrupt source behaves. |
Level-triggered interrupts are cumbersome for firmware. | Edge-triggered interrupts keep the firmware's code complexity low, reduce the number of conditions for firmware, and provide more flexibility when interrupts are handled. |
Enabling and Disabling an Interrupt
重置後,所有中斷都將被禁用,即使它們已被激活。必須使用軟體啓用中斷,以便微控制器對這些中斷做出響應。
IE(中斷啓用)寄存器負責啓用和禁用中斷。IE是一個位尋址寄存器。
Interrupt Enable Register
EA | - | ET2 | ES | ET1 | EX1 | ET0 | EX0 |
---|
EA−全局啓用/禁用。
-−未定義。
ET2−啓用定時器2中斷。
ES−啓用串行埠中斷。
ET1−啓用定時器1中斷。
EX1−啓用外部1中斷。
ET0−啓用定時器0中斷。
EX0−啓用外部0中斷。
爲了實現中斷,我們採取以下步驟&負;
IE寄存器(EA)的D7位必須高,以使其餘寄存器生效。
如果EA=1,中斷將被啓用,如果它們在IE中的對應位高,中斷將被響應。如果EA=0,即使IE寄存器中的相關管腳很高,也不會有中斷響應。
Interrupt Priority in 8051
我們可以通過給任何一個中斷分配更高的優先級來改變中斷優先級。這是通過編程一個名爲IP(中斷優先級)的寄存器來實現的。
下圖顯示了IP寄存器的位。重置後,IP寄存器包含所有0。爲了給任何中斷賦予更高的優先級,我們將IP寄存器中的相應位設爲高。
- | - | - | - | PT1 | PX1 | PT0 | PX0 |
---|
- | IP.7 | Not Implemented. |
- | IP.6 | Not Implemented. |
- | IP.5 | Not Implemented. |
- | IP.4 | Not Implemented. |
PT1 | IP.3 | Defines the Timer 1 interrupt priority level. |
PX1 | IP.2 | Defines the External Interrupt 1 priority level. |
PT0 | IP.1 | Defines the Timer 0 interrupt priority level. |
PX0 | IP.0 | Defines the External Interrupt 0 priority level. |
Interrupt inside Interrupt
What happens if the 8051 is executing an ISR that belongs to an interrupt and another one gets active? In such cases, a high-priority interrupt can interrupt a low-priority interrupt. This is known as interrupt inside interrupt. In 8051, a low-priority interrupt can be interrupted by a high-priority interrupt, but not by any another low-priority interrupt.
Triggering an Interrupt by Software
There are times when we need to test an ISR by way of simulation. This can be done with the simple instructions to set the interrupt high and thereby cause the 8051 to jump to the interrupt vector table. For example, set the IE bit as 1 for timer 1. An instruction SETB TF1 will interrupt the 8051 in whatever it is doing and force it to jump to the interrupt vector table.