Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.



Published on


Published in: Education, Technology
  • Be the first to comment


  1. 1. 8051 Interrupt Programming By Dr. Min He.
  2. 2. 8051 Interrupts <ul><li>Interrupt sources and priority levels </li></ul><ul><li>Interrupt polling priority </li></ul><ul><li>Interrupt vector table </li></ul><ul><li>Registers used by interrupts </li></ul><ul><li>Hardware steps in executing interrupts </li></ul><ul><li>Software steps to enable an interrupt </li></ul><ul><li>Handling external interrupts </li></ul><ul><li>Interrupt Programming </li></ul>
  3. 3. Introducing Interrupts <ul><li>Interrupts and resets are among the most useful mechanisms that a computer system provides. </li></ul><ul><li>Benefits provided by interrupts and resets: </li></ul><ul><ul><li>I/O operations are performed more efficiently </li></ul></ul><ul><ul><li>Errors a r e handled more smoothly, and </li></ul></ul><ul><ul><li>CPU utilization is improved. </li></ul></ul><ul><li>An interrupt is an event that requires the CPU to stop normal program execution and perform a special piece of code called Interrupt Service Routine (ISR) related to the event. </li></ul><ul><ul><li>External Interrupt: an external signal that is used to get the attention of the CPU </li></ul></ul><ul><ul><li>Internal Interrupt: all microcontrollers has on-chip peripheral devices such as timers, A/D converters, serial peripheral interface, and so on. Signals asserted by these devices to get the attentions of the CPU are called internal interrupts. </li></ul></ul><ul><ul><li>Software Interrupt: internal interrupt generated by software errors such as illegal opcodes, overflow, divided-by-zero, and underflow. </li></ul></ul>
  4. 4. Interrupt Service Routine <ul><li>Similar to a procedure call, except that it </li></ul><ul><li>can occur between any two instructions of the program </li></ul><ul><li>is transparent to the running program (usually) </li></ul><ul><li>is typically not explicitly requested by the program </li></ul><ul><li>calls a routine at an address determined by the type of interrupt, not by the program </li></ul><ul><li>atomically changes some processor mode bits in some special function register, such as TCON in 8051. </li></ul>
  5. 5. Common 8051 Interrupts <ul><li>There are 5 interrupt sources in common 8051. A unique number is assigned to each interrupt: </li></ul><ul><li>Can set up two interrupt levels. </li></ul>Interrupt Name Number External Interrupt 0 INT0 0 Timer Interrupt 0 TF0 1 External Interrupt 1 INT1 2 Timer Interrupt 1 TF1 3 Serial Interrupt 1 R1+T1 4 Timer 2 TF2 5
  6. 6. Interrupt Polling Priority Upon Reset <ul><li>External Interrupt 0 </li></ul><ul><li>Timer Interrupt 0 </li></ul><ul><li>External Interrupt 1 </li></ul><ul><li>Timer Interrupt 1 </li></ul><ul><li>Serial Communication </li></ul>
  7. 7. Interrupt Vector Table Interrupt ISR ROM Address (Hex) Pin Flag Clearing Reset 0000 9 Auto External Hardware interrupt 0 (INT0) 0003 P3.2 Auto Timer 0 Interrupt (TF0) 000B Auto External Hardware interrupt 1 (INT1) 0013 P3.3 Auto Timer 1 Interrupt (TF1) 001B Auto Serial COM Interrupt (RI and TI) 0023 Programmer clears it.
  8. 8. Registers Used by Interrupts <ul><li>IE (Interrupt Enable) : to enable interrupts </li></ul><ul><li>IP: set interrupt priority if needed </li></ul><ul><li>TCON: if external interrupt is used, set control bit: edge-triggered vs. level-triggered. </li></ul>EA -- ET2 ES ET1 EX1 ET0 EX0 TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 -- -- PT2 PS PT1 PX1 PT0 PX0
  9. 9. Hardware Steps in Executing an Interrupt <ul><li>Finish executing current instruction and save the address of the next one (PC) on the stack. </li></ul><ul><li>Save status of all the interrupts internally (not on the stack). </li></ul><ul><li>Jump to interrupt vector table </li></ul><ul><li>Get the address of the ISR from interrupt vector table, and jump to execute the ISR. </li></ul><ul><li>The microcontroller get PC from top of stack and returns to the place where it was interrupted upon executing the RETI instruction of the ISR. </li></ul>
  10. 10. Software Steps to Enable an Interrupt <ul><li>Set EA to enable interrupt </li></ul><ul><ul><li>EA=1; </li></ul></ul><ul><li>Set the corresponding interrupt bit in IE to enable a specific interrupt Example: ET0 = 1; enable timer 0 overflow interrupt EX0 = 1; enable external interrupt 0 </li></ul>
  11. 11. External Interrupts --Level-triggered vs. Edge-triggered <ul><li>Level-triggered: </li></ul><ul><li>Default mode upon reset of the 8051 </li></ul><ul><li>A low-level signal triggers the interrupt. </li></ul><ul><li>The low signal must be held low before the execution of ISR and must be removed before RETI. </li></ul><ul><li>Edge-triggered: </li></ul><ul><li>A high-to-low signal triggers the interrupt </li></ul><ul><li>Need to set TCON.0(IT0)/TCON.2(IT1) </li></ul><ul><li>TCON.1(IE0) and TCON.3(IE1) are set when an falling edge is detected and cleared by RETI. </li></ul>
  12. 12. Use TCON Register to Setup External Interrupts D7 D0 <ul><li>IE1/IE0 : external interrupt 1 & 0 edge flag. Set by CPU when the external interrupt edge is detected. Cleared by CPU when the interrupt is processed. </li></ul><ul><li>IT1/IT0 : external interrupt 1 & 0 type control bit. Set/cleared by software to specify falling edge/low-level triggered interrupts. </li></ul>TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
  13. 13. Sampling of External Interrupts <ul><li>Level-triggered external interrupt: To ensure the activation of the hardware interrupt at the INTn pin, the duration of the low-level signal should be around 4 machine cycles, but not more. This is due to the fact that the level-triggered interrupt is not latched. </li></ul><ul><li>Edge-triggered external interrupt: the external source must be held high for at least one machine cycle, and then held low for at least one machine cycle to ensure that the transition is seen by the microcontroller. </li></ul>
  14. 14. Interrupt-in-service Flag: IEx <ul><li>Used to keep track of edge-triggered external interrupt only </li></ul><ul><li>The falling edge is latched by interrupt-in-service flag: IE0/IE1 is set by CPU when external interrupt is detected. </li></ul><ul><li>No other external interrupt on the same pin will be responded when IE0/IE1 is raised. </li></ul><ul><li>These flags are cleared when the ISRs are finished upon execution of instruction RETI. </li></ul>
  15. 15. Interrupt Programming <ul><li>In main: set interrupt mode Ex: Enable external interrupt 0, set it to be edge triggered: </li></ul><ul><li>IT0=1; // set INT0 to be edge triggered </li></ul><ul><li>EX0=1; // enable INT0 </li></ul><ul><li>EA=1; // enable interrupt </li></ul><ul><li>Write ISR function: </li></ul><ul><ul><li>bit my_flag; </li></ul></ul><ul><ul><li>… . </li></ul></ul><ul><ul><li>void chg_mode(void) interrupt 0 </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>my_flag=1; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Notice : No function prototype is needed for interrupt service routine. </li></ul>The interrupt keyword tells the compiler function chg_mode is an ISR. The number tell the compiler the interrupt source for the ISR.
  16. 16. Example: Furnace Controller <ul><li>Using interrupts, design an 8051 furnace controller that keeps a building at 20  C+/-1C  . </li></ul><ul><li>Solution: </li></ul><ul><li>The furnace ON/OFF solenoid is connected to P1.7 such that: P1.7 = 1 for furnace ON, P1.7=0 for furnace OFF. </li></ul><ul><li>Temperature sensor is connected to external interrupt 0 and 1 to provide HOT and COLD signal. </li></ul>
  17. 17. Reading Assignment <ul><li>“ Embedded System Design with the C8051 ”, by Han-Way Huang. ISBN: 978-0495-47174-5, Publisher: Cengage Learning, Copyright: 2009. Chapter 6, sections 1, 2, 4. </li></ul>